setwd
似乎不会更改后续块中的目录。有没有办法将工作目录设置为好?
示例:
```{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。
我希望为所有后续块设置一次目录。
答案 0 :(得分:30)
请参阅Issue #277以及更多背景信息,请参阅软件包作者的评论here
您正在寻找的是root.dir
块选项。
以下内容将为此(及后续)代码块设置根目录
```{r setup}
opts_chunk$set(root.dir = '/tmp')
```
截至RStudio的最新版本(2016年10月/ 11月),knitr render
默认需要以下代码:
```{r setup}
knitr::opts_knit$set(root.dir = '/tmp')
```
请参阅Etienne对以下版本的评论。
答案 1 :(得分:1)
这就是我一直在使用的东西,并且在使用R项目(.Rproj
文件)时似乎很好用:
knitr::opts_chunk$set(
# This should allow Rmarkdown to locate the data
root.dir = rprojroot::find_rstudio_root_file()
)
答案 2 :(得分:-1)
我遵循了整个讨论,但是在我的版本中...
平台x86_64-w64-mingw32
拱形x86_64
os mingw32
系统x86_64,mingw32
状态
专业3
次要6.0
2019年
第04个月
第26天
svn rev 76424
语言R
version.string R版本3.6.0(2019-04-26)
昵称植树
#title of the chunk: "setup"
rm(list=ls())
yourdirectory<-"F:/CasaUfficio" #here you plug in yours
require(knitr)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = yourdirectory)
...似乎不起作用。 实际上
mydir<-getwd()
print(mydir)
返回
[1]“ C:/用户/ Vimercati”
就像根本没有改变根一样