Skip to content

Instantly share code, notes, and snippets.

@mathematicalcoffee
Last active December 16, 2015 07:49
Show Gist options
  • Save mathematicalcoffee/5401238 to your computer and use it in GitHub Desktop.
Save mathematicalcoffee/5401238 to your computer and use it in GitHub Desktop.
Knit a Rmd straight to a PDF (HTML too but then you could just use `knit2html`).
library(knitr)
#' Knits a Rmd to a pdf or HTML
#'
#' In the case of output='html', you may as well just use `knit2html`
#' (although this doesn't clutter your workspace with the intermediate
#' outputs like the .md file).
#'
#' In the case of output='pdf', we knit the Rmd to md (markdown) and then
#' use `pandoc` to convert to PDF.
#'
#' The added benefit is in the 'change.dev' option which can automagically
#' set your global chunk options to produce 'pdf' if you are knitting to pdf
#' and 'svg' if you are knitting to HTML, giving you antialiased graphics
#' regardless of the output format.
#'
#' @param input: path to input Rmd file
#' @param output: either 'pdf' or 'html'
#' @param tangle: whether to additionally produce a script from the input
#' @param out.dir: directory to place output files.
#' @param documentation: if `tangle` is true the level of documentation to put
#' into the code. `0` is pure code, `1` just adds chunk headers,
#' `2` adds all text chunks as roxygen comments
#' @param change.dev: If TRUE, this will change the global chunk options
#' such that all figures have default device 'pdf' if we are
#' knitting to PDF or 'svg' if we are knitting to HTML.
#' or 'svg' (if we are knitting to HTML).
#' Alternatively this may be a string like 'png', in which case
#' this device is used as default.
#' If FALSE, no global chunk options are changed.
#' @param ... : passed to `knit` (and `purl` if `tangle=T`)
#' @return nothing (NULL, invisibly)
#' @seealso knit2pdf for knitting a Rnw to PDF
#' @seealso knit2html for knitting a Rmd to HTML
knit2 <- function (input, output=c('pdf', 'html'), out.dir='.',
tangle=FALSE, documentation=1L,
change.dev=TRUE, ...) {
output <- match.arg(output)
fname <- regmatches(input, regexec('^(.+?)(\\.[^.]+)?$', input))[[1]][2]
# change global option for chunk output if requested
# Note - must call render_markdown() *before* overriding `dev` or else
# it is overridden in render_markdown()
if (isTRUE(change.dev)) {
render_markdown()
opts_chunk$set(dev=switch(output, pdf='pdf', html='svg', 'png'))
} else if (is.character(change.dev)) {
render_markdown()
opts_chunk$set(dev=change.dev)
}
# knit Rmd to md in /tmp somewhere
out <- tempfile(fileext='.md')
knit(input, output=out, ...)
# pander md to pdf or HTML
out <- pandoc(input=out, format=ifelse(output == 'pdf', 'latex', 'html'))
# copy to the output directory
file.copy(out, file.path(out.dir, paste0(fname, '.', output)))
# code: just knit Rmd to R
if (tangle)
purl(input, file.path(out.dir, paste0(fname, '.R')), documentation=documentation, ...)
invisible(NULL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment