如果我设置echo = FALSE,R Markdown会使自定义图消失 [英] R Markdown makes custom plot disappear if I set echo=FALSE

查看:174
本文介绍了如果我设置echo = FALSE,R Markdown会使自定义图消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义函数,该函数将mfrow设置为nxn并创建n ^ 2个散点图,并根据数据帧的输入列表在每个图上具有多个数据集.我的绘图功能的签名看起来像这样:

I created a custom function which sets mfrow to nxn and creates n^2 scatter plots, with multiple data sets on each plot, based on an input list of data frames. The signature of my plotting function looks like this:

plot.return.list<-function(df.list,num.plot,title)

其中 df.list 是我的数据帧列表, num.plot 是要生成的绘图总数(用于设置 mfrow ) 和 title 是整体图标题(该函数为每个单独的子图生成标题).

Where df.list is my list of data frames, num.plot is the total number of plots to generate (used to set mfrow) and title is the overall plot title (the function generates titles for each individual sub-graph).

当我从控制台运行函数时,此创建的图很好.但是,我正在尝试使用RStudio将这个图形放入降价文档中,如下所示:

This creats plots fine when I run the function from the console. However, I'm trying to get this figure into a markdown document using RStudio, like so:

```{r, fig.height=6,fig.width=6} 
plot.return.list(f.1.list,4,bquote(atop("Numerical Approximations vs Exact Soltuions for "
,dot(x)==-1*x*(t))))
```

由于我尚未在我的 {r} 语句中设置 echo 选项,因此将打印绘图代码以及绘图本身.但是,如果我的第一行改为:

Since I haven't set the echo option in my {r} statement, this prints both the plotting code as well as the plot itself. However, if my first line instead reads:

{r, fig.height=6,fig.width=6,echo=FALSE} 

然后,代码和图将从最终文档中消失.

Then both the code AND the plot disappear from the final document.

如何在没有代码的情况下显示情节?根据RStudio提供的示例,设置 echo = FALSE 应该可以使绘图在没有代码的情况下出现,但这不是我正在观察的行为.

How do I make the plot appear WITHOUT the code? According to the example RStudio gives, setting echo=FALSE should make the plot appear without the code, but that isn't the behavior I'm observing.

编辑:我似乎已经将问题归结为 kable .不管我是否正在创建自定义的绘图助手功能,对 kable 的任何调用都会杀死我的绘图.可以在降价促销中复制它:

I seem to have tracked my problem down to kable. Whether or not I'm making a custom plot-helper function, any call to kable kills my plot. This can be reproduced in a markdown:

---
title: "repro"
author: "Frank Moore-Clingenpeel"
date: "October 9, 2016"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
options(default=TRUE)
repro.df<-data.frame((0.1*1:10)%*%t(1:10)) 
```
```{r, echo=FALSE}
kable(repro.df)
```
```{r, fig.height=6,fig.width=6,echo=FALSE} 
plot(repro.df[,1],repro.df[,2])
```

在此代码中,由于我将 echo 设置为false,因此不会绘制该图;删除标志使该图可见

In this code, the plot won't plot because I have echo set to false; removing the flag makes the plot visible

还请注意,在我的复制代码中, kable 生成了一个在最后一行包含一堆垃圾的表-我不知道为什么,但是对于我的完整原著来说并不正确代码,但我认为这与我的问题无关.

Also note that in my repro code, kable produces a table with a bunch of garbage in the last line--I don't know why, but this isn't true for my full original code, and I don't think it's related to my problem.

推荐答案

感谢可复制的示例.由此可见,问题在于您的表块和绘图块之间没有换行符.

Thanks for the reproducible example. From this I can see that the problem is you don't have a newline between your table chunk and your plot chunk.

如果您要编织并检查knit生成的MD文件(或将 html_document 设置为输出格式,并使用 keep_md:true 进行查看),您会看到表格代码和绘图代码之间没有任何换行符.Pandoc需要使用此来分隔表的末尾.没有它,它会认为您的![](path/to/image.png)是表的一部分,因此将其作为表中的垃圾线"而不是单独的图像

If you were to knit this and examine the MD file produced by knit (or set html_document as your output format and have keep_md: true to look at it), you would see that the table code and plot code are not separated by any newline. Pandoc needs this to delimit the end of the table. Without it, it thinks your ![](path/to/image.png) is part of the table and hence puts it as a "junk line" in the table rather than an image on its own.

只需在两个块之间添加换行符,就可以了.(表格必须用空白行包围.)

Just add a newline between the two chunks and you will be fine. (Tables need to be surrounded with blank lines).

((我知道您正在编译到LaTeX,所以可能会使您困惑为什么我要谈论markdown.到MD,然后 pandoc 从MD转到tex.这就是为什么您仍然需要确保降价看起来还可以).

(I know you are compiling to LaTeX so it may confuse you why I am talking about markdown. In case it does, when you do Rmd -> PDF, Rmarkdown uses knit to go from RMD to MD, and then pandoc to go from MD to tex. This is why you still need to make sure your markdown looks OK).

这篇关于如果我设置echo = FALSE,R Markdown会使自定义图消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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