【问题标题】:knitr plots, labels, and captions within one chunk - .Rmd files一大块内的 knitr 绘图、标签和标题 - .Rmd 文件
【发布时间】:2014-12-12 11:46:57
【问题描述】:

Jallen 提出了一种解决方案,用于在一个块中生成 knitr 图、标签和标题 - 用于 Rnw 文件。

knitr plots, labels, and captions within one chunk

这适用于 .Rnw,但我无法让它适用于 .Rmd,不知道出了什么问题...

---
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')

【问题讨论】:

    标签: r knitr


    【解决方案1】:

    knitr plots, labels, and captions within one chunk 中的 plot.knit 函数可以修改以解释 markdown 的语法,而不是 latex。 plot.knit.md 变为

    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))
      }
    

    这适用于以下降价文档。

    ---
    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)
    ```
    

    要弄清楚的一件事是标签的附件......

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2015-11-02
      • 1970-01-01
      • 2015-11-14
      • 2016-07-20
      • 1970-01-01
      • 2012-10-02
      • 2015-02-28
      • 1970-01-01
      相关资源
      最近更新 更多