Hi,
I would like to include a code block that returns an error because a "#" is missing (it is section about commenting code). So what I did is
```{r,error=TRUE}
This is a comment where I forgot the #
ggplot()
`` `
This works well locally, but not on shinyapps. I get an error message that the ggplot function is not found with line reference to a different code block.
---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE)
` ``
## Topic 1
` ``{r chunk1, exercise=TRUE}
ggplot()
` ``
## Topic 2
` ``{r chunk2,error=TRUE, include=TRUE,echo=TRUE,results='asis'}
This is a comment where I forgot the #
ggplot()
` ``
Works well locally. Pushing to shinyapps I get the message:
processing file: Untitled.Rmd
Quitting from lines 8-11 (Untitled.Rmd)
Error in value[3L] : there is no package called 'ggplot2'
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
Execution halted
Seems like the error isn't related to chunk2 at all, but in fact your setup chunk fails because the package ggplot2 is not installed when the shiny app is deployed. I've never deployed an R Markdown to shinyapps.io but it seems like you need to ensure ggplot2 gets installed during deployment.
Maybe try copying and pasting your library(...) calls into a vanilla R script and upload that with your deployment? For regular shiny apps (via .R files) this is how dependencies are identified and installed.
Thanks. That was also my first thought, but it works well once I remove chunk2. In the real application I have several code chunks between the two and it took me ages to figure out that chunk2 was the "cause".
I just tried my suggestion and it worked. I first tried uploading the RMarkdown document you sent me by itself and I got the same error you got. I then created a packages.R file that looked just like this:
library(learnr)
library(ggplot2)
And I included this file alongside the RMarkdown file when selecting which files to publish. The deployment messages showed the packages installing and the app worked fine, see here.
---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, echo=FALSE, include=FALSE}
library(learnr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE)
```
## Topic 1
```{r chunk1, exercise=TRUE}
ggplot()
```
## Topic 2
```{r chunk2,error=TRUE, include=TRUE,echo=TRUE,results='asis'}
This is a comment where I forgot the #
ggplot()
```
Big thanks! I got it to work now. including packages.R didn't help for me, but as you posted it worked for you I knew it had to. My bad: I had to update ggplot2
I still don't understand why it worked when chunk2 was excluded, but at least the issue is solved. Thanks a lot!