R Markdown Chunk和在目录中移动

问题描述 投票:1回答:1

如果我的R Markdown文档位于"documents/report.Rmd",我如何调用"source_files/script1.R""source_files/script2.R"中的源文件。我使用的是Linux和Windows。我们也可以为OS X以及所有Mac用户提供答案。如果我不知道更好,我会把它放在降价块中:

cd ..
source("source_files/script1.R")
r r-markdown
1个回答
1
投票

您可以使用该命令

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命令

© www.soinside.com 2019 - 2024. All rights reserved.