Skip to content

Instantly share code, notes, and snippets.

@PHPirates
Last active September 6, 2018 15:46
Show Gist options
  • Save PHPirates/9c0bfe55500dc9d63e140809235dbb0d to your computer and use it in GitHub Desktop.
Save PHPirates/9c0bfe55500dc9d63e140809235dbb0d to your computer and use it in GitHub Desktop.
Script to let Knitr generate LaTeX from R code
# author: Sten Wessel
# Instructions:
# Put this file on the same level as your LaTeX file
# Put your R scripts in a folder scripts/
# [IntelliJ] If you want to run your R scripts, make a run configuration as with working directory the folder this file is in
# In your R script, make comments like "## ---- part.a ----" to define sections
# In this file, edit the definition of R script content (the HOMEWORK.1 list)
# In the preamble in your LaTeX file, add the following:
#
# % Needed to let Knitr work with the exam document class
# \let\framed\relax
# \let\endframed\relax
# \let\shaded\relax
# \let\endshaded\relax
# \let\leftbar\relax
# \let\endleftbar\relax
#
# \input{knitr/preamble.tex}
#
# Make an empty directory knitr next to the scripts directory
# Make sure you have the knitr R library installed
# To generate the LaTeX, run this script
# Now include the genereated tex with \input{knitr/modeling-part.f.tex} and run the latex
#
# Tip: when using ggplot2, just do print(ggplot(...)) and knitr will generate a pdf in knitr/fig/
# This print statement has to be top-level, it won't work in a function.
library(knitr)
OUTPUT.DIR = 'knitr/'
# String concat shorthand
p <- function(..., sep='') {
paste(..., sep=sep, collapse=sep)
}
# Create temp Rnw file for knitr to process
fname <- "_knitr-compile-temp.Rnw"
file.create(fname)
f <- file(fname)
write("", f, append=FALSE)
# Build LaTeX preamble
writeLines(c(
"<<create-preamble, echo=FALSE, results='asis'>>=",
"cat(knitr:::make_header_latex())",
"@"
), f)
knit(fname, output=p(OUTPUT.DIR, 'preamble.tex'))
write("", f, append=FALSE)
# Definitions of R script content (which labels to process)
HOMEWORK.1 <- list(
prefix = 'homework-', # This will be prefixed to the generated tex filename
input = 'scripts/homework-1.R', # This is the R file that should be run and processed
chunks = list(
list(name='part.a'), # This should be the name of a part in the .R file: ## ---- part.a ----
list(name='part.c')
)
)
# Current definition to compile
COMPILE <- HOMEWORK.1
prefix <- COMPILE$prefix
for (chunk in COMPILE$chunks) {
# Read whole R script and display chunk
writeLines(c(
"<<external-code, cache=FALSE, echo=FALSE>>=",
p("read_chunk('", COMPILE$input, "')"),
"@",
p("<<", chunk$name, ", echo=TRUE, size='footnotesize', tidy=FALSE, out.width='.5\\\\textwidth', fig.align='center', fig.show='hide', fig.path='", OUTPUT.DIR, "fig/", prefix, "'>>="),
"@"
), f)
knit(fname, output=p(OUTPUT.DIR, prefix, chunk$name, '.tex'))
write("", f, append=FALSE)
}
close(f)
file.remove(fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment