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)
@
and in the other as follows:
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)])
```
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
?
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!
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?
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.
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.
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}
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")
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!]
To compile first.Rnw
to PDF, we can simply use knit2pdf
:
knit2pdf("second.Rnw")
Copying both snippets into one R script and clicking "Source" is as close as possible to a "one-button-solution".
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.
- 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.
I just realized to following about render
:
If the input requires knitting then knit
is called prior to pandoc
.
(Source: ?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.