一起运行R Markdown(check.Rmd)和R knitr(test.Rnw)文件 [英] run an R Markdown (check.Rmd ) and an R knitr (test.Rnw ) file together

查看:767
本文介绍了一起运行R Markdown(check.Rmd)和R knitr(test.Rnw)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following problem; There are 2 big documents, one written in R Markdown (check.Rmd ) and the other in R knitr (test.Rnw ). In the first doc we have a code like the following:

\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呢?

Now the question: how can I combine both so that I have just one script which runs and compile both one after each other. Just for clarification, I mean without any changes in both documents how can I have just one script which applyes to the first doc knit PDF to create pdf and to the other one CompilePDF ?

我想在Linux中可以写一个shell脚本,但是在windowes中使用RStudio可以做什么呢? 我真的很感谢我的每一个提示,我有点无助!

I suppose in Linux one can write a shell script but what a bout using RStudio in windowes? I am really grateful for every hint I am a little bit helpless!

附录:原则上如下:如果您要编译编织文件,我们有2个文件,您可以使用位于RStudio底部,用于Markdown文件一个可以使用在RStudio的底部,但是我们希望将两者放在一起并在一个底部滑动.怎么可能?

Addendum: In principle it is as follows: we have 2 files if you would compile a knitr file you would use bottom in RStudio, and for a Markdown file one may use bottom in RStudio, BUT we want to put both together and klick on one bottom. How is it possible?

推荐答案

RStudio按钮编译PDF"(对于RNW文档)和编织PDF"(对于RMD文档)很方便,但是在这种情况下了解他们所做的很重要,以便重现相同或相似的行为.

The RStudio buttons "Compile PDF" (for RNW documents) and "Knit PDF" (for RMD documents) are convenient, but in cases like this one it is important to understand what they do in order to reproduce the same or similar behavior.

总结问题,它要求一种将两个文件(RMD和RNW文档)转换为PDF的方法,最好使用上述两个按钮之类的按钮.

Summing the question up, it asks for a way to convert two files (a RMD and a RNW document) to PDF, preferably using a button like the two buttons mentioned above.

不幸的是,(据我所知)无法向RStudio GUI添加任何用户定义的按钮.但是编写一个可以同时编译两个文档的R脚本很简单.

Unfortunately, (up to my knowledge) it is not possible to add any user-defined buttons to the RStudio GUI. But it is straightforward to write an R script that compiles both documents.

在下面,我假设两个文件:

In the following I assume two files:

  • 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,我们需要以下内容(请参阅如何将R Markdown转换为PDF?):

    To compile first.Rmd to PDF, we need the following (see 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将生成的markdown文件转换为PDF. [请注意底部的附录!]

    The knit call generates first.md from first.Rmd, execting the R code in the chunks. render converts the resulting markdown file to PDF. [Note the addendum at the bottom!]

    要将first.Rnw编译为PDF,我们只需使用knit2pdf:

    To compile first.Rnw to PDF, we can simply use knit2pdf:

    knit2pdf("second.Rnw")
    

    将两个片段复制到一个R脚本中并单击源"尽可能接近一键式解决方案".

    Copying both snippets into one R script and clicking "Source" is as close as possible to a "one-button-solution".

    但是,请注意,这些片段的功能与编译/编织PDF"按钮非常相似,但它们并不完全相同. 编译"按钮启动新的R会话,而上述解决方案使用当前会话.

    However, note that the snippets do something very similar to the "Compile / knit PDF" button, but it is not identical. The "Compile" buttons start a new R session while the solution above uses the current session.

    • 在执行代码片段之前,请确保使用正确的工作目录.
    • 默认情况下,knitknit2pdf均使用envir = parent.frame().这意味着大块的R代码在调用环境中执行(请参阅 parent.frame()parent.env() in R ).这可能是一个有用的功能,例如将变量传递"给块,但是了解这一点很重要.否则,文档可能在一个会话中可以正常编译(在调用环境中存在某些变量),但不能在另一个会话中编译(缺少这些变量).因此,就可重复性而言,此功能有些危险.作为解决方案,可以使用envir = new.env(parent = as.environment(2)).请参阅 knitr从用户环境继承变量,甚至使用envir = new.env() 可以获取有关该主题的更多详细信息.
    • Before executing the snippets make sure to use the correct working directory.
    • Both knit and knit2pdf by default use envir = parent.frame(). That means R code in chunks is executed in the calling enironment (see 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() for more details on that topic.

    我刚刚意识到要关注render:

    如果输入需要编织,则在pandoc之前调用knit.

    (来源:?render)

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

    Therefore, knit(input = "first.Rmd"); render(input = "first.md", output_format = "pdf_document") can be simplified to render(input = "first.Rmd", output_format = "pdf_document"). The envir issues of knit from above apply to render as well.

    这篇关于一起运行R Markdown(check.Rmd)和R knitr(test.Rnw)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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