<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/a-minimal-project-tree-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://talesofr.wordpress.com/2017/12/12/a-minimal-project-tree-in-r/"> R – Tales of R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<div>
<div>
<p>You can also check this post, written in <a rel="nofollow" href="https://twitter.com/hashtag/blogdown" target="_blank">#blogdown</a>, here: <a rel="nofollow" href="https://aurora-mareviv.github.io/2017/12/minimal-project-tree-r/" target="_blank">minimal-project-tree-r</a>.</p>
<h1>Introduction</h1>
<p>The last two days arrived at my twitter feed some discussions on how bad are the following sentences at the beginning of your R script/notebook, sparked by @<a rel="nofollow" href="https://twitter.com/JennyBryan" target="_blank">JennyBryan</a>’s slides at the <a rel="nofollow" href="http://www.nzsa2017.com/" target="_blank">IASC-ARS/NZSA Conference</a>:</p>
<blockquote>
<p><code>setwd()</code></p>
</blockquote>
<p>and</p>
<blockquote>
<p><code>rm(list = ls())</code></p>
</blockquote>
<div></div>
<div>
<div lang="en">
<div>
<blockquote lang="en" class="twitter-tweet">
<p>Blog post elaborating on my advice to avoid setwd() and rm(list = ls()) at top of an <a rel="nofollow" href="http://twitter.com/search?q=%23rstats" title="#rstats" target="_blank">#rstats</a> script.<br>
Wow that reall… <a rel="nofollow" href="https://twitter.com/i/web/status/940436177219338240" target="_blank"> twitter.com/i/web/status/9…</a></p>
<p>— <br>Jenny Bryan (@JennyBryan) <a rel="nofollow" data-datetime="2017-12-12T04:20:31+00:00" href="http://twitter.com/#!/JennyBryan/status/940436177219338240" target="_blank">December 12, 2017</a></p></blockquote>
</div>
</div>
<div>
<blockquote lang="en" class="twitter-tweet">
<p>The only two things that make @<a rel="nofollow" href="https://twitter.com/JennyBryan" target="_blank">JennyBryan</a> <img alt="😤" src="https://s.w.org/images/core/emoji/2.3/72x72/1f624.png" class="wp-smiley"><img alt="😠" src="https://s.w.org/images/core/emoji/2.3/72x72/1f620.png" class="wp-smiley">🤯. Instead use projects + here::here() <a rel="nofollow" href="http://twitter.com/search?q=%23rstats" title="#rstats" target="_blank">#rstats</a> <a rel="nofollow" href="https://t.co/GwxnHePL4n" target="_blank">https://t.co/GwxnHePL4n</a></p>
<p>— <br>Hadley Wickham (@hadleywickham) <a rel="nofollow" data-datetime="2017-12-11T00:50:47+00:00" href="http://twitter.com/#!/hadleywickham/status/940021008764846080" target="_blank">December 11, 2017</a></p></blockquote>
</div>
<div>
<blockquote lang="en" class="twitter-tweet">
<p>How to ensure @<a rel="nofollow" href="https://twitter.com/JennyBryan" target="_blank">JennyBryan</a> doesn't <img alt="🔥" src="https://s.w.org/images/core/emoji/2.3/72x72/1f525.png" class="wp-smiley"> your <img alt="💻" src="https://s.w.org/images/core/emoji/2.3/72x72/1f4bb.png" class="wp-smiley">, or: &quot;Project-oriented workflow&quot; (from the arsonist herself)… <a rel="nofollow" href="https://twitter.com/i/web/status/940582516532105217" target="_blank"> twitter.com/i/web/status/9…</a></p>
<p>— <br>Mara Averick (@dataandme) <a rel="nofollow" data-datetime="2017-12-12T14:02:01+00:00" href="http://twitter.com/#!/dataandme/status/940582516532105217" target="_blank">December 12, 2017</a></p></blockquote>
</div>
<div></div>
</div>
<p> </p>
<p>Jenny Bryan offered a detailed explanation for this, as well as some fixes, in her <a rel="nofollow" href="https://www.tidyverse.org/articles/2017/12/workflow-vs-script/" target="_blank">tidyverse blog post</a>. The main idea was:</p>
<ul>
<li>To ensure reproducibility within a stable working directory tree. She proposes the very concise <code>here::here()</code> but other methods are available such as the <code>template</code> <a rel="nofollow" href="https://github.com/Pakillo/template" target="_blank">package</a>.</li>
<li>To avoid break havoc in other’s computers with <code>rm(list = ls())</code>!.</li>
</ul>
<p>All of this buzz around project self-containment and reproducibility motivated me to finish a minimal directory tree that (with some variations) I have been using for this year’s data analysis endeavours.</p>
<p>It is a extremely simple tree which separates a <code>/data</code>, a <code>/plot</code> and an <code>/img</code> directory inside the main folder (<code>root</code>)</p>
<ul>
<li>The <em>data</em> folder contains both raw data and processed data files saved by R.</li>
<li>The <em>plot</em> folder contains all the plots saved during the workflow.</li>
<li>The <em>img</em> folder has every other image (logos, etc) that R takes as an input to build the results.</li>
<li>Inside the <em>root</em> folder I store the main <code>.R</code> or <code>.Rmd</code> scripts.</li>
</ul>
<p>This ensures that every folder has an unidirectional relationship with the root folder (except the <em>data</em> dir in this case). But the important thing is that the paths in the scripts are set <em>relative</em> to the root folder, so the entire tree can be copied elsewhere and still work as expected.</p>
<p>I also added some more features to the tree:</p>
<ul>
<li>An <code>.Rproj</code> file</li>
<li>Parametrize the <code>.Rmd</code> file</li>
<li>A <span>Git</span> repository so the tree can be conveniently cloned or downloaded, with a .gitignore file:</li>
</ul>
<div>
<p><a rel="nofollow" href="https://github.com/aurora-mareviv/ProjectR" target="_blank"><code>git clone https://github.com/aurora-mareviv/ProjectR</code></a></p>
</div>
<p>Here is a sketch of how it works:</p>
<p><img src="https://i2.wp.com/aurora-mareviv.github.io/images/scheme.png?w=456&ssl=1" data-recalc-dims="1">And here is the actual code of the notebook/script. I have not included regular markdown text outside the R chunks, as this template is intended to be filled with new text each time:</p>
</div>
<div>
<h1>Script code</h1>
<pre><code><span># Installs missing libraries on render!</span>
list.<span>of</span>.packages &lt;- c(<span>&quot;rmarkdown&quot;</span>, <span>&quot;dplyr&quot;</span>, <span>&quot;ggplot2&quot;</span>, <span>&quot;Rcpp&quot;</span>, <span>&quot;knitr&quot;</span>, <span>&quot;readxl&quot;</span>)
<span>new</span>.packages &lt;- list.<span>of</span>.packages[!(list.<span>of</span>.packages %<span>in</span>% installed.packages()[,<span>&quot;Package&quot;</span>])]
<span>if</span>(length(<span>new</span>.packages)) install.packages(<span>new</span>.packages, repos=<span>'https://cran.rstudio.com/'</span>)</code></pre>
<pre><code>library(dplyr)
library(knitr)
library(ggplot2)</code></pre>
<div>
<h2>Working directories</h2>
<pre><code><span># directory where the notebook is</span>
<span>wdir</span> &lt;- getwd() 
<span># directory where data are imported from &amp; saved to</span>
datadir &lt;- paste(wdir, <span>&quot;/data&quot;</span>, sep=<span>&quot;&quot;</span>)
<span># directory where external images are imported from</span>
imgdir &lt;- paste(wdir, <span>&quot;/img&quot;</span>, sep=<span>&quot;&quot;</span>)
<span># directory where plots are saved to</span>
plotdir &lt;- paste(wdir, <span>&quot;/plot&quot;</span>, sep=<span>&quot;&quot;</span>)
<span># y la carpeta inmediatamente por encima</span>
wdirUp &lt;- gsub(<span>&quot;\\ProjectR&quot;</span>, <span>&quot;&quot;</span>, wdir) </code></pre>
</div>
<div>
<h2>Data import</h2>
<pre><code><span># Data name (stored as a parameter in the Rmarkdown notebook)</span>
<span>params</span> &lt;- NULL
params<span>$dataname</span> &lt;- <span>&quot;cars&quot;</span></code></pre>
<pre><code><span># MSEXCEL</span>
<span>dataname</span> &lt;- params<span>$dataname</span> <span># archive name</span>
routexl &lt;- paste(datadir, <span>&quot;/&quot;</span>, dataname, <span>&quot;.xlsx&quot;</span>, sep=<span>&quot;&quot;</span>)  <span># complete route to archive</span>

library(readxl)
mydata &lt;- read_excel(routexl, sheet = <span>1</span>)  <span># imports first sheet</span></code></pre>
<pre><code><span># CSV / TSV (separated by tabs in this example)</span>
<span>dataname</span> &lt;- params<span>$dataname</span> <span># archive name</span>
routecsv &lt;- paste(datadir, <span>&quot;/&quot;</span>, dataname, <span>&quot;.csv&quot;</span>, sep=<span>&quot;&quot;</span>)  <span># complete route to archive</span>

mydata &lt;- read.csv(paste(routecsv, sep=<span>&quot;&quot;</span>), 
         header = TRUE, 
         sep = <span>&quot;\t&quot;</span>,
         dec = <span>&quot;,&quot;</span>)</code></pre>
</div>
<div>
<h2>Data operations</h2>
<pre><code>head(mydata)</code></pre>
<pre><code>     <span>speed</span> dist
   <span>1</span>     <span>4</span>    <span>2</span>
   <span>2</span>     <span>4</span>   <span>10</span>
   <span>3</span>     <span>7</span>    <span>4</span>
   <span>4</span>     <span>7</span>   <span>22</span>
   <span>5</span>     <span>8</span>   <span>16</span>
   <span>6</span>     <span>9</span>   <span>10</span></code></pre>
<pre><code>p1 <span><span>-</span> <span>ggplot</span>(<span>cars</span>, <span>aes</span>(<span>x</span>=<span>speed,</span> <span>y</span>=<span>dist))</span> + <span>geom_point</span>()
<span>p1</span></span></code></pre>
<p><img src="https://i2.wp.com/aurora-mareviv.github.io/post/2017-12-12-a-minimal-project-tree-in-r_files/figure-html/unnamed-chunk-9-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>Save plots</h2>
<pre><code><span># TO PDF </span>
<span>plotname1</span> &lt;- <span>&quot;p1.pdf&quot;</span>
<span># TO PNG </span>
plotname2 &lt;- <span>&quot;p1.png&quot;</span>

routeplot1 &lt;- paste(plotdir, <span>&quot;/&quot;</span>, plotname1, sep=<span>&quot;&quot;</span>)
routeplot2 &lt;- paste(plotdir, <span>&quot;/&quot;</span>, plotname2, sep=<span>&quot;&quot;</span>)
ggsave(routeplot1)  <span># (see http://ggplot2.tidyverse.org/reference/ggsave.html)</span>
ggsave(routeplot2) </code></pre>
</div>
<div>
<h2>Save data</h2>
<pre><code><span># RDATA</span>
<span>save</span>(mydata, file=<span>&quot;data/mydata.RData&quot;</span>)</code></pre>
<pre><code><span># MSEXCEL # not run</span>
<span>dataname2</span> &lt;- <span>&quot;mydata&quot;</span>  <span># name we will give to file</span>
routexl2 &lt;- paste(datadir, <span>&quot;/&quot;</span>, dataname2, <span>&quot;.xlsx&quot;</span>, sep=<span>&quot;&quot;</span>)   <span># complete route to future archive</span>

library(xlsx)
write.xlsx(mydata, routexl2) <span># creates archive in specified route</span></code></pre>
<pre><code><span># CSV / TSV (separated by tabs in this example)</span>
dataname2 &lt;- <span>&quot;mydata&quot;</span>  <span># name we will give to file</span>
routecsv2 &lt;- paste(datadir, <span>&quot;/&quot;</span>, dataname2, <span>&quot;.csv&quot;</span>, sep=<span>&quot;&quot;</span>)  <span># complete route to future archive</span>

write.table(mydata, file = routecsv2, append = <span>FALSE</span>, quote = <span>FALSE</span>, sep = <span>&quot;\t &quot;</span>,
            eol = <span>&quot;\n&quot;</span>, na = <span>&quot;NA&quot;</span>, dec = <span>&quot;.&quot;</span>, row.names = <span>FALSE</span>,
            col.names = <span>TRUE</span>)</code></pre>
<p>This script -and the dir tree that contains it- is saving me a lot of time and headaches (where I’ve put that data?….), I hope it can be also useful for people out there!.</p>
</div>
</div>
<div>
<h1>Future improvements</h1>
<ul>
<li>Add a <em>Makefile</em>?</li>
<li>Use <a rel="nofollow" href="https://travis-ci.org/" target="_blank">Travis.ci</a>?</li>
<li>Etc…</li>
</ul>
</div>
</div>
</div>
<div></div>
<p> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/talesofr.wordpress.com/926/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/talesofr.wordpress.com/926/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="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://talesofr.wordpress.com/2017/12/12/a-minimal-project-tree-in-r/"> R – Tales of R</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/jthKEv5mPmw" height="1"></a></div>