Rmarkdown displays a plot when its not supposed to

Quick Problem Statement :
R markdown is displaying a plot that I'm not calling in my code. It seems to magically appear out of nowhere.

Background:
I'm using R markdown to display a heatmap I create using ggplot2+plotly, in my report.
The report shows this heatmap correctly.

Here is my Rmarkdown code inside a Rmd file call heatmap.Rmd
...

summarise.heatmap(inpt1, inpt2)
```                                                                                                                                          ....(1)
 ```{r echo=FALSE, results = 'asis', message=FALSE}                                        ....(2)
knitr::knit_child('child_report.Rmd', envir=environment(), quiet = TRUE)

In the above code snippet, function summarise.heatmap() outputs a ggplotly object. I've shown the function below. The code then proceeds to call a template Rmd file called child_report.Rmd.

Here is the summarise.heatmap function. (My work prohibits me from showing my original code for security reasons so here is a snippet)

summarise.heatmap <- function(inpt1,inpt2){
...
htmap <- ggplot(data=scores, aes(x=metric,y=Split,fill=dy,text=paste0(round(dy,1)," mV"))) +
geom_tile() +
facet_grid(~Deck, scales = 'free') +
scale_fill_distiller(palette="RdYlBu") +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())

ggplotly(htmap, tooltip="text")

}
The above function is sourced inside the heatmap.rmd file and works correctly.

Finally the heatmap.Rmd is called inside another R script as follows:
rmarkdown::render("heatmap.Rmd",
output_file = paste0(results.folder,
"heatmap_",
inpt1,"-",
inpt2,
".html"))

Here is the heatmap as seen in my rmarkdown report

Problem Statement:
Rmarkdown report in addition to the correct heatmap shows another heatmap that I'm not calling anywhere AT ALL in my code. Report displays this wrong heatmap right after code lines (1) and before (2) shown above.

This second heatmap was generated as part of my development process. So its loaded in memory somewhere. I do not understand how Rmarkdown prints this old heatmap even though there is no call for this in Rmarkdown code.

Interestingly when I click on the "knit" button in my Rstudio, I dont see this issue. In other words, only the correct heatmap that I call in my code, is generated. So it is somehow related to rmarkdown::render call from my R script (shown above)

What I've tried so far :

  • Since the knit button works but the render call shows this issue, I'm strongly inclined to think this is a bug.
  • However I tried printing the subsequent lines of code shown in (2). They seem to be working correctly.

Need help

1 Like

As a hint, here is the difference without the two :

  • Clicking the Knit button will call rmarkdown::render but in a new clean session in the background
  • Calling render directly in the console of the current RSession will mean that the code chunks will be executed in the same working environment, same context.

You can try running render in a new session

callr::r(function(inpt1, inpt2) { 
  rmarkdown::render("heatmap.Rmd",
      output_file = paste0(results.folder,
          "heatmap_",
          inpt1,"-",
          inpt2,
          ".html"))
}, args  = list(inpt1 = inpt1, inpt2 = inpt2))
```

Assuming you have inpt1 and inpt2 in you workspace.

Without looking at the code, I can't say why there is this plot, but it could definitely come from some object in your workspace.

Thank you for the response. Good to understand this difference.

I found the issue. I added a line :
donotdisplay <- device.off()
after line (1) and before line (2) in my heatmap.Rmd code. And this weird plot stopped showing.

I do not understand why the plot in RSession's memory was getting plotted randomly before. But switching off the device worked. So I'd still say this is a bug?? I will try to mock up a small separate code to replicate this issue.

But, thanks again!
Pradeep

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.