使用 opts_chunk$set(root.dir = ...) 在 knitr 中设置工作目录不起作用
Setting work directory in knitr using opts_chunk$set(root.dir = ...) doesn't work
我的 R 项目的结构就像一个包含目录 /R
、/vignettes
、/data
等的包。在我的 /vignettes
中的一个 Rmd 文档中,我获取了一个脚本它位于 /R
。在此脚本中,我使用 read.csv()
加载位于 inst/extdata/
.
的文件
现在的问题是默认情况下Rmd文件里面的工作目录就是文件所在的目录。我们称它为 /Users/Me/Docs/Proj/vignettes
。但是,为了使 R 脚本 运行,工作目录需要是项目的主目录 (/Users/Me/Docs/Proj
)。
我尝试使用 knitr::opts_chunk$set(root.dir = normalizePath("..")
更改 Rmd 文件中的工作目录。但是显然这个 不会 更改工作目录,因为如果我在它之后调用 getwd()
输出仍然是 /Users/Me/Docs/Proj/vignettes
而 knitr::chunk_opts$get("root_dir")
returns /Users/Me/Docs/Proj
。
这是一个最小的示例 Rmd 文件:
```{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'
```
我使用的是 RStudio 版本 0.99.435。这是我的会话信息:
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
非常感谢任何帮助。如果您需要更多信息 post 对该问题发表评论。提前致谢!
It is knitr::opts_knit
而不是 knitr::opts_chunk
.
正如 Yihui 在他的回答中指出的那样,错误只是我使用了 opts_chunk$set()
而不是 opts_knit$set()
。
然而,可能值得注意的是,工作目录的更改不会影响当前块,只会影响下一个块。所以e。 G。如果你想加载相对于新工作目录的数据,请在以下块中执行此操作。
如果你有一个带有嵌套子文件夹的 R 项目,因此 .Rproj 和 .Rmd 文件位于不同的文件夹中,你可以使用命令 rprojroot::find_rstudio_root_file()
查找并将工作目录设置为项目的Kniting 期间的主文件夹(而不是包含 rMarkdown 代码文件的文件夹)。
所以至少要使用以下内容:
```{r setup}
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
```
在 setup
区块内。
另见
root.dir =
设置工作目录的一些实现细节。
尽管 Yihui and Tommy 已经给出了一些很棒的答案。我仍然被困在设置工作目录中。所以我想在这里做出一个完整的答案。
Knitr’s settings must be set in a chunk before any chunks which rely on those settings to be active. It is recommended to create a knit configuration chunk as the first chunk in a script with cache = FALSE and include = FALSE options set. This chunk must not contain any commands which expect the settings in the configuration chunk to be in effect at the time of execution.
- 我的代码示例:
就我而言,.Rproj
和 .Rmd
文件位于同一文件夹中。
```{r setup, include=FALSE, cache = FALSE}
require("knitr")
## setting working directory
opts_knit$set(root.dir = "~/Documents/R/Example")
```
对我来说 knitr::opts_knit$set
使用了 root.dir
但没有使用 echo = FALSE
而
knitr::opts_chunk$set
不适用于 root.dir
但确实适用于 echo = FALSE
因此,
我在设置块中使用了两个选项:
```{r settings, include = FALSE}
knitr::opts_chunk$set(echo = FALSE
, comment = NA
, warning = FALSE
, error = FALSE
, message = FALSE
, tidy = TRUE)
knitr::opts_knit$set(root.dir = 'C:/...')
```
当 .Rmd文件位于其他文件上游的子目录中。如果您遇到同样的问题,请尝试:
library(here)
knitr::include_graphics(here("figs/figure.png"))
在这些情况下,您显然也可以使用 ../
,但这样一来,调试时路径将无法直接在控制台中使用。
解决方案来源:https://community.rstudio.com/t/knittr-include-graphics-image-png-cannot-find-files/82959/2
我的 R 项目的结构就像一个包含目录 /R
、/vignettes
、/data
等的包。在我的 /vignettes
中的一个 Rmd 文档中,我获取了一个脚本它位于 /R
。在此脚本中,我使用 read.csv()
加载位于 inst/extdata/
.
的文件
现在的问题是默认情况下Rmd文件里面的工作目录就是文件所在的目录。我们称它为 /Users/Me/Docs/Proj/vignettes
。但是,为了使 R 脚本 运行,工作目录需要是项目的主目录 (/Users/Me/Docs/Proj
)。
我尝试使用 knitr::opts_chunk$set(root.dir = normalizePath("..")
更改 Rmd 文件中的工作目录。但是显然这个 不会 更改工作目录,因为如果我在它之后调用 getwd()
输出仍然是 /Users/Me/Docs/Proj/vignettes
而 knitr::chunk_opts$get("root_dir")
returns /Users/Me/Docs/Proj
。
这是一个最小的示例 Rmd 文件:
```{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'
```
我使用的是 RStudio 版本 0.99.435。这是我的会话信息:
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
非常感谢任何帮助。如果您需要更多信息 post 对该问题发表评论。提前致谢!
It is knitr::opts_knit
而不是 knitr::opts_chunk
.
正如 Yihui 在他的回答中指出的那样,错误只是我使用了 opts_chunk$set()
而不是 opts_knit$set()
。
然而,可能值得注意的是,工作目录的更改不会影响当前块,只会影响下一个块。所以e。 G。如果你想加载相对于新工作目录的数据,请在以下块中执行此操作。
如果你有一个带有嵌套子文件夹的 R 项目,因此 .Rproj 和 .Rmd 文件位于不同的文件夹中,你可以使用命令 rprojroot::find_rstudio_root_file()
查找并将工作目录设置为项目的Kniting 期间的主文件夹(而不是包含 rMarkdown 代码文件的文件夹)。
所以至少要使用以下内容:
```{r setup}
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
```
在 setup
区块内。
另见
root.dir =
设置工作目录的一些实现细节。
尽管 Yihui and Tommy 已经给出了一些很棒的答案。我仍然被困在设置工作目录中。所以我想在这里做出一个完整的答案。
Knitr’s settings must be set in a chunk before any chunks which rely on those settings to be active. It is recommended to create a knit configuration chunk as the first chunk in a script with cache = FALSE and include = FALSE options set. This chunk must not contain any commands which expect the settings in the configuration chunk to be in effect at the time of execution.
- 我的代码示例:
就我而言,.Rproj
和 .Rmd
文件位于同一文件夹中。
```{r setup, include=FALSE, cache = FALSE}
require("knitr")
## setting working directory
opts_knit$set(root.dir = "~/Documents/R/Example")
```
对我来说 knitr::opts_knit$set
使用了 root.dir
但没有使用 echo = FALSE
而
knitr::opts_chunk$set
不适用于 root.dir
但确实适用于 echo = FALSE
因此,
我在设置块中使用了两个选项:
```{r settings, include = FALSE}
knitr::opts_chunk$set(echo = FALSE
, comment = NA
, warning = FALSE
, error = FALSE
, message = FALSE
, tidy = TRUE)
knitr::opts_knit$set(root.dir = 'C:/...')
```
当 .Rmd文件位于其他文件上游的子目录中。如果您遇到同样的问题,请尝试:
library(here)
knitr::include_graphics(here("figs/figure.png"))
在这些情况下,您显然也可以使用 ../
,但这样一来,调试时路径将无法直接在控制台中使用。
解决方案来源:https://community.rstudio.com/t/knittr-include-graphics-image-png-cannot-find-files/82959/2