r - 一个 block 中的 knit 绘图、标签和标题 - .Rmd 文件

标签 r knitr

Jallen 开发了一种解决方案,用于在一个 block 内为 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')

最佳答案

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

这适用于以下 Markdown 文档。

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

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

关于r - 一个 block 中的 knit 绘图、标签和标题 - .Rmd 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27443019/

相关文章:

r - 获取最新记录,但如果最新记录为空,则获取最后一条最新记录

r - 添加缺失的时间值

r - 在 r 中绘制没有 google map API 的城市 map

r - 在代码块输出中显示 Python >>> 提示符

R Markdown - 更改 html 输出中的字体大小和字体类型

r - 在 Rmarkdown 中动态创建选项卡对 ggplot 不起作用,而对 plotly 起作用

r - Knitr:只有 block 输出是 html(其余部分保持 rmarkdown)

r - 当某些个体只知道 1 个 parent 时,在 R 中创建一个谱系?

R markdown asis 破坏了有效的 html 代码

r - 从注释数据库中选择注释的函数不起作用