Skip to content

Instantly share code, notes, and snippets.

@nhoffman
Created March 17, 2016 04:08
Show Gist options
  • Save nhoffman/ddfe7f2cf90f4e85d1c2 to your computer and use it in GitHub Desktop.
Save nhoffman/ddfe7f2cf90f4e85d1c2 to your computer and use it in GitHub Desktop.
knit R markdown files
#!/usr/bin/env Rscript
if(Sys.getenv("VIRTUAL_ENV") == ""){ stop("An active virtualenv is required") }
source(file.path(Sys.getenv('VIRTUAL_ENV'), 'bin', 'rvenv'))
suppressPackageStartupMessages(library(argparse, quietly = TRUE))
suppressPackageStartupMessages(library(rmarkdown, quietly = TRUE))
main <- function(arguments){
parser <- ArgumentParser()
parser$add_argument('infile')
parser$add_argument('-o', '--outfile')
parser$add_argument('-f', '--format', choices=c('html', 'pdf', 'all'),
default='html', help='output format [default %(default)s]')
args <- parser$parse_args(arguments)
format <- if(args$format == 'all'){
'all'
}else{
gettextf('%s_document', args$format)
}
rmarkdown::render(
args$infile,
output_file=args$outfile,
output_format=format)
}
main(commandArgs(trailingOnly=TRUE))
## invisible(warnings())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment