Printing plots generated in a function using R Markdown in RStudio - r

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

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.

Multiple Plot outputs from a function in rmarkdown

I have a function that produces 2 chart objects and I'd like to use this function in an rmarkdown file and knit it so that I get an HTML report. The problem I'm facing is that when I use this function, I get an output that looks something like this:
Here "Plot1" and "Plot2" are the actual plots that get rendered. What changes to I need to make so that I only get the following?
Plot1
Plot2
Here is the code chunk from my R-markdown file.
```{r OverallGRP106_trended_fav, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, paged.print=TRUE}
trended_fav(psc_surv,"Overall","GRP106")
```
The return statement of function "trended_fav" is as follows:
return(list(plot(p4), plot(p1)))
I think you'll be ok if you assign the function's output to a variable and then call up the element of the variable separately, like so:
---
output: html_document
---
```{r setup, include=FALSE}
library(ggplot2)
trended_fav <- function() {
p4 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p1 <- ggplot(txhousing, aes(year, median)) + geom_point()
plot_list <- list(p4,p1)
return(plot_list)
}
a <- trended_fav()
```
## First report
Here it is:
```{r}
a[1]
```
## Second report
This one is even better:
```{r}
a[2]
```

How to embed plots into a tab in RMarkdown in a procedural fashion?

I can embed plots using just RMarkdown's {.tabset}
#### Heading {.tabset}
##### Subheading 1
```{r, echo=F}
df[[1]]
```
This produces individual tabs with the specified graphs (df is a list of graphs, calling df[[i]] produces a graph) in the preview pane (renders all the graphs inline in RStudio).
And I can generate just the tabs using a for loop.
```{r, results='asis', echo = FALSE}
for (i in 1:length(gg0)) {
cat("##### ",q$Subheading[i],"\n")
}
```
And this produces the desired output - the tabs with the names in the Subheading column.
However, I am stuck in trying to generate the graphs themselves using the for loop similar to how I did when I coded it manually.
Extending the above, I tried to generate the markdown that produced the initial output but the plot fails to generate (both in the inline markdown and preview).
```{r, results='asis', echo = FALSE}
for (i in 1:length(gg0)) {
cat("##### ",q$Subheading[i],"\n")
cat('```{r, echo=F} \n')
cat("gg0[[",i,"]]\n")
cat('``` \n')
}
```
Maybe I am missing a finer point regarding markdown? I have tried various patterns using cat (and even without)
I would prefer a RMarkdown solution but other solutions are just as welcome.
I played around a little and found a solution. You have to use print within the asis code chunk...
```{r}
library(ggplot2)
gg0 <- list()
gg0[[1]] <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
gg0[[2]] <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
gg0[[3]] <- ggplot(mtcars, aes(mpg, drat)) + geom_point()
headings <- c('hp','disp','drat')
```
#### Heading {.tabset}
```{r, results='asis', echo = FALSE}
for (i in 1:length(gg0)) {
cat("##### ",headings[i],"\n")
print(gg0[[i]])
cat('\n\n')
}
```
As an explanation, the cat command together with results='asis' produces the markdown code for a lower level headline and prints the ggplot graph afterwards. Since we used `{.tabset} in the parent headline, it creates the plots in separate tabs.
Adding both plot.new(), dev.off() inside the for loop solves the problem of adding all the figures in the last tab. See the complete solution here.

How do I parameterize template blocks in knitr?

Say I have the following code in knitr. How can I run it multiple times with different values of i?
```{r, echo=FALSE}
i<-0.1
```
### X,Y plot of Y=X+e where e is a standard normal distro: mean=0, sd=`r i`
```{r, echo=FALSE}
r<-rnorm(100,mean=0,sd=i)
x<-seq(0,1,length.out=100)
y<-x+r
plot(x,y)
```
EDIT:
As has been suggested ... I tried to do something like this: start a loop in an R code block, have a template in between and then close the loop -- R throws and error.
```{r, echo=FALSE}
for (i in 1:4) {
```
# bla
```{r, echo=FALSE}
}
```
What makes this question tricky is that not only the chunk content (the plot) must be repeated, but the heading as well. That's why we can neither simply reuse the chunk nor just loop over the plot command like
for (i in 1:3) { plot(rnorm(100, sd = i)) }
But it's almost that simple: We loop over the code that produces the plot and output the heading from inside the loop. This requires the chunk option results="asis" and cat to get verbatim markdown output:
```{r, echo=FALSE, results = "asis"}
sdVec <- c(0.1, 0.2, 0.3)
for (sd in sdVec) {
cat(sprintf("\n### X,Y plot of Y=X+e where e ~ N(0, %s)", sd))
r<-rnorm(100,mean=0,sd=sd)
x<-seq(0,1,length.out=100)
y<-x+r
plot(x,y)
}
```
See this answer for related issues.

Rstudio does not make plots with knit Word

I seem to have discovered an odd behaviour with the knit Word command in RStudio
This works:
```{r qplot, fig.width = 6, fig.height=6, message=FALSE}
library(ggplot2)
summary(cars)
qplot(speed, dist, data = cars) + geom_smooth()
````
this does not work
```{r q plot, fig.width = 6, fig.height=6, message=FALSE}
library(ggplot2)
summary(cars)
qplot(speed, dist, data = cars) + geom_smooth()
```
returning this message:
pandoc.exe: Could not find image `./test_files/figure-docx/q%20plot.png', skipping...
The issue seems to be with the name of the chunk (i.e. qplot vs. q plot). When there is a space in the chunk name the plot does not render.
It only seems to affect the rendering of Word documents. Rendering html works fine.
I'm using RStudio 0.98.1028 and R3.1.1 on windows 7.
Has anyone else encountered this behaviour?
update
a space after the chunk name also seems to elicit the same behaviour:
this does not work
```{r q_plot , fig.width = 6, fig.height=6, message=FALSE}
library(ggplot2)
summary(cars)
qplot(speed, dist, data = cars) + geom_smooth()
```
Posting the solution in case someone runs across this in future.
From Ben Bolker in the comments Avoid spaces and periods . in chunk labels and directory names as stated in the knitr documentation http://yihui.name/knitr/options.
This error only seems to affect making plots using knitWord. Code chunks with labels that contain spaces and that don't have plotting commands render normally. knitHTML also seems to work fine regardless of if chunk labels have a space or not.
# Let's make a plot
```{r ugly plot}
plot(btc_prices)
```
should apparently be
# Let's make a plot
```{r ugly_plot}
plot(btc_prices)
```
So no spaces... otherwise you'll waste hours googling and crying.

Resources