How to convert .Rmd file to .Rnw file of Rstudio?

2019-03-11 01:27发布

问题:

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)

回答1:

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.



标签: r knitr rstudio