knitr: add to previous plot in new code chunk - r

I am using the knitr package for R to produce a LaTeX document combining text with embedded R plots and output.
It is common to write something like this:
We plot y vs x in a scatter plot and add the least squares line:
<<scatterplot>>=
plot(x, y)
fit <- lm(y~x)
abline(fit)
#
which works fine.
(For those not familiar with knitr or Sweave, this echos the code and output in a LaTeX verbatim environment and also adds the completed plot as a figure in the LaTeX document.)
But now I would like to write more detailed line-by-line commentary like:
First we plot y vs x with a scatterplot:
<<scatterplot>>=
plot(x, y)
#
Then we regress y on x and add the least squares line to the plot:
<<addline>>=
fit <- lm(y~x)
abline(fit)
#
The problem is that there are now two knitr code chunks for the same plot. The second code chunk addline fails because the plot frame created in the first code chunk scatterplot is not visible to the code in the second code chunk. The plotting window doesn't seem to be persistent from one code chunk to the next.
Is there any way that I can tell knit() to keep the plot window created by plot() active for the second code chunk?
If that is not possible, how else might I achieve LaTeX-style commentary on code lines that add to existing plots?
One Day Later
I can now see that essentially the same question has been asked before, see:
How to build a layered plot step by step using grid in knitr? from 2013 and
Splitting a plot call over multiple chunks from 2016.
Another question from 2013 is also very similar:
How to add elements to a plot using a knitr chunk without original markdown output?

You can set knitr::opts_knit$set(global.device = TRUE), which means all code chunks share the same global graphical device. A full example:
\documentclass{article}
\begin{document}
<<setup, include=FALSE>>=
knitr::opts_knit$set(global.device = TRUE)
#
First we plot y vs x with a scatterplot:
<<scatterplot>>=
x = rnorm(10); y = rnorm(10)
plot(x, y)
#
Then we regression y and x and add the least square line to the plot:
<<addline>>=
fit <- lm(y~x)
abline(fit)
#
\end{document}

You can show code without evaluating it by adding the chunk option eval=FALSE. If you only want to show the final version of the plot with the regression line added, then use eval=FALSE for the first plot(x,y).
Then we add two chunks for the regression line: One is the complete code needed to render the plot, but we don't want to display this code, because we don't want to repeat the plot(x,y) call. So we add a second chunk that we echo, to display the code, but don't evaluate.
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r data}
set.seed(10)
x=rnorm(10)
y=rnorm(10)
```
First we plot y vs x with a scatterplot:
```{r scatterplot, eval=FALSE}
plot(x, y)
```
Then we regress y on x and add the least squares line to the plot:
```{r addline, eval=FALSE}
fit <- lm(y~x)
abline(fit)
```
```{r echo=FALSE}
plot(x,y)
fit <- lm(y~x)
abline(fit)
```
Here's what the output document looks like:

Related

Print a plot I have saved in the environment anywhere in Rmarkdown

I create an Rmarkdown document where I would like to create a plot at the start of the document, and then print it at the end of the document.
I thought the best way to achieve this would be to save the plot in the environment and then recall it later, I save this as follows:
plot(1:5, 1:5) ; plot1 <- recordPlot() # I create a plot and save it as plot1
This plot is saved under "Data" in the environment.
If I enter plot1 into the console, my plot is reproduced, but when I try to display it directly in Rmarkdown as follows I get the following error:
plot(plot1)
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' is a list, but does not have components 'x' and 'y'
How I can take the plot that I saved into Data and print it anywhere I would like in my Rmarkdown document?
p.s. I know it's tempting to say to repeat the plot again later in the document, but the parameters that build the plot are subsequently altered for another part of my analysis.
Re-producible example:
x = 1
plot_later <- function() {
plot(x)
}
plot_later()
x = -10
plot_later()
X starts at 1 then changes to -10 on the Y axis, I want it to stay at the initial value of 1.
Solution based on https://bookdown.org/yihui/rmarkdown-cookbook/reuse-chunks.html :
---
title: plot now, render later
output: html_document
---
We put some plot expression here to evaluate it later:
```{r, deja-vu, eval=FALSE}
x = 1
plot(x)
```
Here we change `x` - but only within the corresponding chunk's scope:
```{r}
x = 10
```
... moving on
Here, we evaluate and plot the expression defined earlier; x is taken from that chunk's scope, so it still evaluates to `1`:
```{r, deja-vu, eval=TRUE}
```
One option could be saving the plot as grob object using as.grob function from ggplotify and then print it elsewhere.
---
title: "Saving A Plot"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r}
library(ggplotify)
library(grid)
show_captured_plot <- function(grb) {
grid::grid.newpage()
grid::grid.draw(grb)
}
```
```{r}
x <- 1
p <- as.grob(~plot(x))
```
Now we can plot the figure here.
```{r}
x <- 10
show_captured_plot(p)
```
here, check out this link: https://bookdown.org/yihui/rmarkdown-cookbook/fig-chunk.html
It has a lot of instructions on how to get started with rmarkdown.
Specifically answering your question:
We generate a plot in this code chunk but do not show it:
```{r cars-plot, dev='png', fig.show='hide'}
plot(cars)
```
After another paragraph, we introduce the plot:
![A nice plot.](`r knitr::fig_chunk('cars-plot', 'png')`)
Basically you have to save your graph as a variable and then call on it using the knitr::fig_chunk() function.

I have a for loop of qplots, how can you structure the plots individually using LaTeX code e.g adding captions to the plots

\begin{figure}[h]
\centering
<<echo=FALSE>>=
for(i in 2:38){
print(my_plot_function(i))
}
#
\end{figure}
This is my code, but what is happening is that when I compile my PDF I only get the first two plots that fit on the first page and I do not get the rest of the plots.
I would like to have all of the plots on separate pages.
And how would I go about adding captions to each individual plot in the for loop.
\documentclass{article}
\begin{document}
<<echo = FALSE, fig.cap = c("First, Second, Third"), results = "asis">>=
library(ggplot2)
for (i in 1:3) {
print(qplot(x = 1, y = i))
cat("Arbitrary \\LaTeX code! \\clearpage")
}
#
\end{document}
You can use the chunk option fig.cap to add captions to your plots. Note that this will wrap your figure in a figure environment, turning it into a float.
Use the chunk option results="asis" to be able to print arbitrary text (including LaTeX markup) into your document using cat().

Null output when printing heatmap.2 object in rmarkdown

I'm using rmarkdown via R-Studio and want to plot a heatmap by the heatmap.2. When I change the angle of column labels via the strCol option I get a NULL message printed before the heatmap in the output PDF file.
Attached a minimal code reproduce the problem:
{r, message=FALSE,warning=FALSE, echo=FALSE}
require(gplots)
data(mtcars)
x <- as.matrix(mtcars)
heatmap.2(x,srtCol=0)
The PDF look like
Is there any way to remove this NULL from the PDF output?
Try the following modification using capture.output. This did not print NULL for me.
```{r, message=FALSE,warning=FALSE, echo=FALSE}
require(gplots)
data(mtcars)
x <- as.matrix(mtcars)
res <- capture.output(heatmap.2(x,srtCol=0))
```
There may be a better way with some option to heatmap.2 but I didn't see it in the documentation. This was based off of the following SO post Suppress one command's output in R.

Reusing chunks in Knitr

I'm having lots of fun with Knitr but noticed I am reusing code in a bad way - cut and paste. In my example I want to load a dataset, calculate some statistics and print those, and plot the dataset -- easy to do with a few chunks, but if I want to do the same thing with another dataset I have to copy and paste the chunks and change only the name of the dataset.
Suppose I have something like this :
<p>Load the dataset <tt>dataset01</tt></p>
<!--begin.rcode load-dataset01
# Create an alias so there is no need to change it several times in the
# chunks
myDataset <- dataset01
a <- calcSomeStats(myDataset)
input <- myDataset[,1:2]
ideal <- class.ind(myDataset$label)
end.rcode-->
<p>Now let's plot it</p>
<!--begin.rcode plot-dataset01, fig.width=10, fig.height=10
neurons <- 1
NNET = nnet(input, ideal, size=neurons,softmax=TRUE)
plotnet(NNET)
par(pty="s",xpd=T, mar=par()$mar+c(0,0,0,2))
axis(1, at = seq(bbox[1],bbox[2], by = 2), las=1)
axis(2, at = seq(bbox[1],bbox[2], by = 2), las=2)
points(myDataset$x,myDataset$y,
col=myPal[unclass(myDataset$label)],cex=2,pch=16)
legend("topright", levels(factor(myDataset$label)),fill=myPal,inset=c(-0.1,0))
end.rcode-->
Code is not really complete, there are other parts that I am still developing, but it is working.
My question is, considering the two chunks shown as code above, which is the best (or Riest) way to reuse it? Suppose I have a list of dozens of datasets and I want to run the same chunks on them, is possible even substituting the non-R, HTML parts. Is it possible?
I've naively tried to create a function but since it starts with this:
<!--begin.rcode
abc <- function(n)
{
<!--begin.rcode howdoInamethischunkwithanuniquename
n <- n*2
end.rcode-->
}
end.rcode-->
it did not work (error: unexpected end of input)
thanks
Rafael
Edit: there are similar questions with answers in Using loops with knitr to produce multiple pdf reports... need a little help to get me over the hump and https://github.com/yihui/knitr/issues/435 but they cover LaTeX and/or R markdown, not HTML.
Another edit: things I've tried after #Yuhui comment:
Using the same label for both chunks
<!--begin.rcode chunkA, echo=TRUE, results='hide'
x <- rnorm(100)
end.rcode-->
<p>Plot it?</p>
<!--begin.rcode chunkA, echo=FALSE, results='markup'
mean(x)
end.rcode-->
With this I get the "Error in parse_block(g[-1], g[1], params.src) : duplicate label 'chunkA'" message.
Using chunk option ref.label
<!--begin.rcode chunkA, echo=TRUE, results='hide'
x <- rnorm(100)
end.rcode-->
<p>Plot it?</p>
<!--begin.rcode chunkB, ref.label='chunkA', echo=FALSE, results='markup'
mean(x)
end.rcode-->
With this I get the R code (x <- rnorm(100)), "Plot it?" and then nothing. Changing echo to TRUE just repeat (x <- rnorm(100)).
More information
My scenario is having several small data frames that have the same structure (x,y,label) and I want to process them in a chunk "A" and plot them with similar parameters in another chunk "B". If I do this without reusing code, I have to copy-and-paste chunks "A" and "B" several times, which is not a really good idea.
I know I cannot pass a parameter to a HTML chunk, and the recipes at http://yihui.name/knitr/demo/reference/ seems close to what I need, but I cannot figure out how to do them in R+HTML.
OK, I got it, and am posting this to serve as an example.
From what I understand, it is not possible to create a knitr chunk that works as a function. So, this is not possible:
<!--begin.rcode fakeFunction
# do something with myData, assume it is defined!
end.rcode-->
<!--begin.rcode myPlot1 ref.label='fakeFunction'
myData <- iris
# Assume fakeFunction will be executed somehow with iris
end.rcode-->
<!--begin.rcode myPlot2 ref.label='fakeFunction'
myData <- cars
# Assume fakeFunction will be executed somehow with cars
end.rcode-->
What will work is something like this:
<!--begin.rcode
myData <- iris
end.rcode-->
<!--begin.rcode plot
summary(myData)
end.rcode-->
<!--begin.rcode
myData <- cars
end.rcode-->
<!--begin.rcode plot2, ref.label='plot'
end.rcode-->
Basically we're saying that chunk plot2 will "paste" the code from chunk plot. We don't need to define anything else in plot2, and I guess it will be ignored anyway.
I haven't figured out a detail, though. Suppose I have the chunk plot working OK (imagine dozens of lines of R code) and want a slight different behavior in plot2, that would impact a single line of code. From what I understand I won't be able to do this with knitr -- anyone knows how to reuse code by writing chunks as procedures or functions?
I got similar ERRORs.
What I did was to name the chunk differently or not name them at all.
For example
{r, echo=F }
some code here
this is an example of a default code chunk without a name
{r setup, echo=F }
some code here
this is a chunk with name "setup."
Basically, if you have all chunk unnamed, or have all different named chunk, youll be fine.

Printing plots generated in a function using R Markdown in RStudio

I am trying to use the R-Markdown feature in R Studio where I was trying to print plots which are generated inside a function. This is a basic run down example of what I am trying to do.
**Test printing plots generated in a function**
================================================
``` {r fig.width=8, fig.height=4, warning=FALSE, eval=TRUE, message=FALSE, tidy=TRUE, dev='png', echo=FALSE, fig.show='hold', fig.align='center'}
dat <- data.frame(x=c(1:10),y=c(11:20),z=c(21:30),name=rep(c("a","b"),each=5))
library(ggplot2)
ex <- function(data){
plot(data[,1],data[,2])
plot(data[,1],data[,3])
}
for (i in 1:10){
t1 <- rbind(i,ex(dat))
}
t1
```
Those testing this code, please make sure to save it as ".Rmd" file and then run the knithtml() in RStudio toolbar. This code above works absolutely fine with the kind of html output I desire. However when I replace the base plotting function by ggplot based code, I cannot get the knithtml() to produce the ggplot output of the 10 plots that I got like before. The base plot code above is now replaced by the following code
p1 <- ggplot(data=data, aes(x=data[,1],y=data[,2]))
p1 <- p1+geom_point()
p1
Am I missing something very simple here.
VJ
There are two problems in your code:
ggplot doesn't recognize the data x and y data, bacause it works inside the data environment. You should give it the column names directly.
The code in yur loop doesn't make sense. You can't mix a plot with an index... (the reason it works with the base plot is through a side-effect) I've replaced it with the simple plot command.
The following will work:
**Test printing plots generated in a function**
================================================
``` {r fig.width=8, fig.height=4, warning=FALSE, eval=TRUE, message=FALSE, tidy=TRUE, dev='png', echo=FALSE, fig.show='hold', fig.align='center'}
dat <- data.frame(x=c(1:10),y=c(11:20),z=c(21:30),name=rep(c("a","b"),each=5))
library(ggplot2)
ex <- function(data){
p1 <- ggplot(data=data, aes(x=x,y=y))
p1 <- p1+geom_point()
return(p1)
}
for (i in 1:2){
plot(ex(dat))
}
```

Resources