运行 一个 R Markdown (check.Rmd ) 和一个 R knitr (test.Rnw ) 一起归档

run an R Markdown (check.Rmd ) and an R knitr (test.Rnw ) file together

我有以下问题;有 2 个大文档,一个用 R Markdown (check.Rmd ) 编写,另一个用 R knitr (test.Rnw ) 编写。在第一个文档中,我们有如下代码:

\section{Organisations Test}

\textbf{Running Organisations Checks}

<<CreateOrganisations, echo=FALSE, progress=TRUE, warning=FALSE, eval=TRUE>>=
source("OrganisationsTest.R")
OrganisationsTest(current_schema,server,user,pass)

@

另一个如下:

  2. check the downwards shock
```{r chunk_Int_Sh_p2,  echo=FALSE}
unique(param.int.shock.tab[SHOCKTYPE=="SHOCK_DOWN"&PERIODEND<21|PERIODEND==90, list( Maturity=PERIODEND, Shock_value=100*SHOCKVALUE)])
``` 

现在的问题是:我怎样才能将两者结合起来,以便我只有一个脚本可以一个接一个地运行和编译。只是为了澄清,我的意思是,如果两个文档都没有任何更改,我怎么能只有一个脚本适用于第一个文档 knit PDF 来创建 pdf 和另一个 CompilePDF

我想在 Linux 中可以编写 shell 脚本,但是在 Windows 中使用 RStudio 又如何呢? 真的很感谢每一个提示我有点无奈!

附录:原则上如下:如果您要编译一个 knitr 文件,我们有 2 个文件,您将在 RStudio 中使用 bottom in RStudio, and for a Markdown file one may use 底部,但我们想要将两者放在一起并点击一个底部。怎么可能?

RStudio 按钮 "Compile PDF"(用于 RNW 文档)和 "Knit PDF"(用于 RMD 文档)很方便,但在这种情况下,了解它们的作用很重要 do 以重现相同或相似的行为。

总结这个问题,它要求一种将两个文件(一个 RMD 和一个 RNW 文档)转换为 PDF 的方法,最好使用像上面提到的两个按钮这样的按钮。

不幸的是,(据我所知)无法将任何用户定义的按钮添加到 RStudio GUI。但是编写编译两个文档的R脚本很简单。

下面我假设有两个文件:

  • first.Rmd:

    This is a RMD file.
    
    ```{r, echo=FALSE}
    plot(1)
    ```
    
  • second.Rnw:

    \documentclass{article}
    \begin{document}
    
    This is a RNW file.
    
    <<>>=
    plot(1)
    @
    \end{document}
    

要将 first.Rmd 编译为 PDF,我们需要以下内容(参见 How to convert R Markdown to PDF?):

library(knitr)
library(rmarkdown)

knit(input = "first.Rmd")
render(input = "first.md", output_format = "pdf_document")

knit 调用从 first.Rmd 生成 first.md,执行块中的 R 代码。 render 将生成的降价文件转换为 PDF。 [注意底部的附录!]

要将first.Rnw编译成PDF,我们可以简单地使用knit2pdf:

knit2pdf("second.Rnw")

将两个片段复制到一个 R 脚本中并单击 "Source" 尽可能接近 "one-button-solution"。

但是,请注意,这些片段的作用与 "Compile / knit PDF" 按钮非常相似,但并不完全相同。 "Compile" 按钮启动 new R session 而上面的解决方案使用 current session.

  • 在执行代码片段之前确保使用正确的工作目录。
  • knitknit2pdf 默认都使用 envir = parent.frame()。这意味着块中的 R 代码在 调用 环境中执行(有关该主题的更多详细信息,请参阅 What is the difference between parent.frame() and parent.env() in R). This can be a useful feature, for example to "pass" variables to chunks, but it is important to know about it. Otherwise a document might compile just fine in one session (where certain variables exist in the calling environment) but cannot be compiled in another session (that is missing these variables). Therefore, this feature is a little bit dangerous in terms of reproducibility. As a solution, envir = new.env(parent = as.environment(2)) could be used; see knitr inherits variables from a user's environment, even with envir = new.env()

我刚刚意识到要关注 render:

If the input requires knitting then knit is called prior to pandoc.

(来源:?render

因此,knit(input = "first.Rmd"); render(input = "first.md", output_format = "pdf_document")可以简化为render(input = "first.Rmd", output_format = "pdf_document")。上述 knitenvir 问题也适用于 render