[R] Using rmarkdown with many plots created in a loop
Richard M. Heiberger
rmh @end|ng |rom temp|e@edu
Mon Aug 20 15:45:33 CEST 2018
## Don,
## This is how I would approach the task of a set of coordinated plots.
## I would place individual plots inside a table. The rows would index the
## datasets and there would be one or more data or description columns
## in addition to the column containing the graphs.
## I use the microplot package that I placed on CRAN about two years ago.
## install.packages("microplot") ## if necessary
library(microplot)
latexSetOptions()
## I normally use lattice. microplot also works with ggplot or base graphics
library(lattice)
## I placed your data into a single data.frame
myd <- lapply( 1:3,
function(i) list(df=data.frame(x=1:5, y=rnorm(5)),
comment=paste('Data', LETTERS[i]))
)
mydf <- cbind(group=rep(c("A", "B", "C"), each=5),
rbind(myd[[1]]$df, myd[[2]]$df, myd[[3]]$df))
mydf
## construct a lattice with multiple panels
my.lattice <-
xyplot(y ~ x | group, data=mydf,
layout=c(1,3), as.table=TRUE, col="black",
scales=list(alternating=FALSE),
ylab=list(rot=1))
my.lattice
## microplot provides latex.trellis, which is a method for Hmisc::latex
## simplest display
latex(my.lattice)
## now with comments and optional additional arguments
mycomments <-
c("Interesting Comment",
"Full \\LaTeX\\ with an equation $e^{-x^2}$",
"\\begin{tabular}{l}$\\frac{dy}{dx}$ \\\\is interesting \\\\and
has multiple lines\\end{tabular}")
latex(my.lattice,
title="Dataset",
height.panel=1, width.panel=1.5, ## inches
height.x.axis=.38, width.y.axis=.45,
graph.header="xyplot(y ~ x | group)",
dataobject=mycomments,
colheads=c("Comments", "", "", "xyplot( y \\~{} x )"),
caption="Very Interesting Caption",
caption.loc="bottom",
arraystretch=1.5)
## microplot produces MS Word tables as well as LaTeX tables.
## microplot works with ‘Sweave’, ‘knitr’, ‘emacs’ ‘orgmode’, and ‘rmarkdown’
## Start with ?microplot-package
## and look at the demos and examples and vignette.
## Rich
On Thu, Aug 16, 2018 at 7:44 PM, MacQueen, Don via R-help
<r-help using r-project.org> wrote:
> I would appreciate some suggestions of a good way to prepare a report using rmarkdown,
> in which I loop through subsets of a data set, creating a plot of each subset, and interspersing
> among the figures some text relevant to each figure.
>
> One way is to have an R script write the rmd file, then render it.
> It works, but it's cumbersome and difficult to get the rmd syntax correct.
> I would very much appreciate suggestions for a better way.
>
> Reproducible example below.
>
> Thanks
> -Don
>
>
> Example data (other data structures could be used), and an example using this approach.
>
> myd <- lapply( 1:3,
> function(i) list(df=data.frame(x=1:5, y=rnorm(5)),
> comment=paste('Data', LETTERS[i]))
> )
>
> Example interactive review (details would change depending on data structure)
> (I would typically insert pauses when working interactively)
>
> for (i in 1:3) {
> cat(paste('Figure',i,'shows',myd[[i]]$comment),'\n')
> with(myd[[i]]$df , plot(x,y))
> mtext(myd[[i]]$comment)
> mtext( paste(nrow(myd[[i]]$df),'points'), adj=1)
> }
>
> Note that along with the data I've saved some comments relevant to each subset.
> I've calculated them in the example data, but in general they could be completely
> arbitrary and come from anywhere.
>
> Now I'd like to get the same plots and comments into a report prepared using rmarkdown.
> Here's one way, having the loop create an rmd file, then rendering it.
>
> ### example script begins
> library(rmarkdown)
>
> myf <- 'myd.rmd'
> sink(myf)
> cat('---
> title: Example
> ---
>
> Here are some figures with a comment appearing before each.\n\n'
> )
> sink()
>
> for (i in 1:3) {
> cat(paste('Figure',i,'comment:',myd[[i]]$comment),'\n', file=myf, append=TRUE)
>
> cat("
> ```{r echo=FALSE, fig.cap='",paste('fig',i),"caption.'}
> with(myd[[",i,"]]$df , plot(x,y))
> mtext(myd[[",i,"]]$comment)
> mtext( paste(nrow(myd[[",i,"]]$df),'points'), adj=1)
> ```
> ", file=myf, append=TRUE)
>
> }
>
> cat('Done with report\n', file=myf, append=TRUE)
>
> render(myf)
>
> --
> Don MacQueen
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
> Lab cell 925-724-7509
>
>
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list