Home > OS >  Export R script without output to PDF/Word
Export R script without output to PDF/Word

Time:08-25

Is it possible to export the R script to a PDF and/or Word document without the outputs (i.e. without whatever the console prints; plots, graphs etc.)? I know about the r markdown package but as far as I know, it exports the script only with outputs.

CodePudding user response:

rmarkdown is very flexible, you don't have to include output. If you set the option eval = FALSE none of the code will be evaluated, so no outputs will be generated.

See here for a detailed list of options available at the code-chunk level.

CodePudding user response:

To follow up on @GregorThomas's answer: if you simply add R code block-formatting around all of your code with eval=FALSE specified and save it with an .rmd extension, you can then click "Knit to PDF" in RStudio (which will automatically add a minimal header). I think you could probably also do rmarkdown::render("myfile.rmd", output_format = "pdf_document"). If you wanted you could set up a little script to do the minimal editing and rendering automatically ...

```{r eval = FALSE}
x <- 2 3
print("hello")
```

Something like (untested!)

printme <- function(file) {
   tt <- tempfile(fileext = ".Rmd")
   writeLines(c("```{r eval=FALSE}",
              readLines(file),
              "```"),
         tt)
   rmarkdown::render(tt, output_format = "pdf_document", 
                     output_file = "out.pdf")
}
  •  Tags:  
  • r
  • Related