How to save R source code to a PDF - r

So I have some R source code, how do I save that source code into a PDF. I am able to save the markdown output as PDF through knitr. But how do I save the code itself onto a PDF?

Use echo = TRUE in your R chunk

Related

launch a file rmd ( markdown ) from R without using RStudio

It makes a moment for which I look if it's possible to launch/execute a file rmd ( markdown ) from R without using RStudio.
Because I have constraints in the work: I cannot use Rstudio.
I have examine but everybody uses use Markdown since Rstudio.
Converting Rmarkdown to PDF without RStudio

knit to pdf in emacs using polymode

I am trying to knit my Rnw file into PDF. With ess-noweb mode, I could knit and view the pdf but with polymode, I could knit to tex with name filename[woven].tex but could not view the pdf. Do I need to open the tex file again and compile it or is there any direct way?
Use M-n-e to export Rnw to PDF in polymode. It will take care of both weaving and latexing, and opens up the PDF for you.

Jump to position in .Rnw file in PDF produced by knitr

I'm using knitr to convert .Rnw files to .pdf files. I can use Skim to jump from a position in the .pdf file to the .tex file produced by knitr.
How can I jump from a position in the .pdf file to the .Rnw?
Check out SyncTeX. For example, RStudio's PDF viewer provides a synchronization between the PDF and the underlying Rnw file, see here or here.

Using knitr to create HTML slide with separate output of just the R code

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.

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

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.

Resources