Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting work directory in knitr using opts_chunk$set(root.dir = ...) doesn't work

Tags:

r

rstudio

knitr

My R project is structured like a package with directories /R, /vignettes, /data etc. In one of my Rmd docs in /vignettes I source a script which in located in /R. Inside this script I use read.csv() to load a file located in inst/extdata/.
The problem now is that by default the working directory inside the Rmd file is the directory where the file is located. Let's call it /Users/Me/Docs/Proj/vignettes. However in order for the R script to run the working directory needs to be the project's main directory (/Users/Me/Docs/Proj).
I tried to change the working directory in the Rmd file using knitr::opts_chunk$set(root.dir = normalizePath(".."). However apparently this doesn't change the working directory since if I call getwd() after it the output is still /Users/Me/Docs/Proj/vignettes whereas knitr::chunk_opts$get("root_dir") returns /Users/Me/Docs/Proj.

Here is a minimal example Rmd file:

```{r}
getwd()  # returns 'Users/Me/Docs/Proj/vignettes'
knitr::opts_chunk$set(root.dir = normalizePath(".."))  # should change the working directory to 'Users/Me/Docs/Proj'
getwd()  # again returns 'Users/Me/Docs/Proj/vignettes'
knitr::opts_chunk$get("root.dir")  # returns 'Users/Me/Docs/Proj'
```

I am using RStudio Version 0.99.435. Here is my session Info:

R version 3.2.0 (2015-04-16)  
Platform: x86_64-apple-darwin14.3.0 (64-bit)  
Running under: OS X 10.10.3 (Yosemite)  

locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

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

loaded via a namespace (and not attached):
[1] htmltools_0.2.6 tools_3.2.0     yaml_2.1.13     rmarkdown_0.6.1 digest_0.6.8   

Any help is kindly appreciated. If you need more info post a comment to the question. Thanks in advance!

like image 727
Thomas Neitmann Avatar asked May 14 '15 12:05

Thomas Neitmann


People also ask

How do I change the directory in R Markdown?

The usual way to change the working directory is setwd() , but please note that setwd() is not persistent in R Markdown (or other types of knitr source documents), which means setwd() only works for the current code chunk, and the working directory will be restored after this code chunk has been evaluated.

How do I find my current working directory in R?

If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will get the current working directory or current path of the R script. To change the current working directory we need to use a function called setwd( ).

How does R knitr work?

When you run render , R Markdown feeds the . Rmd file to knitr, which executes all of the code chunks and creates a new markdown (. md) document which includes the code and its output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.

How do I show output in R?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .


3 Answers

It is knitr::opts_knit instead of knitr::opts_chunk.

like image 175
Yihui Xie Avatar answered Oct 26 '22 01:10

Yihui Xie


Where you have an R project with nested subfolders, so that the .Rproj and .Rmd files are located in different folders, you can use the command rprojroot::find_rstudio_root_file() to find and set the working directory to the Project's main folder during Kniting (instead of the folder containing the rMarkdown code file).

So at a minimum use the following:

```{r setup}

knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())

```

inside the setup chunk.

See also Automatically finding the path of current R project in R Studio

like image 33
JWilliman Avatar answered Oct 26 '22 00:10

JWilliman


As Yihui has pointed out in his answer the mistake was simply that I used opts_chunk$set() instead of opts_knit$set().

However it might be worth noting that the change of the working directory affects not the current but only the next chunk. So e. g. if you want to load data relative to the new working directory do that in the following chunk.

like image 39
Thomas Neitmann Avatar answered Oct 26 '22 01:10

Thomas Neitmann