Steve Powell
Steve Powell

Reputation: 1698

knitr plots, labels, and captions within one chunk - .Rmd files

Jallen produced a solution for producing knitr plots, labels, and captions within one chunk - for Rnw files.

knitr plots, labels, and captions within one chunk

This works nicely for .Rnw but I can't make it work for .Rmd, don't see what is going wrong...

---
output:
  pdf_document:
    fig_caption: yes
    fig_crop: no
---

```{r startup,echo=FALSE,results='hide',message=FALSE,tidy=FALSE,warning=FALSE,fig.keep='all',comment=NA}
require(knitr)
require(ggplot2)
opts_knit$set(progress = F, verbose = F)
opts_chunk$set(comment=NA,
           tidy=FALSE,
           warning=FALSE, 
           message=FALSE, 
           echo=FALSE, 
           dpi=600,
           fig.width=6.75, fig.height=4, # Default figure widths
           dev=c("pdf",'tiff'),
           dev.args=list(pdf=list(NULL),tiff=list(compression='lzw')),
           error=FALSE)

```


```{r plotloop,results='asis'}
for(x in seq(1,20)){
  x1<-data.frame(x=seq(1,10),y=seq(1,10))
  plt<-ggplot(data=x1,aes(x,y))+geom_point()
  figLabel=paste('Figure',x,sep='')
  capt<-paste('Caption for fig.',x)
  cat(knit(text=(paste("```{r ",figLabel,",fig.pos='h',fig.cap='",capt,"'}\nplt\n```",sep=''))))
cat('\\newpage')

Upvotes: 2

Views: 1162

Answers (1)

JAllen
JAllen

Reputation: 638

plot.knit function in knitr plots, labels, and captions within one chunk can be modified to account for the syntax of markdown as opposed to latex. plot.knit.md becomes

plot.knit.md<-function(chunkLabel,#text for chunk label which is also used for figure file name
                    capt,#text for caption
                    plt,
                    ...)
  {
  cat(knit(text=knit_expand(text="```{r, {{chunkLabel}},eval=TRUE,fig.cap='{{capt}}',echo=FALSE}\nplt\n```"),
       quiet=TRUE))
  }

This works in the following markdown doc.

---
title: "plot.knit.md demo"
author: "Joel Allen"
date: "04/23/2015"
output:
  html_document: default
  pdf_document:
    fig_caption: yes
---

This is an R Markdown document to demonstrate the generation of self-contained code chunks in a markdown file.  It is particularly useful for situations where multiple plots are generated in a single code chunk allowing for dynamic label and caption support.

This example draws on 

http://stackoverflow.com/questions/21685885/knitr-plots-labels-and-captions-within-one-chunk

in response to 

https://stackoverflow.com/questions/27443019/knitr-plots-labels-and-captions-within-one-chunk-rmd-files

Items to note:

#. output pdf_document fig_caption option must be set to yes

#. plot.knit has been modified to plot.knit.md to account for the different syntax needed.

```{r, echo=FALSE,results='asis'}
plot.knit.md<-function(chunkLabel,#text for chunk label which is also used for figure file name
                    capt,#text for caption
                    plt,
                    ...)
  {
  cat(knit(text=knit_expand(text="```{r, {{chunkLabel}},eval=TRUE,fig.cap='{{capt}}',echo=FALSE}\nplt\n```"),
       quiet=TRUE))
  }
require(ggplot2)
cars.p<-ggplot(cars,aes(x=speed,y=dist))+
  geom_point()
plot.knit.md(chunkLabel="carsPlot",capt="this is a caption for the cars plot",plt=cars.p)
```

One thing to figure out though is the attachment of labels...

Upvotes: 2

Related Questions