Generating markdown html shows charts when running within Rstudio but not when via rmarkdown::render()?

I have a markdown file template.Rmd that processes some data and displays some charts. All works fine when I run the file from within Rstudio with the file open and selecting knit > knit to html.

But when I try to render via a script, the output is generated and saved to a directory, but when I open it only text is shown, no charts.

My template.Rmd file. I use backslashes \ to try to get around using 3 backticks for markdown in my doc Vs. on this posts page so the post renders more readable code blocks. (but those backslashes don't exist on my actual file):

---
title: "ModelMultipliers"
output: html_document
---

\```{r setup, include=FALSE}
knitr::opts_chunk$set(
	echo = FALSE,
	message = FALSE,
	warning = FALSE
)
pacman::p_load(tidyverse, lubridate, Metrics, foreach, ggpubr, DT)
code here
\```

\```{r preprocessing, include=FALSE}
code here
\```

# Blah

Some blocks of text here

\```{r explore, echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE}
# some code that generates some ggplots which are meant to be visible
\```
  
\```{r modeling, echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE}
# some more code that generates some ggplots which are meant to be visible
\```

\```{r plots, echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE}
# some code that generates some ggplots which are meant to be visible```
\```

If I run this within Rstudio UI, all is well, the desired html output is rendered and pops up after rendering. I see lots of charts.

But when I try to run via a script with the block below, only the text is shown, no charts:

library(tidyverse)
GAME <- Sys.getenv('GAME')
CURRDATE <- Sys.Date() %>% as.character()
rmarkdown::render("template.Rmd", 
                  output_format = "html_document",
                  output_file = paste0('_', CURRDATE, '_', GAME, '.html'),
                  output_dir = paste0('runs/', GAME)
                  )

The file is generated and shows in the right directory, just no visuals.

Why does rendering this way as opposed to the UI drop the visuals? Is that expected? How can I get around this so as when I call rmarkdown::render() the file is generated including the charts?

Are the plots in loops or something else ?

You can try to explicitly call print on the ggplot2 object. When it is inside for loops, knitr won't be able to call the printing for you when rendering. (It is working inside the IDE because it is evaluated differently).

Could it be for loops ?

This seemed to be unique to my environment because when I tried on docker it all worked soundly. I tried to delete this post (apparently I have that level of rights) but could not see how to :confused:

Tried clicking the 3 dots then the trash icon but get 'you don't have permission'. Either way my script has evolved and I cannot readily reproduce.

1 Like

To answer your q the plots were part of a list column and printed out using, if I recall a tidyr map func

1 Like