I really like using knitr in Rstudio and have been using it to write markdown presentations and data analysis. I want to use the same code and results in a paper and want to convert the code chunks in Rmd file ```{r} to the chunks of Rnw file << >>= #.
This allows using the same document and code written for presentation for the main paper as well.
Is there a way of converting between code chunks of markdown and Rnw files ?
or the entire file itself as apart from the difference in syntax of code chunks, they are quite similar in the markup (converting the text to latex is easy with say pandoc)
Instead of converting the whole document, you can just externalize your R code so it can be shared across different documents; see http://yihui.name/knitr/demo/externalization/
Once you have read_chunk('Rcode.r'), you can use ```{r label} in your Rmd and <<label>>= in your Rnw document, where label comes from the line ## #knitr label in the R script.
Related
For lectures, I am using knitr to produce LaTeX beamer slides as a PDF. For a given lecture, I want to produce also (a) 1-up handout (using the handout option, and (b) the same handout formatted 4-up.
I find I have to run knitr 3 times to do this as shown below. Is there a way to simplify this work flow?
A lecture stub:
\documentclass[10pt,table]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
...
\end{document}
And I run knitr as
knit2pdf("Lecture1.Rnw")
To get the 1-up handout (which suppresses the separate pages when you use transitions), I edit the first line to:
\documentclass[10pt,table,handout]{beamer}
and run
knit2pdf("Lecture1.Rnw" output="Lecture1-1up.tex")
Finally, to get the 2 x 2 version, I use the LaTeX pgfpages package,
\documentclass[10pt,table,handout]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}[letterpaper,landscape]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
And run:
knit2pdf("Lecture1.Rnw" output="Lecture1-4up.tex")
(I found that with beamer, I could not simply print the PDF 4-up using Adobe Acrobat -- it generated a corrupt PDF file. I was forced to use pgfpages)
Then, of course I have to revert my .Rnw file to the original if I need re-do the slides. Very tedious. There must be a better way.
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.
I have a rmarkdown document (.Rmd) that I've been doing some analysis in. I now realize I would prefer the functionality of LaTeX. Is there an easy way to convert the file to .Rnw, preserving the chunks (not just the body text)? Articles seem to concentrate on converting the output (i.e. from .html to .pdf) or from sweave to markdown.
I am not sure if this type of question complies with the SO rules for well-defined questions ;) ... anyway:
I want to convert several R Sweave files (.Rnw) to R markdown files (.Rmd). Jeromy Anglin has posted on this matter here but there is no code supplied. I tried to use pandoc, but of course pandoc cannot handle the chunk tags and inline code tags correctly.
Consclusion: I guess I will have do write some code to parse my .Rnw files to prepare them for pandoc conversion. Thus my questions:
Is there a better way to go?
Does someone by chance have code
available that will do the job?
TIA
As #Karl commented, LaTeX --> markdown is not a trivial conversion as there are far more options and environments available in LaTeX compared to markdown. You are probably best off working with something like pandoc (see Demo #5). Basically, instead of doing
.Rnw --> .Rmd --> .md
you would do
.Rnw --> .tex --> .md
with pandoc. If you really want to go from .Rnw --> .Rmd, you may want to check out the pander package to write a function to extract code chunks, convert the remaining LaTeX content to markdown, and then re-insert the code chunks into the markdown document.
I am using knitr to create a set of lecture slides for a class using R. I would like to create a separate "companion file" that contains just has the R code (corresponding to the slides), so that students can execute the R code by cutting and pasting from the companion file.
For example, in the .Rmd file:
``` {r ....}
plot(x,y)
```
Then there would be a text file with:
plot(x,y)
But, have such a file be automatically produced from the .Rmd file?
Even better if the .Rmd file has such tags:
``` {r basic.plot ....}
plot(x,y)
```
Then, text file has:
# basic.plot
plot(x,y)
Can this be accomplished using knitr?
Yes, this is possible. What you're trying to do is called tangling, and it comes from the world of literate programming.
The knit function supports a tangle option that should be set to TRUE if you want to extract source code.