Include plots but not console in R markdown - r

When I knit in R markdown, I not only get the nice plots I want to display but also little console displays like this above each and every plot:
## $`2FL`
## $`2FL`$Timepoint
I have tried include=FALSE, echo=FALSE, warning=FALSE, and message=FALSE to no avail.
When I try results='hide' I don't get the plots I want.
Does anyone know how to show only the plot output and not annoying little console things like this? I would have thought message=FALSE would work but like I said it didn't.
Thanks in advance.

Related

Code appearing in my RMarkdown PDF output despite using all code chunk instructions

I'm using R Studio and a RMarkdown file that I output as a PDF using Knitr.
There is some code that appears, even though I have set echo=false and comment=na for my code chunks.
The two code bits that appear in my PDF (and that I would want to hide) are :
I see this line over a graphic made with ggplot : ‘geom_smooth()‘ using formula = ’y ~ x’
I see this over all of different table() outputs : # A tibble: 8 x 3
Is there a way to hide that kind of code from my outputs? Am I missing something?
Thanks!
Just to make sure, I also added
{r echo=FALSE, comment=NA, MESSAGE=FALSE, INCLUDE=FALSE, WARNING=FALSE, ERROR=FALSE, RESULTS="hide"}
before my chunks, and yet, i'm still seeing these bits.

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

suppress console output in r markdown, but keep plot

Hi I have the following markdown chunk:
```{r, echo=FALSE,warning=FALSE,message=FALSE,error=FALSE}
lapply(obj,function(x) plot(x,main="some plot") box() axis(1,at=seq(0,25,by=1))
```
The output is multiple plots. However I also get the console message in the pdf document underneath the plots.
<Plot 1> nice plot 1!
<Plot 2> nice plot 2!
-- nasty horrible console output
## [[1]]
01.2882829
## [[2]]
120.29393933
I have tried echo/warning/error/message = FALSE, but neither of these suppress the console output
please help!
try this:
{r, echo=FALSE,results='hide',fig.keep='all'}
lapply(obj,function(x) plot(x,main="some plot") box() axis(1,at=seq(0,25,by=1))
Wrapping any object in invisible will prevent automatically printing it.
You should be able to use
invisible(lapply(obj,function(x) plot(x,main="some plot")))
However the fact that echo=FALSE doesn't work suggests that there might be something else going on.
These are the options that worked for me:
echo=FALSE, message=FALSE, results='hide'
I was having this problem as well in my R notebook and echo=FALSE didn't do anything. However message=FALSE does.
```{r, message=FALSE}
Try this,
It will hide the errors, warning, code, and console output. It will show only the graphs.
{r, echo=FALSE,warning=FALSE,message=FALSE,error=FALSE, results='hide',fig.keep='all'}
lapply(obj,function(x) plot(x,main="some plot") box() axis(1,at=seq(0,25,by=1))
Later you can export it to HTML which will be neat and readable
Simply having ```{r, results = 'hide'} or ```{r, results = FALSE} for your chunk options suppresses R output but not warnings, messages or errors. No extra functions are needed.
More details can be found here.
https://yihui.org/knitr/options/#text-output

preventing a chunk run in rmarkdown

I am using Rmarkdown and Knitr using Rstudio.
The following both print script and output to html.
```{r}
summary(cars)
```
However the following will only print the output, that is embedded plot.
```{r, echo=FALSE}
plot(cars)
```
I have situation different than above, I want to present the script but should not run in html as this will take very long time (hours if not days) to run. So I just did was put comment sign.
```{r}
#summary(cars)
```
But I need a better way to do this - Is there any better way presenting script without running it.
eval = FALSE
Checkout The R Markdown Cheat Sheet http://blog.rstudio.org/2014/08/01/the-r-markdown-cheat-sheet/
It summarizes the options for code chunks

How to add elements to a plot using a knitr chunk without original markdown output?

For documentary purposes, I want some code for a plot in the html-output, but not the plot. Later, I have to call the plotting code, and add something to the plot, but only seeing the additional code. I tried this:
```{r non.finished.plotting, eval=FALSE}
plot(1,type="n")
```
Some explanatory text here in the output:
"This produces an empty plot, and we could now add some points to it manually."
```{r add.layer, fig.width=5, fig.height=5}
<<non.finished.plotting, echo=FALSE>>
points(x=rnorm(100,1,0.1), y=rnorm(100,0.8,0.1) )
```
I have found the echo-notation at Yihui's, but when I knit this, I get an error message in the output.
## Error: plot.new has not been called yet
I also tried fiddling with the chunk options, but I could not find a combination which does what I want.
(Sorry, this is very basic, but I did not find something quite like this example.)
Chunk references in <<>> do not respect chunk options, so <<non.finished.plotting, echo=FALSE>> will not work. What you can do is to move the chunk option echo back to the main chunk like this:
```{r add.layer, fig.width=5, fig.height=5, echo=-1}
<<non.finished.plotting>>
points(x=rnorm(100,1,0.1), y=rnorm(100,0.8,0.1) )
```
echo=-1 means do not echo the first expression (as documented). This is probably what you want:

Resources