如果我的R Markdown文档位于"documents/report.Rmd"
,我如何调用"source_files/script1.R"
或"source_files/script2.R"
中的源文件。我使用的是Linux和Windows。我们也可以为OS X以及所有Mac用户提供答案。如果我不知道更好,我会把它放在降价块中:
cd ..
source("source_files/script1.R")
您可以使用该命令
knitr::opts_knit$set(root.dir = "path/to/project/directory")
我喜欢使用Rstudio,所以我将root.dir
设置为我的.Rproj文件所在的位置。然后,您可以像处理任何其他项目一样处理目录,只需使用root.dir
中的相对路径来引用它们。
请注意,此命令应该放在您的第一个代码块中,即类似以下内容
```{r setup, include = FALSE}
knitr::opts_knit$set(root.dir = "path/to/dir/")
```
并且此预设这个新目录的安装程序块中不应有其他命令。你应该开始第二个块来使用你的source
命令