Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created April 18, 2012 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/2414974 to your computer and use it in GitHub Desktop.
Save ramnathv/2414974 to your computer and use it in GitHub Desktop.
Looping Through a Data Frame with Sweave
# Generate PDF for a Pid
gen_pdf <- function(sdf){
rnw_file <- sprintf("%s.rnw", unique(sdf$Pid))
tex_file <- sub('.rnw$', '.tex$', rnw_file)
file.copy('template.Rnw', rnw_file)
Sweave(rnw_file)
tools::texi2pdf(tex_file, clean = TRUE, quiet = TRUE)
unlink(c(rnw_file, tex_file))
}
# Loop through all unique Pids
library(plyr)
d_ply(df, .(Pid), gen_pdf)
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\SweaveOpts{concordance=TRUE}
% Modify the text as needed. Note that sdf is a subset of the original
% data frame for a given Pid.
This is the report for \Sexpr{unique(sdf$Pid)}.
% Generate the plot
<<plot, fig = T, width = 5, height = 5>>=
plot(sdf$ksap1, sdf$ksap2)
@
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment