Skip to content

Instantly share code, notes, and snippets.

@jpritikin
Last active December 27, 2015 00:09
Show Gist options
  • Save jpritikin/7235206 to your computer and use it in GitHub Desktop.
Save jpritikin/7235206 to your computer and use it in GitHub Desktop.
knitr doesn't generate the figure correctly when knit_hooks$set is enabled in the shell script.
#!/bin/sh
set -o errexit
set -o nounset
set -o noclobber
name=predis-sm
R --no-save --restore <<'EOF'
library(knitr)
opts_knit$set(error=TRUE)
opts_chunk$set(echo=FALSE, cache=TRUE)
if(1) {
knit_hooks$set(inline=function (x) {
if (any(is.na(x))) return(paste("\\textbf{TODO}\\todo{Sexpr evaluated to NA}"))
if (is.numeric(x))
x = round(x, getOption("digits"))
paste(as.character(x), collapse = ", ")
})
}
library(ggplot2)
update <- function(stem) {
rnw <- paste(stem,"Rnw",sep='.')
# if (file.info(rnw)$mtime > file.info(paste(stem,"tex",sep='.'))$mtime) {
knit(rnw)
# }
}
update("predis-sm")
EOF
[ -d gen ] || mkdir gen
pdflatex -shell-escape $name
\documentclass{article}
\usepackage[backgroundcolor=yellow,shadow,textwidth=1in]{todonotes}
\begin{document}
<<cache=FALSE>>=
if (length(ls())) rm(list=ls()) # new section
opts_chunk$set(fig.path="figure/intro/")
@
\begin{figure}
<<fig.height=3>>=
plot(sin)
@
\caption{Empirical response}
\end{figure}
Much work remains \Sexpr{NA}, but I have already begun to use OpenMx
\end{document}
@yihui
Copy link

yihui commented Oct 31, 2013

opts_knit$set(error=TRUE) should be opts_chunk$set(error=TRUE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment