EVOLUTION-MANAGER
Edit File: rprojroot.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="generator" content="pandoc" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="author" content="Kirill Müller" /> <meta name="date" content="2018-01-03" /> <title>Finding files in project subdirectories</title> <style type="text/css">code{white-space: pre;}</style> <style type="text/css"> div.sourceCode { overflow-x: auto; } table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode { margin: 0; padding: 0; vertical-align: baseline; border: none; } table.sourceCode { width: 100%; line-height: 100%; } td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; } td.sourceCode { padding-left: 5px; } code > span.kw { color: #007020; font-weight: bold; } /* Keyword */ code > span.dt { color: #902000; } /* DataType */ code > span.dv { color: #40a070; } /* DecVal */ code > span.bn { color: #40a070; } /* BaseN */ code > span.fl { color: #40a070; } /* Float */ code > span.ch { color: #4070a0; } /* Char */ code > span.st { color: #4070a0; } /* String */ code > span.co { color: #60a0b0; font-style: italic; } /* Comment */ code > span.ot { color: #007020; } /* Other */ code > span.al { color: #ff0000; font-weight: bold; } /* Alert */ code > span.fu { color: #06287e; } /* Function */ code > span.er { color: #ff0000; font-weight: bold; } /* Error */ code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */ code > span.cn { color: #880000; } /* Constant */ code > span.sc { color: #4070a0; } /* SpecialChar */ code > span.vs { color: #4070a0; } /* VerbatimString */ code > span.ss { color: #bb6688; } /* SpecialString */ code > span.im { } /* Import */ code > span.va { color: #19177c; } /* Variable */ code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */ code > span.op { color: #666666; } /* Operator */ code > span.bu { } /* BuiltIn */ code > span.ex { } /* Extension */ code > span.pp { color: #bc7a00; } /* Preprocessor */ code > span.at { color: #7d9029; } /* Attribute */ code > span.do { color: #ba2121; font-style: italic; } /* Documentation */ code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */ code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */ code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */ </style> <link href="data:text/css;charset=utf-8,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23header%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0Aa%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%20code%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%0A" rel="stylesheet" type="text/css" /> </head> <body> <h1 class="title toc-ignore">Finding files in project subdirectories</h1> <h4 class="author"><em>Kirill Müller</em></h4> <h4 class="date"><em>2018-01-03</em></h4> <p>The <em>rprojroot</em> package solves a seemingly trivial but annoying problem that occurs sooner or later in any largish project: How to find files in subdirectories? Ideally, file paths are relative to the <em>project root</em>.</p> <p>Unfortunately, we cannot always be sure about the current working directory: For instance, in RStudio it’s sometimes:</p> <ul> <li>the project root (when running R scripts),</li> <li>a subdirectory (when building vignettes),</li> <li>again the project root (when executing chunks of a vignette).</li> </ul> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">basename</span>(<span class="kw">getwd</span>())</code></pre></div> <pre><code>## [1] "vignettes"</code></pre> <p>In some cases, it’s even outside the project root.</p> <p>This vignette starts with a very brief summary that helps you get started, followed by a longer description of the features.</p> <div id="tldr" class="section level2"> <h2>TL;DR</h2> <p>What is your project: An R package?</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">rprojroot<span class="op">::</span>is_r_package</code></pre></div> <pre><code>## Root criterion: contains a file `DESCRIPTION` with contents matching `^Package: `</code></pre> <p>Or an RStudio project?</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">rprojroot<span class="op">::</span>is_rstudio_project</code></pre></div> <pre><code>## Root criterion: contains a file matching `[.]Rproj$` with contents matching `^Version: ` in the first line</code></pre> <p>Or something else?</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">rprojroot<span class="op">::</span><span class="kw">has_file</span>(<span class="st">".git/index"</span>)</code></pre></div> <pre><code>## Root criterion: contains a file `.git/index`</code></pre> <p>For now, we assume it’s an R package:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">root <-<span class="st"> </span>rprojroot<span class="op">::</span>is_r_package</code></pre></div> <p>The <code>root</code> object contains a function that helps locating files below the root of your package, regardless of your current working directory. If you are sure that your working directory is somewhere below your project’s root, use the <code>root$find_file()</code> function:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">readLines</span>(root<span class="op">$</span><span class="kw">find_file</span>(<span class="st">"DESCRIPTION"</span>), <span class="dv">3</span>)</code></pre></div> <pre><code>## [1] "Package: rprojroot" ## [2] "Title: Finding Files in Project Subdirectories" ## [3] "Version: 1.3-2"</code></pre> <p>You can also construct an accessor to your root using the <code>root$make_fix_file()</code> function:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">root_file <-<span class="st"> </span>root<span class="op">$</span><span class="kw">make_fix_file</span>()</code></pre></div> <p>Note that <code>root_file()</code> is a <em>function</em> that works just like <code>$find_file()</code> but will find the files even if the current working directory is outside your project:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">withr<span class="op">::</span><span class="kw">with_dir</span>( <span class="st">"../.."</span>, <span class="kw">readLines</span>(<span class="kw">root_file</span>(<span class="st">"DESCRIPTION"</span>), <span class="dv">3</span>) )</code></pre></div> <pre><code>## [1] "Package: rprojroot" ## [2] "Title: Finding Files in Project Subdirectories" ## [3] "Version: 1.3-2"</code></pre> <p>If you know the absolute path of some directory below your project, but cannot be sure of your current working directory, pass that absolute path to <code>root$make_fix_file()</code>:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">root_file <-<span class="st"> </span>root<span class="op">$</span><span class="kw">make_fix_file</span>(<span class="st">"C:</span><span class="ch">\\</span><span class="st">Users</span><span class="ch">\\</span><span class="st">User Name</span><span class="ch">\\</span><span class="st">..."</span>)</code></pre></div> <p>As a last resort, you can get the path of standalone R scripts or vignettes using the <code>thisfile()</code> function:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">root_file <-<span class="st"> </span>root<span class="op">$</span><span class="kw">make_fix_file</span>(<span class="kw">dirname</span>(<span class="kw">thisfile</span>()))</code></pre></div> <p>The remainder of this vignette describes implementation details and advanced features.</p> </div> <div id="project-root" class="section level2"> <h2>Project root</h2> <p>We assume a self-contained project where all files and directories are located below a common <em>root</em> directory. Also, there should be a way to unambiguously identify this root directory. (Often, the root contains a regular file whose name matches a given pattern, and/or whose contents match another pattern.) In this case, the following method reliably finds our project root:</p> <ul> <li>Start the search in any subdirectory of our project</li> <li>Proceed up the directory hierarchy until the root directory has been identified</li> </ul> <p>The Git version control system (and probably many other tools) use a similar approach: A Git command can be executed from within any subdirectory of a repository.</p> <div id="a-simple-example" class="section level3"> <h3>A simple example</h3> <p>The <code>find_root()</code> function implements the core functionality. It returns the path to the first directory that matches the filtering criteria, or throws an error if there is no such directory. Filtering criteria are constructed in a generic fashion using the <code>root_criterion()</code> function, the <code>has_file()</code> function constructs a criterion that checks for the presence of a file with a specific name and specific contents.</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(rprojroot) <span class="co"># List all files and directories below the root</span> <span class="kw">dir</span>(<span class="kw">find_root</span>(<span class="kw">has_file</span>(<span class="st">"DESCRIPTION"</span>)))</code></pre></div> <pre><code>## [1] "API" "DESCRIPTION" "Makefile" ## [4] "NAMESPACE" "NEWS.md" "R" ## [7] "README.md" "_pkgdown.yml" "appveyor.yml" ## [10] "cran-comments.md" "docs" "inst" ## [13] "man" "readme" "revdep" ## [16] "rprojroot.Rcheck" "rprojroot.Rproj" "tests" ## [19] "tic.R" "vignettes"</code></pre> <div id="relative-paths-to-a-stable-root" class="section level4"> <h4>Relative paths to a stable root</h4> <p>Here we illustrate the power of <em>rprojroot</em> by demonstrating how to access the same file from two different working directories. Let your project be a package called <code>pkgname</code> and consider the desired file <code>rrmake.R</code> at <code>pkgname/R/rrmake.R</code>. First, we show how to access from the <code>vignettes</code> directory, and then from the <code>tests/testthat</code> directory.</p> <div id="example-a-from-vignettes" class="section level5"> <h5>Example A: From <code>vignettes</code></h5> <p>When your working directory is <code>pkgname/vignettes</code>, you can access the <code>rrmake.R</code> file by:</p> <ol style="list-style-type: decimal"> <li>Supplying a pathname relative to your working directory. Here’s two ways to do that:</li> </ol> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">rel_path_from_vignettes <-<span class="st"> "../R/rrmake.R"</span> rel_path_from_vignettes <-<span class="st"> </span><span class="kw">file.path</span>(<span class="st">".."</span>, <span class="st">"R"</span>, <span class="st">"rrmake.R"</span>) ##identical</code></pre></div> <ol start="2" style="list-style-type: decimal"> <li>Supplying a pathname to the file relative from the root of the package, e.g.,</li> </ol> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">rel_path_from_root <-<span class="st"> "R/rrmake.R"</span> rel_path_from_root <-<span class="st"> </span><span class="kw">file.path</span>(<span class="st">"R"</span>, <span class="st">"rrmake.R"</span>) ##identical</code></pre></div> <p>This second method requires finding the root of the package, which can be done with the <code>has_file()</code> function:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">has_file</span>(<span class="st">"DESCRIPTION"</span>)</code></pre></div> <pre><code>## Root criterion: contains a file `DESCRIPTION`</code></pre> <p>So, using <em>rprojroot</em> you can specify the path relative from root in the following manner:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Specify a path/to/file relative to the root</span> rel_path_from_root <-<span class="st"> </span><span class="kw">find_root_file</span>(<span class="st">"R"</span>, <span class="st">"rrmake.R"</span>, <span class="dt">criterion =</span> <span class="kw">has_file</span>(<span class="st">"DESCRIPTION"</span>))</code></pre></div> </div> <div id="example-b-from-teststestthat" class="section level5"> <h5>Example B: From <code>tests/testthat</code></h5> <p>When your working directory is <code>pkgname/tests/testthat</code>, you can access the <code>rrmake.R</code> file by:</p> <ol style="list-style-type: decimal"> <li>Supplying a pathname relative to your working directory.</li> </ol> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">rel_path_from_testthat <-<span class="st"> "../../R/rrmake.R"</span></code></pre></div> <p>Note that this is different than in the previous example! However, the second method is the same…</p> <ol start="2" style="list-style-type: decimal"> <li>Supplying a pathname to the file relative from the root of the package. With <em>rprojroot</em>, this is the exact same as in the previous example.</li> </ol> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Specify a path/to/file relative to the root</span> rel_path_from_root <-<span class="st"> </span><span class="kw">find_root_file</span>(<span class="st">"R"</span>, <span class="st">"rrmake.R"</span>, <span class="dt">criterion =</span> <span class="kw">has_file</span>(<span class="st">"DESCRIPTION"</span>))</code></pre></div> </div> <div id="summary-of-examples-a-and-b" class="section level5"> <h5>Summary of Examples A and B</h5> <p>Since Examples A and B used different working directories, <code>rel_path_from_vignettes</code> and <code>rel_path_from_testthat</code> were different. This is an issue when trying to re-use the same code. This issue is solved by using <em>rprojroot</em>: the function <code>find_root_file()</code> finds a file relative from the root, where the root is determined from checking the criterion with <code>has_file()</code>.</p> <p>Note that the follow code produces identical results when building the vignette <em>and</em> when sourcing the chunk in RStudio, provided that the current working directory is the project root or anywhere below. So, we can check to make sure that <em>rprojroot</em> has succesfully determined the correct path:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Specify a path/to/file relative to the root</span> rel_path_from_root <-<span class="st"> </span><span class="kw">find_root_file</span>(<span class="st">"R"</span>, <span class="st">"rrmake.R"</span>, <span class="dt">criterion =</span> <span class="kw">has_file</span>(<span class="st">"DESCRIPTION"</span>)) <span class="co"># Find a file relative to the root</span> <span class="kw">file.exists</span>(rel_path_from_root)</code></pre></div> <pre><code>## [1] TRUE</code></pre> </div> </div> </div> <div id="criteria" class="section level3"> <h3>Criteria</h3> <p>The <code>has_file()</code> function (and the more general <code>root_criterion()</code>) both return an S3 object of class <code>root_criterion</code>:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">has_file</span>(<span class="st">"DESCRIPTION"</span>)</code></pre></div> <pre><code>## Root criterion: contains a file `DESCRIPTION`</code></pre> <p>In addition, character values are coerced to <code>has_file</code> criteria by default, this coercion is applied automatically by <code>find_root()</code>. (This feature is used by the introductory example.)</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">as.root_criterion</span>(<span class="st">"DESCRIPTION"</span>)</code></pre></div> <pre><code>## Root criterion: contains a file `DESCRIPTION`</code></pre> <p>The return value of these functions can be stored and reused; in fact, the package provides 9 such criteria:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">criteria</code></pre></div> <pre><code>## $is_rstudio_project ## Root criterion: contains a file matching `[.]Rproj$` with contents matching `^Version: ` in the first line ## ## $is_r_package ## Root criterion: contains a file `DESCRIPTION` with contents matching `^Package: ` ## ## $is_remake_project ## Root criterion: contains a file `remake.yml` ## ## $is_projectile_project ## Root criterion: contains a file `.projectile` ## ## $is_git_root ## Root criterion: one of ## - contains a directory `.git` ## - contains a file `.git` with contents matching `^gitdir: ` ## ## $is_svn_root ## Root criterion: contains a directory `.svn` ## ## $is_vcs_root ## Root criterion: one of ## - contains a directory `.git` ## - contains a file `.git` with contents matching `^gitdir: ` ## - contains a directory `.svn` ## ## $is_testthat ## Root criterion: directory name is `testthat` (also look in subdirectories: `tests/testthat`, `testthat`) ## ## $from_wd ## Root criterion: from current working directory ## ## attr(,"class") ## [1] "root_criteria"</code></pre> <p>Defining new criteria is easy:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">has_license <-<span class="st"> </span><span class="kw">has_file</span>(<span class="st">"LICENSE"</span>) has_license</code></pre></div> <pre><code>## Root criterion: contains a file `LICENSE`</code></pre> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">is_projecttemplate_project <-<span class="st"> </span><span class="kw">has_file</span>(<span class="st">"config/global.dcf"</span>, <span class="st">"^version: "</span>) is_projecttemplate_project</code></pre></div> <pre><code>## Root criterion: contains a file `config/global.dcf` with contents matching `^version: `</code></pre> <p>You can also combine criteria via the <code>|</code> operator:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">is_r_package <span class="op">|</span><span class="st"> </span>is_rstudio_project</code></pre></div> <pre><code>## Root criterion: one of ## - contains a file `DESCRIPTION` with contents matching `^Package: ` ## - contains a file matching `[.]Rproj$` with contents matching `^Version: ` in the first line</code></pre> </div> <div id="shortcuts" class="section level3"> <h3>Shortcuts</h3> <p>To avoid specifying the search criteria for the project root every time, shortcut functions can be created. The <code>find_package_root_file()</code> is a shortcut for <code>find_root_file(..., criterion = is_r_package)</code>:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Print first lines of the source for this document</span> <span class="kw">head</span>(<span class="kw">readLines</span>(<span class="kw">find_package_root_file</span>(<span class="st">"vignettes"</span>, <span class="st">"rprojroot.Rmd"</span>)))</code></pre></div> <pre><code>## [1] "---" ## [2] "title: \"Finding files in project subdirectories\"" ## [3] "author: \"Kirill Müller\"" ## [4] "date: \"`r Sys.Date()`\"" ## [5] "output: rmarkdown::html_vignette" ## [6] "vignette: >"</code></pre> <p>To save typing effort, define a shorter alias:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">P <-<span class="st"> </span>find_package_root_file <span class="co"># Use a shorter alias</span> <span class="kw">file.exists</span>(<span class="kw">P</span>(<span class="st">"vignettes"</span>, <span class="st">"rprojroot.Rmd"</span>))</code></pre></div> <pre><code>## [1] TRUE</code></pre> <p>Each criterion actually contains a function that allows finding a file below the root specified by this criterion. As our project does not have a file named <code>LICENSE</code>, querying the root results in an error:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Use the has_license criterion to find the root</span> R <-<span class="st"> </span>has_license<span class="op">$</span>find_file R</code></pre></div> <pre><code>## function (..., path = ".") ## { ## find_root_file(..., criterion = criterion, path = path) ## } ## <environment: 0x55721401cd30></code></pre> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Our package does not have a LICENSE file, trying to find the root results in an error</span> <span class="kw">R</span>()</code></pre></div> <pre><code>## Error: No root directory found in /tmp/RtmpIOVFMP/Rbuild4ab6ad646f7/rprojroot/vignettes or its parent directories. Root criterion: contains a file `LICENSE`</code></pre> </div> <div id="fixed-root" class="section level3"> <h3>Fixed root</h3> <p>We can also create a function that computes a path relative to the root <em>at creation time</em>.</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Define a function that computes file paths below the current root</span> F <-<span class="st"> </span>is_r_package<span class="op">$</span><span class="kw">make_fix_file</span>() F</code></pre></div> <pre><code>## function (...) ## { ## file.path("/tmp/RtmpIOVFMP/Rbuild4ab6ad646f7/rprojroot", ## ...) ## } ## <environment: 0x5572140051a8></code></pre> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Show contents of the NAMESPACE file in our project</span> <span class="kw">readLines</span>(<span class="kw">F</span>(<span class="st">"NAMESPACE"</span>))</code></pre></div> <pre><code>## [1] "# Generated by roxygen2: do not edit by hand" ## [2] "" ## [3] "S3method(\"|\",root_criterion)" ## [4] "S3method(as.root_criterion,character)" ## [5] "S3method(as.root_criterion,default)" ## [6] "S3method(as.root_criterion,root_criterion)" ## [7] "S3method(format,root_criterion)" ## [8] "S3method(print,root_criterion)" ## [9] "S3method(str,root_criteria)" ## [10] "export(as.root_criterion)" ## [11] "export(criteria)" ## [12] "export(find_package_root_file)" ## [13] "export(find_remake_root_file)" ## [14] "export(find_root)" ## [15] "export(find_root_file)" ## [16] "export(find_rstudio_root_file)" ## [17] "export(find_testthat_root_file)" ## [18] "export(from_wd)" ## [19] "export(get_root_desc)" ## [20] "export(has_dir)" ## [21] "export(has_dirname)" ## [22] "export(has_file)" ## [23] "export(has_file_pattern)" ## [24] "export(is.root_criterion)" ## [25] "export(is_git_root)" ## [26] "export(is_projectile_project)" ## [27] "export(is_r_package)" ## [28] "export(is_remake_project)" ## [29] "export(is_rstudio_project)" ## [30] "export(is_svn_root)" ## [31] "export(is_testthat)" ## [32] "export(is_vcs_root)" ## [33] "export(root_criterion)" ## [34] "export(thisfile)" ## [35] "export(thisfile_knit)" ## [36] "export(thisfile_r)" ## [37] "export(thisfile_rscript)" ## [38] "export(thisfile_source)" ## [39] "import(backports)" ## [40] "importFrom(utils,str)" ## [41] "importFrom(utils,tail)"</code></pre> <p>This is a more robust alternative to <code>$find_file()</code>, because it <em>fixes</em> the project directory when <code>$make_fix_file()</code> is called, instead of searching for it every time. (For that reason it is also slightly faster, but I doubt this matters in practice.)</p> <p>This function can be used even if we later change the working directory to somewhere outside the project:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Print the size of the namespace file, working directory outside the project</span> withr<span class="op">::</span><span class="kw">with_dir</span>( <span class="st">"../.."</span>, <span class="kw">file.size</span>(<span class="kw">F</span>(<span class="st">"NAMESPACE"</span>)) )</code></pre></div> <pre><code>## [1] 1010</code></pre> <p>The <code>make_fix_file()</code> member function also accepts an optional <code>path</code> argument, in case you know your project’s root but the current working directory is somewhere outside. The path to the current script or <code>knitr</code> document can be obtained using the <code>thisfile()</code> function, but it’s much easier and much more robust to just run your scripts with the working directory somewhere below your project root.</p> </div> </div> <div id="testthat-files" class="section level2"> <h2><code>testthat</code> files</h2> <p>Tests run with <a href="https://cran.r-project.org/package=testthat"><code>testthat</code></a> commonly use files that live below the <code>tests/testthat</code> directory. Ideally, this should work in the following situation:</p> <ul> <li>During package development (working directory: package root)</li> <li>When testing with <code>devtools::test()</code> (working directory: <code>tests/testthat</code>)</li> <li>When running <code>R CMD check</code> (working directory: a renamed recursive copy of <code>tests</code>)</li> </ul> <p>The <code>is_testthat</code> criterion allows robust lookup of test files.</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">is_testthat</code></pre></div> <pre><code>## Root criterion: directory name is `testthat` (also look in subdirectories: `tests/testthat`, `testthat`)</code></pre> <p>The example code below lists all files in the <a href="https://github.com/krlmlr/rprojroot/tree/master/tests/testthat/hierarchy">hierarchy</a> test directory. It uses two project root lookups in total, so that it also works when rendering the vignette (<em>sigh</em>):</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">dir</span>(is_testthat<span class="op">$</span><span class="kw">find_file</span>(<span class="st">"hierarchy"</span>, <span class="dt">path =</span> is_r_package<span class="op">$</span><span class="kw">find_file</span>()))</code></pre></div> <pre><code>## [1] "DESCRIPTION" "a" "b" "c" ## [5] "hierarchy.Rproj"</code></pre> <div id="another-example-custom-testing-utilities" class="section level3"> <h3>Another example: custom testing utilities</h3> <p>The hassle of using saved data files for testing is made even easier by using <em>rprojroot</em> in a utility function. For example, suppose you have a testing file at <code>tests/testthat/test_my_fun.R</code> which tests the <code>my_fun()</code> function:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">my_fun_run <-<span class="st"> </span><span class="kw">do.call</span>(my_fun, my_args) testthat<span class="op">::</span><span class="kw">test_that</span>( <span class="st">"my_fun() returns expected output"</span>, testthat<span class="op">::</span><span class="kw">expect_equal</span>( my_fun_run, expected_output ) )</code></pre></div> <p>There are two pieces of information that you’ll need every time <code>test_my_fun.R</code> is run: <code>my_args</code> and <code>expected_output</code>. Typically, these objects are saved to <code>.Rdata</code> files and saved to the same subdirectory. For example, you could save them to <code>my_args.Rdata</code> and <code>expected_output.Rdata</code> under the <code>tests/testthat/testing_data</code> subdirectory. And, to find them easily in any contexts, you can use <em>rprojroot</em>!</p> <p>Since all of the data files live in the same subdirectory, you can create a utility function <code>get_my_path()</code> that will always look in that directory for these types of files. And, since the <em>testthat</em> package will look for and source the <code>tests/testthat/helper.R</code> file before running any tests, you can place a <code>get_my_path()</code> in this file and use it throughout your tests:</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">## saved to tests/testthat/helper.R get_my_path <-<span class="st"> </span><span class="cf">function</span>(file_name) { rprojroot<span class="op">::</span><span class="kw">find_testthat_root_file</span>( <span class="st">"testing_data"</span>, filename ) }</code></pre></div> <p>Now you can ask <code>get_my_path()</code> to find your important data files by using the function within your test scripts!</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">## Find the correct path with your custom rprojroot helper function path_to_my_args_file <-<span class="st"> </span><span class="kw">get_my_path</span>(<span class="st">"my_args.Rdata"</span>) ## Load the input arguments <span class="kw">load</span>(<span class="dt">file =</span> path_to_my_args_file) ## Run the function with those arguments my_fun_run <-<span class="st"> </span><span class="kw">do.call</span>(my_fun,my_args) ## Load the historical expectation with the helper <span class="kw">load</span>(<span class="dt">file =</span> <span class="kw">get_my_path</span>(<span class="st">"expected_output.Rdata"</span>)) ## Pass all tests and achieve nirvana testthat<span class="op">::</span><span class="kw">test_that</span>( <span class="st">"my_fun() returns expected output"</span>, testthat<span class="op">::</span><span class="kw">expect_equal</span>( my_fun_run, expected_output ) )</code></pre></div> <p>For an example in the wild, see the <a href="https://github.com/tidyverse/readxl/blob/0d9ad4f570f6580ff716e0e9ba5048447048e9f0/tests/testthat/helper.R#L1-L3"><code>test_sheet()</code> function</a> in the <em>readxl</em> package.</p> </div> </div> <div id="summary" class="section level2"> <h2>Summary</h2> <p>The <em>rprojroot</em> package allows easy access to files below a project root if the project root can be identified easily, e.g. if it is the only directory in the whole hierarchy that contains a specific file. This is a robust solution for finding files in largish projects with a subdirectory hierarchy if the current working directory cannot be assumed fixed. (However, at least initially, the current working directory must be somewhere below the project root.)</p> </div> <div id="acknowledgement" class="section level2"> <h2>Acknowledgement</h2> <p>This package was inspired by the gist <a href="https://gist.github.com/jennybc/362f52446fe1ebc4c49f">“Stop the working directory insanity”</a> by Jennifer Bryan, and by the way Git knows where its files are.</p> </div> <!-- dynamically load mathjax for compatibility with self-contained --> <script> (function () { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; document.getElementsByTagName("head")[0].appendChild(script); })(); </script> </body> </html>