How to convert R Markdown to PDF?

后端 未结 8 1191
天命终不由人
天命终不由人 2020-11-28 00:36

I\'ve previously asked about the commands for converting R Markdown to HTML.

What is a good way to convert R Markdown files to PDF documents?

8条回答
  •  清酒与你
    2020-11-28 01:30

    I found using R studio the easiest way, but if wanting to control from the command line, then a simple R script can do the trick using rmarkdown render command (as mentioned above). Full script details here

    #!/usr/bin/env R
    
    # Render R markdown to PDF.
    # Invoke with:
    # > R -q -f make.R --args my_report.Rmd
    
    # load packages
    require(rmarkdown)
    
    # require a parameter naming file to render
    if (length(args) == 0) {
        stop("Error: missing file operand", call. = TRUE)
    } else {
        # read report to render from command line
        for (rmd in commandArgs(trailingOnly = TRUE)) {
            # render Rmd to PDF
            if ( grepl("\\.Rmd$", rmd) && file.exists(rmd)) {
                render(rmd, pdf_document())
            } else {
                print(paste("Ignoring: ", rmd))
            }
        }
    }
    

提交回复
热议问题