Setting working directory for rmarkdown

4,357 views
Skip to first unread message

Elise Hellwig

unread,
Jun 16, 2016, 11:53:15 PM6/16/16
to Davis R Users' Group
 Hi,

Recently there was an update that changed how setting a working directory in rmarkdown works. Before you could just use setwd(), but now when you do that there is a warning that directs you to a paragraph in the knit topic in R help that says

The working directory when evaluating R code chunks is the directory of the input document by default, so if the R code involves external files (like read.table()), it is better to put these files under the same directory of the input document so that we can use relative paths. However, it is possible to change this directory with the package option opts_knit$set(root.dir = ...) so all paths in code chunks are relative to this root.dir. It is not recommended to change the working directory via setwd() in a code chunk, because it may lead to terrible consequences (e.g. figure and cache files may be written to wrong places). If you do use setwd(), please note that knitr will always restore the working directory to the original one. Whenever you feel confused, print getwd() in a code chunk to see what the working directory really is.

So the obvious solution is to change the working directory using opts_knit. However when I do that nothing happens. I run the following code and I get the output in the attached figure:

---
title: "Test"
output: html_document
---

```{r setup}
library(knitr)

print(getwd())
opts_knit$set(root.dir='/Users/echellwig/Drive/Phenology')
print(getwd())

```

I could move the files into the working directory with the .rmd file, but I would prefer not to do that if at all possible. Note: I am on a Mac running OSX 10.11.5 (El Capitan) and my R and Rstudio software is up to date. Any insight would be much appreciated!

Thanks,
Elise
Rmarkdown_screenshot.png

Jaime Ashander

unread,
Jun 17, 2016, 1:02:59 AM6/17/16
to davi...@googlegroups.com, Davis R Users' Group
I don't know about how to fix within knitr. (I haven't dealt with this issue.) But a quick fix is to use a symlink, which you can make in terminal:

cd path/to/project
ln -s /full/path/to/big/file .

This should allow you to run code as is, without setwd

- Jaime

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Michael Hannon

unread,
Jun 17, 2016, 5:33:52 AM6/17/16
to davi...@googlegroups.com
I concur with Jaime's suggestion, but I note that on my system (see
below), I *do* see the effect of the opts_knitset statement. I wonder
if your rmarkdown package is up-to-date. Note, by the way, that you
should be getting the *same* value from `getwd` in both of your print
statements. I.e., there is no "mid-stream" change in its value.
Evidently, rmarkdown scans the file for global options before
rendering the document.

-- Mike

> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8
LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices datasets utils methods base

other attached packages:
[1] rmarkdown_0.9.6

loaded via a namespace (and not attached):
[1] htmltools_0.3.5 Rcpp_0.12.5 digest_0.6.9
junk.html

Matt Espe

unread,
Jun 27, 2016, 2:13:23 PM6/27/16
to Davis R Users' Group
Hi Elise,

You need to set the knit options in the parent environment, not inside a chunk. Run opts_knit$set() in your R session, then knit your document. This is because the process inside the chunks is separate from the process that is doing the knitting. This works as you want:

opts_knit$set(root.dir="~/Downloads/")
knit("test.Rmd")

Alternatively, you can use full path names to avoid the issue all together, but this makes the code less portable to other machines.

Matt

Matt Espe

unread,
Jun 27, 2016, 2:18:35 PM6/27/16
to Davis R Users' Group
I would add that I recommend against creating a symlink, as it can cause some unexpected behaviors. 

Matt
Reply all
Reply to author
Forward
0 new messages