Dear Don, Have a look at the knit_expand() function. Then you can create two Rmd files. One main file and one template file for the subsets. knit_expand() will find and replace anything between double curly brackets ("{{x}}" in the example below) with the value of the variable.
The main file: --- title: "main" output: html_document --- ```{r setup, include=FALSE} library(knitr) ``` ```{r} mydf <- data.frame( id = 1:4, x = rnorm(100), y = rnorm(100) ) ``` ```{r} rmd <- sapply( 1:4, function(x) { knit_expand("child.Rmd", x = x) } ) rmd <- paste(rmd, collapse = "\n") cat(rmd) ``` ```{r results = "asis"} rendered <- knit(text = rmd, quiet = TRUE) cat(rendered, sep = "\n") ``` The child.Rmd file ## ID {{x}} ```{r, fig.cap = "The caption: ID = {{x}}", echo = FALSE} i <- {{x}} detail <- subset(mydf, id == i) plot(x ~ y, data = detail) ``` Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkel...@inbo.be Havenlaan 88 bus 73, 1000 Brussel www.inbo.be /////////////////////////////////////////////////////////////////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey /////////////////////////////////////////////////////////////////////////////////////////// <https://www.inbo.be> 2018-08-17 1:44 GMT+02:00 MacQueen, Don via R-help <r-help@r-project.org>: > 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@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.