Frank Frank - 1 year ago 131
Markdown Question

R Markdown makes custom plot disappear if I set echo=FALSE

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:


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

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 "

Since I haven't set the
option in my
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.

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

EDIT: I seem to have tracked my problem down to
. Whether or not I'm making a custom plot-helper function, any call to
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)
```{r, echo=FALSE}
```{r, fig.height=6,fig.width=6,echo=FALSE}

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

Also note that in my repro code,
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.

Answer Source

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.

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

(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).