问题
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
回答1:
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?
回答2:
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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/40717410/opts-knitsetroot-dir-path-does-not-work-after-rstudio-upgrade-1-0-44