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)

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

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

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.

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.

EDIT: 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])
```

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

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.

Frank
  • 544
  • 2
  • 14
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – mathematical.coffee Oct 09 '16 at 23:29
  • I'm at a loss. I create a dummy data set and plug it into my function, and all of a sudden it works as expected in the markdown. I have no idea how what I'm doing for repro is different from my original code. It's the exact same formula, with same-formatted data plugged into it – Frank Oct 10 '16 at 00:26
  • Ahhh, that's the most frustrating kind of bug! What about if you run `knit` (rather than `render`) and examine the `md` file? If that works, then perhaps something about `render` (which is essentially `knit` and then `pandoc`) is mucking things up? Or then we can rule out rmarkdown and focus on knitr. – mathematical.coffee Oct 10 '16 at 00:31
  • So I dug in some more--I have a table above my plot that I build with `kable`, and when I comment the table out my markdown functions as expected. Further, if I add `kable` to me repro code mucks up any plot below it. Do I need a new question? – Frank Oct 10 '16 at 00:38
  • Nope, you seem to have identified your problem - could you update the question with a reproducible example? – mathematical.coffee Oct 10 '16 at 00:42

1 Answers1

1

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

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194