<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/interesting-packages-taken-from-r-pharma/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://medium.com/@zappingseb/interesting-packages-taken-from-r-pharma-e89639788180?source=rss-dbc9f652035a------2"> Stories by Sebastian Wolf on Medium</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A few month ago I joined the R/Pharma conference in Cambridge, MA.</p>
<figure><img alt="" src="https://i0.wp.com/cdn-images-1.medium.com/max/1024/1*uGAoQ-Ljn7LX4rrIAd2GPw.jpeg?w=456&ssl=1" data-recalc-dims="1"></figure>
<p>As a take away I thought of my project and how I can improve, with solutions others provided. Mainly solutions in <em>R</em> are <em>R-packages. </em>So I’m a R-Shiny programmer in a regulated environment, so the list of the solutions I took are mainly helping you, if you are providing a) Shiny Apps b) Statistical packages c) verified solutions. Let’s go and see which R-packages I did not know and now find really useful:</p>
<h3>Packrat</h3>
<p>What most of my co-workers are producing, are reports. Actually tons of statistical reports. As we work in a regulated environment, all reports are double-checked. Meaning you program it, and someone else programs it, too. You do not want to waste time, because there was an update of a mathematical package which leads to differences in a number. There is a really nice solution for that.</p>
<p>Packrat allows you to store all packages you are using for a certain session/project. The main guide for packrat can be found at the <a rel="nofollow" href="https://rstudio.github.io/packrat/commands.html" target="_blank">RStudio blog describing</a> it.</p>
<p>Packrat will not only store all packages, but also all project files. It’s integrated in RStudio’s user interface. It allows you to share projects along different co-workers really fast.</p>
<p>The main lack I see is the need for a server, where you store all these packages. This should be solved with RStudio’s new <a rel="nofollow" href="https://www.rstudio.com/products/package-manager/" target="_blank">Package Manager</a>. Another disadvantage is the incompatibility with some packages. I noticed that I could not use the BH package under R-3.4.2 with packrat and had to find a work-around for that.</p>
<h3>Diffdf</h3>
<p>I have to tell you that I wasted nearly 30% of some of my days comparing data.frames. It’s an important task in testing statistical outcome or any calculations done with a statistical application you compiled. In Pharmaceutical and diagnostics applications one of the most relevant aspects is the validity of data. To ensure that the data we use in clinical studies, quality assurance or on a daily basis just getting data from a co-worker. For me this task has not only been hard, but even harder to document.</p>
<p>The <a rel="nofollow" href="https://cran.r-project.org/web/packages/diffdf/index.html" target="_blank">diffdf</a> package by <a rel="nofollow" href="https://github.com/kieranjmartin" target="_blank">Kieran Martin</a> really solved that task. It not only provides you with a neat interface, but also with well arranged outcomes.</p>
<p>The basic diffdf example looks like this:</p>
<pre>library(diffdf)<br>iris2 &lt;- iris<br>for (i in 1:3) iris2[i,i] &lt;- i^2<br>iris2$new_var &lt;- &quot;hello&quot;<br>class(iris2$Species) &lt;- &quot;some class&quot;<br>diffdf(iris, iris2)</pre>
<p>You can see that basically one column is newly introduced, three values are changed in 3 different numeric columns and the type of a column is changed. All these three changes are displayed in a separate output. Additionally also things that did not change are mentioned, with can be really helpful, in case you do not check for full equality of the data frames you are comparing.</p>
<pre>Differences found between the objects!<br><br>A summary is given below.<br><br>There are columns in BASE and COMPARE with different classes !!<br>All rows are shown in table below<br><br>  ==================================<br>   VARIABLE  CLASS.BASE  CLASS.COMP <br>  ----------------------------------<br>   Species     factor    some class <br>  ----------------------------------<br><br>There are columns in COMPARE that are not in BASE !!<br>All rows are shown in table below<br><br>  =========<br>   COLUMNS <br>  ---------<br>   new_var <br>  ---------<br><br>Not all Values Compared Equal<br>All rows are shown in table below<br><br>  =================================<br>     Variable    No of Differences <br>  ---------------------------------<br>   Sepal.Length          1         <br>   Sepal.Width           1         <br>   Petal.Length          1         <br>  ---------------------------------<br><br><br>All rows are shown in table below<br><br>  ============================================<br>     VARIABLE    ..ROWNUMBER..  BASE  COMPARE <br>  --------------------------------------------<br>   Sepal.Length        1        5.1      1    <br>  --------------------------------------------<br><br><br>All rows are shown in table below<br><br>  ===========================================<br>    VARIABLE    ..ROWNUMBER..  BASE  COMPARE <br>  -------------------------------------------<br>   Sepal.Width        2         3       4    <br>  -------------------------------------------<br><br><br>All rows are shown in table below<br><br>  ============================================<br>     VARIABLE    ..ROWNUMBER..  BASE  COMPARE <br>  --------------------------------------------<br>   Petal.Length        3        1.3      9    <br>  --------------------------------------------</pre>
<p>The output is easily readable and covers all the information you need to do the expected: comparing two data frames. What I really like is the quick feedback on how many differences were observed. In case you have a lot of differences, expect you added +1 to every value of a column, you can immediately see this in the summary.</p>
<p>Additionally the detailed information, given not only the value difference, but also the position of the value in the table, is a huge advantage. Sometimes analyzing large cohorts of patients, can reveal a difference in measurement 99,880 and you do not want to scroll through a table of “matches” until you find this one difference. Therefore this detail view is a huge advantage against other packages.</p>
<h3>Archivist</h3>
<p>An R package designed to improve the management of results of data analysis. Key functionalities of this package include:</p>
<p>(i) management of local and remote repositories which contain R objects and their meta-data (objects’ properties and relations between them);</p>
<p>(ii) archiving R objects to repositories;</p>
<p>(iii) sharing and retrieving objects (and it’s pedigree) by their unique hooks;</p>
<p>(iv) searching for objects with specific properties or relations to other objects;</p>
<p>(v) verification of object’s identity and context of it’s creation.</p>
<p>This can be really important in reproducible data analytics. In pharmacological projects you often have to reproduce cases after really long time. The archivist package allows to store models, data sets and whole R objects, which can also be functions or expressions, in files. Now you can store the file in a long-term data storage and even after 10 years, using packrat + archivist you’ll be able to reproduce your study.</p>
<h4>Example for task (ii) — restore models</h4>
<p>This example gives a list of models stored inside the package</p>
<pre>library(archivist)<br>models &lt;- asearch(&quot;pbiecek/graphGallery&quot;, patterns = &quot;class:lm&quot;)<br>modelsBIC &lt;- sapply(models, BIC)</pre>
<pre>sort(modelsBIC)</pre>
<h4>Example for task (i) — store objects locally</h4>
<p>A data.frame comes with in my repository at <a rel="nofollow" href="https://github.com/zappingseb/RPharma2018packages#archivist" target="_blank">https://github.com/zappingseb/RPharma2018packages</a> in the arepo folder. Your task is to create a new data.frame, store it in the arepo_new folder and add it to the restored data.frame. If everything works out the sum of the data.frames shows up to be 2 for position (1,1).</p>
<pre>library(archivist)</pre>
<pre>repo &lt;- &quot;arepo_new&quot;<br>createLocalRepo(repoDir = repo, default = TRUE)</pre>
<pre>df &lt;- data.frame(x=c(1,2),y=c(2,3))<br>saveToRepo(df)</pre>
<pre>setLocalRepo(&quot;arepo&quot;)<br>df2 &lt;- loadFromLocalRepo(&quot;4a7369a8c51cb1e7efda0b46dad8195e&quot;,value = TRUE)</pre>
<pre>df_test &lt;- df + df2</pre>
<pre>print(df_test[1,1]==2)</pre>
<p>You can see in this task, that my old data.frame is not only stored as a data.frame, but also with a distinct and reproducible md5 hash. This makes it incredibly easy to find stuff in a few years again and showcase that it’s the exact piece you needed.</p>
<h3>logR</h3>
<p>The <a rel="nofollow" href="https://github.com/jangorecki/logR" target="_blank">logR </a>package can be used to basically log steps of your analysis. In case you have a lot of steps in your analysis and need to know, how long these take, what was the status (error, warning) and what was the exact call, you can use logR to store everything that was done. logR therefore connects to a PostGres database and logs all steps of your analysis there. I can highly recommend to use logR in case you’re not sure if your analysis will go through running it a second time. logR will check each one of your steps, therefore any failure is stored. If your next step runs just because of any environment variable, that was set, you can definitely see this. Here is the basic example for logR from the <a rel="nofollow" href="https://github.com/jangorecki" target="_blank">author</a>:</p>
<pre>library(logR)<br><br># setup connection, default to env vars: `POSTGRES_DB`, etc.<br># if you have docker then: docker run --rm -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=postgres --name pg-logr postgres:9.5<br>logR_connect()<br># [1] TRUE<br><br># create logr table<br>logR_schema()<br><br># make some logging and calls<br><br>logR(1+2) # OK<br>#[1] 3<br>logR(log(-1)) # warning<br>#[1] NaN<br>f = function() stop(&quot;an error&quot;)<br>logR(r &lt;- f()) # stop<br>#NULL<br>g = function(n) data.frame(a=sample(letters, n, TRUE))<br>logR(df &lt;- g(4)) # out rows<br>#  a<br>#1 u<br>#2 c<br>#3 w<br>#4 p<br><br># try CTRL+C / 'stop' button to interrupt<br>logR(Sys.sleep(15))<br><br># wrapper to: dbReadTable(conn = getOption(&quot;logR.conn&quot;), name = &quot;logr&quot;)<br>logR_dump()<br>#   logr_id              logr_start          expr    status alert                logr_end      timing in_rows out_rows  mail message cond_call  cond_message<br>#1:       1 2016-02-08 16:35:00.148         1 + 2   success FALSE 2016-02-08 16:35:00.157 0.000049163      NA       NA FALSE      NA        NA            NA<br>#2:       2 2016-02-08 16:35:00.164       log(-1)   warning  TRUE 2016-02-08 16:35:00.171 0.000170801      NA       NA FALSE      NA   log(-1) NaNs produced<br>#3:       3 2016-02-08 16:35:00.180      r &lt;- f()     error  TRUE 2016-02-08 16:35:00.187 0.000136896      NA       NA FALSE      NA       f()      an error<br>#4:       4 2016-02-08 16:35:00.197    df &lt;- g(4)   success FALSE 2016-02-08 16:35:00.213 0.000696145      NA        4 FALSE      NA        NA            NA<br>#5:       5 2016-02-08 16:35:00.223 Sys.sleep(15) interrupt  TRUE 2016-02-08 16:35:05.434 5.202319000      NA       NA FALSE      NA        NA            NA</pre>
<h3><a rel="nofollow" href="https://github.com/zappingseb/RPharma2018packages#rinno" target="_blank">RInno</a> — Shiny Apps as Windows Applications</h3>
<p>We often build up shiny apps that need local PC settings to perform well. For example did we build a shiny app that accesses a MySQL database by the Active Directory login of the user. To get the Active Directory Credentials without a login window, we just ran the shiny app locally. As not all users in the department knew how to run R + runApp() , RInno sounds like a great solution for me.</p>
<figure><img alt="" src="https://i1.wp.com/cdn-images-1.medium.com/max/838/1*dwoDG0P8ESvNXyblczyWIA.png?w=456&ssl=1" data-recalc-dims="1"></figure>
<p>RInno packs your shiny app into an .exe file that your users can run directly on their PC. This would also allow them to use fancy ggplot functionalities on locally stored Excel files. This can be really important in case data is under security protection and cannot be uploaded to a server. The <a rel="nofollow" href="https://github.com/ficonsulting/RInno" target="_blank">tutorial </a>given by the developer can help you a lot to understand the issue and how to solve it.</p>
<h3>Yardstick</h3>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Gx2bExzFYDD7W5aO"><figcaption>Photo by <a rel="nofollow" href="https://unsplash.com/@sernarial?utm_source=medium&utm_medium=referral" target="_blank">patricia serna</a> on <a rel="nofollow" href="https://unsplash.com/?utm_source=medium&utm_medium=referral" target="_blank">Unsplash</a></figcaption></figure>
<p>A package containing all measures you might need to evaluate the predictive power of a statistical model. I came across this package in one of the first sessions. Whenever we think of a proper way how to measure the difference between our model and the data, we discussed a lot of different ways. Of course it’s simple to write sqrt(sum((x-y)**2)) but it looks way better in yardstick two_class_example %>% rmse(x, y) . In yardstick you know where your data is coming from and you can easily exchange the function rmse while in the example I’ve shown you need to re-code the whole functionality. Yardstick will save a lot of discussions in our team in the future. Happy it came out.</p>
<p><img width="1" src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e89639788180" height="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://medium.com/@zappingseb/interesting-packages-taken-from-r-pharma-e89639788180?source=rss-dbc9f652035a------2"> Stories by Sebastian Wolf on Medium</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/XI2Y9Rb3-gw" height="1"></a></div>