6
votes

I upgraded to RStudio 1.0.44 and it seems knitr::opts_knit$set(root.dir = path) where path is my directory is not working as before. It throws a message:

The working directory was changed to /... inside a notebook chunk. The working
directory will be reset when the chunk is finished running. Use the knitr
root.dir option in the setup chunk to change the the working directory for
notebook chunks.

This message will now appear in every following command. Note that I am not knitting the rmd yet. I'm just running commands out of it. Setting the working directory directly in command line via setwd() returns the correct path in getwd() but loading a file with relative path (./...) again would return above message. The exact same rmd works fine with RStudio 0.99.896. What am I missing?

sessionInfo()

R version 3.3.0 (2016-05-03)  
Platform: x86_64-w64-mingw32/x64 (64-bit)  
>Running under: Windows 7 x64 (build 7601) Service Pack 1  

other attached packages:
[1] scales_0.4.0     ggplot2_2.1.0    xtable_1.8-2     data.table_1.9.6  
[5] dplyr_0.4.3      knitr_1.15       pander_0.6.0 
6
I got the same problem. Have you solved yours? How?Daijiang Li
hi, not solved and no one (from @ RStudio) answered either. see my own answer / explanation below. but it's not satisfying.Triamus
but effectively it means that I can currently not use the new RStudio with my old workflow as he wouldn't let me change the working directory with a single line execution via ctrl+r. he would return the correct directory via getwd() but e.g. in a relative data.table::fread("./my_file"), he would try finding the file where the rmd is stored not in the working directory I just changed. I don't see how people that split code and data into different working directories can now work interactively in RStudio markdown?Triamus

6 Answers

4
votes

You can change the working directory with:

```{r "setup", include=FALSE}
knitr::opts_knit$set(root.dir = getwd())  # with something else than `getwd()`
```

as described at the bottom of the dedicated page in rmarkdown website.

But Restart R and run all chunks (accessible in the "Run>" tab in RStudio) fixed the very same problem on my machine.

Does this help?

3
votes

I put my Rmd file in doc/file.rmd and R code file in R/code.r, and the project file is the current folder .. When I knit the Rmd file, knitr::opts_knit$set(root.dir = "..") does not work but seated("..") works.

I just find that I put source(R/code.r) in the same first setup chunk as knitr::opts_knit$set(root.dir = ".."). That is:

```{r "setup", include=FALSE}
knitr::opts_knit$set(root.dir = "..") 
source(R/code.r)
```

When I split it into two chunks, it works now. That is:

```{r "setup", include=FALSE}
knitr::opts_knit$set(root.dir = "..")
```

```{r "source"}
source(R/code.r)
```

I am not sure whether this is your problem. I put it here just in case someone or future myself makes the same silly mistake.

2
votes

I ran into this behavior too. The fix is to use normalizePath():

```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir = normalizePath(".."))
```

EDIT

It turns out this was not the fix. But since this appears a lot in google searches, I finally found out that the chunk where you setup knitr parameters must not run your code.

You should source your scripts from other chunks. This will fix the issue.

This is documented here.

1
votes

as far as I can see, the behavior of rmd files has changed in new rstudio. only running a line in a chunk via ctrl+r will result in message as describe in OPs question. rather the key combination ctrl+shift+enter results in the execution of a particular chunk. so it seems I cannot run a single line in an rmd and have its output written to console.

0
votes

enter image description here

You can also choose the working directory from the menu Tools -> Global Options -> R Markdown. And change the "evaluate chunks in directory" to the same as the directory your Rproj is in. This work for my Rstudio V1.3.1093 for mac.

https://bookdown.org/yihui/rmarkdown-cookbook/working-directory.html

-1
votes

I don't have enough reputation to comment, or I would have commented on @philsf's answer.

I combined @Chunhui Gu and @philsf answers for my issue. I'm using RStudio Version 1.3.1093.

I had the same confusion as the OP with regards to checking my wd (getwd()) and seeing the right project directory but then having Markdown show a different directory when I was knitting. Moreover, I would have to change a "." to ".." in all the file pathways when switching between knitting and running code with cmd-enter (mac).

My setup: First, I changed Tools > Global Options > R Markdown set to "evaluate chunks in directory", but I also had included the normalizePath() chunk option, thinking that would help.

    ```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir = normalizePath(".."))
```

Per @philsf, I took this chunk out, and now the pathways work when knitting and when running cmd-enter.

I suspect that normalizePath() setup chunk was helpful in an older RStudio version (?), or it's possible the R Markdown > "Evaluate chunks in directory" option has always been there and I only just found out about it. Hopefully, this helps others with the same frustrating problem.