在 RStudio/RMarkdown 中,如何设置? [英] In RStudio/RMarkdown, how to setwd?
问题描述
setwd
似乎不会更改后续块中的目录.有没有办法永久设置工作目录?
setwd
in an Rmd file in RStudio does not appear to change the directory in subsequent chunks. Is there a way to set the working directory for good?
示例:
```{r}
setwd("/tmp")
getwd()
```
```{r}
getwd()
```
输出:
setwd("/tmp")
getwd()
## [1] "/private/tmp"
getwd()
## [1] "/Users/me/src"
这是在 Mac OS 10.8.5 上使用 RStudio 0.97.551、R 版本 3.0.2 和 knitr 版本 1.5.
This is on Mac OS 10.8.5 using RStudio 0.97.551, R version 3.0.2 and knitr version 1.5.
我希望为所有后续块设置一次目录.
I wish to set the directory once for all subsequent chunks.
推荐答案
See Issue #277 and for further background, the package author's comments here
您要查找的是 knitr::opts_knit
中的 root.dir
选项.
What you are looking for is the root.dir
option in knitr::opts_knit
.
以下将为后续代码块(但不是此块)设置根目录:
The following will set the root directory for subsequent code chunks (but not this chunk):
```{r setup}
knitr::opts_knit$set(root.dir = '/tmp')
```
RStudio 1.0.44
从 RStudio 的最新版本(2016 年 10 月/11 月)开始,knitr 的 render
默认需要以下代码片段:
RStudio 1.0.44
as of RStudio's latest release (Oct/Nov 2016), the following snippet is needed for knitr's render
default:
```{r setup}
knitr::opts_knit$set(root.dir = '/tmp')
```
请参阅 Etienne 对以下版本的评论.
see Etienne's comment about versions below.
这篇关于在 RStudio/RMarkdown 中,如何设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!