<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/rstudioaddins-part-1-code-reproducibility-testing/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/"> Jozef's Rblog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h1>Contents</h1>
<ol>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#introduction" target="_blank">Introduction</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#prerequisites-and-recommendations" target="_blank">Prerequisites and recommendations</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#step-1---creating-a-package" target="_blank">Step 1 – Creating a package</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#step-2---writing-the-first-functions" target="_blank">Step 2 – Writing the first functions</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#step-3---setting-up-an-addin" target="_blank">Step 3 – Setting up an addin</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#step-4---updating-our-description-and-namespace" target="_blank">Step 4 – Updating our <code>DESCRIPTION</code> and <code>NAMESPACE</code></a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#what-is-next---always-paying-our-technical-debts" target="_blank">What is next – Always paying our (technical) debts</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#wrapping-up" target="_blank">Wrapping up</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#tldr---just-give-me-the-package" target="_blank">TL;DR – Just give me the package</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/#references" target="_blank">References</a></li>
</ol>
</div>
<div>
<h1>Introduction</h1>
<p>This is the first post in the RStudio:addins series. The aim of the series is to walk the readers through creating an R package that will contain functionality for integrating useful addins into the RStudio IDE. At the end of this first article, your RStudio will be 1 useful addin richer.</p>
<blockquote>
<h4>The addin we will create in this article will let us run a script open in RStudio in R vanilla mode via a keyboard shortcut and open a file with the script’s output in RStudio.</h4>
</blockquote>
<p>This is useful for testing whether your script is reproducible by users that do not have the same start-up options as you (e.g. preloaded environment, site file, etc.), making it a good tool to test your scripts before sharing them.</p>
<p>If you want to get straight to the code, you can find it at <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins.git" target="_blank"><code>https://gitlab.com/jozefhajnala/jhaddins.git</code></a></p>
</div>
<div>
<h1>Prerequisites and recommendations</h1>
<p>To make the most use of the series, you will need the following:</p>
<ol>
<li><a rel="nofollow" href="http://www.cran.r-project.org/" target="_blank">R</a>, ideally version 3.4.3 or more recent, 64bit</li>
<li><a rel="nofollow" href="https://www.rstudio.com/products/RStudio/" target="_blank">RStudio</a> IDE, ideally version 1.1.383 or more recent</li>
<li>Also recommended</li>
</ol>
<ul>
<li><a rel="nofollow" href="https://git-scm.com/downloads" target="_blank">git</a>, for version control</li>
<li><a rel="nofollow" href="https://tortoisegit.org/" target="_blank">TortoiseGit</a>, convenient shell interface to git for those using Windows, with pretty icons and all</li>
</ul>
<ol start="4">
<li>Recommended R packages (install with <code>install.packages(&quot;packagename&quot;)</code>, or via RStudio’s Packages tab):</li>
</ol>
<ul>
<li><code>devtools</code> – makes your development life easier</li>
<li><code>testthat</code> – provides a framework for unit testing integrated into RStudio</li>
<li><code>roxygen2</code> – makes code documentation easy</li>
</ul>
</div>
<div>
<h1>Step 1 – Creating a package</h1>
<ol>
<li>Use <code>devtools::create</code> to create a package (note that we will update more <code>DESCRIPTION</code> fields later and you can also choose any path you like and it will be reflected in the name of the package)</li>
</ol>
<pre><code>devtools::create(
  path = &quot;jhaddins&quot;
, description = list(&quot;License&quot; = &quot;GPL-3&quot;)
)</code></pre>
<ol start="2">
<li>
<p>In RStudio or elsewhere navigate to the <code>jhaddins</code> folder and open the project <code>jhaddins.Rproj</code> (or the name of your project if you chose a different path)</p>
</li>
<li>
<p>Run the first check and install the package</p>
</li>
</ol>
<pre><code>devtools::check()   # Ctrl+Shift+E or Check button on RStudio's build tab
devtools::install() # Ctrl+Shift+B or Install button on RStudio's build tab</code></pre>
<ol start="4">
<li>Optionally, initialize git for version control</li>
</ol>
<pre><code>devtools::use_git()</code></pre>
</div>
<div>
<h1>Step 2 – Writing the first functions</h1>
<p>We will now write some functions into a file called <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/master/R/makeCmd.R" target="_blank">makeCmd.R</a> that will let us run the desired functionality:</p>
<ol>
<li><code>makeCmd</code> to create a command executable via <code>system</code> or <code>shell</code>, with defaults set up for executing an R file specified by <code>path</code></li>
</ol>
<pre><code>makeCmd &lt;- function(path
                  , command = &quot;Rscript&quot;
                  , opts = &quot;--vanilla&quot;
                  , outputFile = NULL
                  , suffix = NULL
                  , addRhome = TRUE) {
  if (Sys.info()[&quot;sysname&quot;] == &quot;Windows&quot;) {
    qType &lt;- &quot;cmd2&quot;
  } else {
    qType &lt;- &quot;sh&quot;
  }
  if (isTRUE(addRhome)) {
    command &lt;- file.path(R.home(&quot;bin&quot;), command)
  }
  cmd &lt;- paste(
    shQuote(command, type = qType)
  , shQuote(opts, type = qType)
  , shQuote(path, type = qType)
  )
  if (!is.null(outputFile)) {
    cmd &lt;- paste(cmd, &quot;>&quot;, shQuote(outputFile))
  }
  if (!is.null(suffix)) {
    cmd &lt;- paste(cmd, suffix)
  }
  cmd
}</code></pre>
<ol start="2">
<li><code>executeCmd</code> to execute a command</li>
</ol>
<pre><code>executeCmd &lt;- function(cmd, intern = FALSE) {
  sysName &lt;- Sys.info()[&quot;sysname&quot;]
  stopifnot(
    is.character(cmd)
  , length(cmd) == 1
  , sysName %in% c(&quot;Windows&quot;, &quot;Linux&quot;)
  )

  if (sysName == &quot;Windows&quot;) {
    shell(cmd, intern = intern)
  } else {
    system(cmd, intern = intern)
  }
}</code></pre>
<ol start="3">
<li><code>replaceTilde</code> for Linux purposes</li>
</ol>
<pre><code>replaceTilde &lt;- function(path) {
  if (substr(path, 1, 1) == &quot;~&quot;) {
    path &lt;- sub(&quot;~&quot;, Sys.getenv(&quot;HOME&quot;), path, fixed = TRUE)
  }
  file.path(path)
}</code></pre>
<ol start="4">
<li>And finally the function which will be used for the addin execution – <code>runCurrentRscript</code> to retrieve the path to the currently active file in RStudio, run it, write the output to a file <code>output.txt</code> and open the file with output.</li>
</ol>
<pre><code>runCurrentRscript &lt;- function(
  path = replaceTilde(rstudioapi::getActiveDocumentContext()[[&quot;path&quot;]])
, outputFile = &quot;output.txt&quot;) {
  cmd &lt;- makeCmd(path, outputFile = outputFile)
  executeCmd(cmd)
  if (!is.null(outputFile) &amp;&amp; file.exists(outputFile)) {
    rstudioapi::navigateToFile(outputFile)
  }
}</code></pre>
</div>
<div>
<h1>Step 3 – Setting up an addin</h1>
<p>Now that we have all our functions ready, all we have to do is create a file <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/master/inst/rstudio/addins.dcf" target="_blank">addins.dcf</a> under the <code>\inst\rstudio</code> folder of our package. We specify the <code>Name</code> of the addin, write a nice <code>Description</code> of what it does and most importantly specify the <code>Binding</code> to the function we want to call:</p>
<div>
<img alt="creating addins.dcf under inst/rstudio" src="https://i2.wp.com/jozefhajnala.gitlab.io/r/img/r101-addin-01.gif?w=456&ssl=1" data-recalc-dims="1">
<p>creating addins.dcf under inst/rstudio</p>
</div>
<pre><code>Name: runCurrentRscript
Description: Executes the currently open R script file via Rscript with --vanilla option
Binding: runCurrentRscript
Interactive: false</code></pre>
<p>Now we can rebuild and install our package and in RStudio’s menu navigate to <code>Tools -> Addins -> Browse Addins...</code>, and there it is – our first addin. For the best experience, we can click the <code>Keyboard Shortcuts...</code> button and assign a keyboard shortcut to our addin for easy use.</p>
<div>
<img alt="setting an RStudio addin keyboard shortcut" src="https://i0.wp.com/jozefhajnala.gitlab.io/r/img/r101-addin-02.gif?w=456&ssl=1" data-recalc-dims="1">
<p>setting an RStudio addin keyboard shortcut</p>
</div>
<p>Now just open an R script, hit our shortcut and voilà, our script gets execute via RScript in vanilla mode.</p>
</div>
<div>
<h1>Step 4 – Updating our <code>DESCRIPTION</code> and <code>NAMESPACE</code></h1>
<p>As our last steps, we should</p>
<ol>
<li>Update our <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/master/DESCRIPTION" target="_blank">DESCRIPTION</a> file with <code>rstudioapi</code> as <code>Imports</code>, as we will be needing it before using our package:</li>
</ol>
<pre><code>Package: jhaddins
Title: JH's RStudio Addins
Version: 0.0.0.9000
Authors@R: person(&quot;Jozef&quot;, &quot;Hajnala&quot;, email = &quot;jozef.hajnala@gmail.com&quot;, role = c(&quot;aut&quot;, &quot;cre&quot;))
Description: Useful addins to make RStudio even better.
Depends: R (>= 3.0.1)
Imports: rstudioapi (>= 0.7)
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1</code></pre>
<ol start="2">
<li>Update our <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/master/NAMESPACE" target="_blank">NAMESPACE</a> by importing the functions from other packages that we are using, namely:</li>
</ol>
<pre><code>importFrom(rstudioapi, navigateToFile)
importFrom(rstudioapi, getActiveDocumentContext)</code></pre>
<p>Now we can finally rebuild and install our package again and run a <code>CHECK</code> to see that we have no errors, warnings and notes telling us something is wrong. Make sure to use the <code>document = FALSE</code> for now.</p>
<pre><code>devtools::install() # Ctrl+Shift+B or Install button on RStudio's build tab
devtools::check(document = FALSE)   # Ctrl+Shift+E or Check button on RStudio's build tab</code></pre>
</div>
<div>
<h1>What is next – Always paying our (technical) debts</h1>
<p>In the next post of the series, we will pay our debt of</p>
<ul>
<li>missing documentation for our functions, that will help us to generate updates to our <code>NAMESPACE</code> automatically and help us get a nice documentation so that we can read about our functions using <code>?</code></li>
<li>and unit tests to help us sleep better knowing that our functions get tested!</li>
</ul>
</div>
<div>
<h1>Wrapping up</h1>
<p>We can quickly create an RStudio addin by:</p>
<ol>
<li>Creating an R package</li>
<li>Writing a function in that package</li>
<li>Creating a <code>addins.dcf</code> in <code>\inst\rstudio</code> folder of our package</li>
</ol>
</div>
<div>
<h1>TL;DR – Just give me the package</h1>
<p>Use git clone from: <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins.git" target="_blank"><code>https://gitlab.com/jozefhajnala/jhaddins.git</code></a></p>
</div>
<div>
<h1>References</h1>
<ul>
<li><a rel="nofollow" href="https://raw.githubusercontent.com/rstudio/cheatsheets/master/rstudio-ide.pdf" target="_blank">RStudio IDE cheat sheet</a> (4.4MB, pdf)</li>
<li><a rel="nofollow" href="https://rviews.rstudio.com/2016/11/11/easy-tricks-you-mightve-missed/" target="_blank">RStudio IDE tricks</a> you might have missed</li>
<li><a rel="nofollow" href="https://www.rstudio.com/resources/webinars/understanding-add-ins/" target="_blank">Understanding Addins</a> – A fantastic webinar, where you can learn how to write and setup addins step-by-step</li>
</ul>
</div>
	<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://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/"> Jozef's Rblog</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/bGgBTSkJSg0" height="1"></a></div>