编织器:for循环中每个.tabset个地块 [英] Knitr: one plot per .tabset in for-loop

查看:47
本文介绍了编织器:for循环中每个.tabset个地块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我正在尝试使用knitr动态创建单独的选项卡集并在每个选项卡集内绘制图形.以下是对我失败尝试的描述.我希望能找到一个解决方案,但我怀疑我只是不了解knitr呈现输出的基本方式.因此,如果您可以向我介绍资源或提供一些概念上的指导,也将不胜感激.

Long story short, I'm trying to use knitr to dynamically create separate tabsets and render a plot within each tabset. Below is a description of my failed attempts. I would appreciate a solution, but I suspect I'm just not understanding the fundamental way in which knitr renders output. So if you could point me to resources or provide some conceptual guidance, that would also be appreciated.

动态创建标签集本身很容易.这是我的.R文件

Dynamically creating the tabsets themselves is easy. Here is my .R file

#' ---
#' title: 
#' author: 
#' date: 
#' output:
#'    html_document
#' ---

#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
  cat('##',i,' \n')
}

当我在此.R文件中调用rmarkdown :: render()时,我得到10个空选项卡集(请参见下文).我不太确定猫是做什么的,也不知道为什么\ n是必需的(这是一些概念上的解释会被理解的地方),但是它确实有效. 现在,我将一个boxplot()调用添加到for循环中,并期望在每个选项卡集上获得相同的boxplot.

When I call rmarkdown::render() on this .R file, I get 10 empty tabsets (see below). I'm not really sure what cat does or why the \n is necessary (this is where some conceptual explanation would be appreciated), but it works. Now I add in a boxplot() call to the for-loop and expect to get the same boxplot on each tabset.

#' ---
#' title: 
#' author: 
#' date: 
#' output:
#'    html_document
#' ---

#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
  cat('##',i,' \n')
  boxplot(iris$Sepal.Length~iris$Species)
}

但是,第一个选项卡显示为空,第二个选项卡显示此输出呕吐物.

But instead, the first tab comes up empty and the second tab is this vomit of output.

如果我添加另一个带有两个"\ n"字符的cat(),则所有选项卡都会出现,但是只有第二个选项卡会填充图表.其他选项卡为空.

If I add another cat() with two "\n" characters, then all the tabs appear, but only the second tab is populated with the chart. The other tabs are empty.

#' ---
#' title: 
#' author: 
#' date: 
#' output:
#'    html_document
#' ---

#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
  cat('##',i,' \n')
  boxplot(iris$Sepal.Length~iris$Species)
  cat(' \n \n')
}

推荐答案

所以我找到了一个解决方案,但仍然不知道为什么为什么有效.将不胜感激任何反馈.在每个plot()工作之前添加plot.new().

So I found a solution, but still do not know why it works. Would appreciate any feedback. Adding plot.new() before each plot() works.

#' ---
#' title:
#' author: 
#' date: 
#' output:
#'    html_document
#' ---

#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
  cat('##',i,' \n')
  plot.new()
  boxplot(iris$Sepal.Length~iris$Species)
  cat(' \n \n')
}

这篇关于编织器:for循环中每个.tabset个地块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆