从主.R文件编织.rmd时,无法在.rmd块中找到文件源 [英] trouble finding file source in .rmd chunk when knitting .rmd from master .R file

查看:773
本文介绍了从主.R文件编织.rmd时,无法在.rmd块中找到文件源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为testknit的项目目录(我知道,请参见 github (对于MRE),在其中有几个子目录,包括scripts,其中保存了.R.rmd文件.

Let's say I have a project directory called testknit (and I do, see github for MRE), and inside this I have several subdirectories, including scripts where I keep .R and .rmd files.

在RStudio中,我创建一个项目并选择此testknit目录,以便在打开该项目时,工作目录为mypath/testknit.

In RStudio, I create a project and select this testknit directory so that when I open the project, the working directory is mypath/testknit.

testknit/scripts内部,我有一个master.R文件.如果我想获取一个名为testsource1.R的文件,该文件也位于testknit/scripts中,则可以从master.R内部运行source("scripts/testsource1.R").

Inside testknit/scripts I have a master.R file. If I want to source a file called testsource1.R, which is also in testknit/scripts, I can run source("scripts/testsource1.R") from within master.R.

library(knitr)
getwd()
# [1] "mypath/testknit"
source("scripts/testsource1.R")

到目前为止,一切都很好.

So far so good.

但是,我们也想编织一个位于testknit/scripts中的名为test.rmd.rmd文件.我可以从master.R运行knit("scripts/test.rmd").

But let's say I also want to knit a .rmd file called test.rmd that is located in testknit/scripts. I can run knit("scripts/test.rmd") from master.R.

我的test.rmd文件执行以下操作:

My test.rmd file does the following:

```{r setup}
  library(knitr)
  opts_knit$set(root.dir='../')
```

```{r option1}
  source("scripts/testsource2.R")
```

```{r option2}
  source("testsource2.R")
```

由于test.rmd存在于testknit/scripts中,因此我在第一个块中指定了opts_knit$set(root.dir='../'),因此knitr知道我的根目录确实是上一级.

Since test.rmd exists within testknit/scripts, I specify opts_knit$set(root.dir='../') in the first chunk so knitr knows that my root directory is really one level up.

当我在RStudio中打开test.rmd并单击knit HTML时,可以预见,option1块有效,而option2块无效.

When I open test.rmd in RStudio and click knit HTML, predictably, the option1 chunk works and the option2 chunk does not.

但是,当我尝试通过从master.R运行knit("scripts/test.rmd")而不是从.rmd文件内部进行编织来编织test.rmd时,这两个块选项均无效.两者都返回一个错误,提示没有该名称的文件.

But when I try to knit test.rmd by running knit("scripts/test.rmd") from master.R instead of knitting from within the .rmd file, neither chunk option works. Both return an error that there is no file by that name.

我做错了什么?从母版.R编织.rmd文件时,为什么R无法找到testsource2.R?

What am I doing wrong? Why can't R find testsource2.R when knitting the .rmd file from the master .R?

有关可重现的示例,请参见上面的github链接.

See github link above for reproducible example.

更新:

正如我在下面的注释中指出的那样,我尝试在opts_knit$set之前添加wd <- getwd()并将(root.dir='../')更改为(root.dir=wd).因此,当我从master.R运行knit("scripts/test.rmd")时,运行option2块是因为我添加的wd被设置为mypath/testknit/scripts.但是,如果我打开.rmd文件并运行所有块,则将wd设置为根目录mypath/testknit,然后运行option1块.

As I noted below in the comments, I tried adding wd <- getwd() just before opts_knit$set and changed (root.dir='../') to (root.dir=wd). So when I run knit("scripts/test.rmd") from master.R, the option2 chunk runs because the wd I added gets set to mypath/testknit/scripts. But if i open the .rmd file and run all chunks, wd is set to the root directory, mypath/testknit, and the option1 chunk runs.

我需要工作目录来保留项目根目录.对于我来说,这似乎不是一个优雅的解决方案,但可以进行以下更改:

I need the working directory to remain the project root. This does not seem like an elegant solution to me, but changing:

```{r setup}
  library(knitr)
  opts_knit$set(root.dir='../')
```

```{r setup}
  library(knitr)
  wd <- ifelse(basename(getwd())=="scripts", 
               gsub("/scripts", "", getwd()),
               getwd())
  opts_knit$set(root.dir=wd)
```

让我运行.rmd文件或master.R中的knit("scripts/test.rmd")文件时的所有块.可以,但是感觉好像我使用了错误的方法.

lets me run all chunks when in the .rmd file or knit("scripts/test.rmd") from master.R. It works, but it feels like I am taking the wrong approach.

推荐答案

@Yihui:也许您可以使用normalizePath('../')../设置为绝对路径.相对的工作目录可能会使人感到困惑(至少,在我阅读过多级别的相对路径后,我的头很痛:).顺便说一句,当您在RStudio中编织HTML时,RStudio首先将工作目录更改为输入的Rmd文件.

@Yihui: Perhaps you can make ../ an absolute path using normalizePath('../'). A relative working directory can be confusing to reason about (at least my head hurts after I read too many levels of relative paths :). BTW, when you Knit HTML in RStudio, RStudio first changes the working directory to the input Rmd file.

我:是的!仅使用opts_knit$set(root.dir=normalizePath('../'))即可将.rmd文件从master.R编织并编织为html或从.rmd内部运行所有块.我更新了 github示例. test-b.rmd现在显示了这一点.谢谢!

Me: yes! using just opts_knit$set(root.dir=normalizePath('../')) works for knitting the .rmd file from master.R and knitting to html or running all chunks from within the .rmd. I updated the github example. test-b.rmd now shows this. thanks!

这篇关于从主.R文件编织.rmd时,无法在.rmd块中找到文件源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆