Including code block with error

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.

Other errors errors work well. Any advice?

Thanks,
Hans

Is this in a R Markdown document? Have you tried add a chunk option for error = TRUE, such as...

```{r error=TRUE}
stop("This code errors-out, but the script continues...")
```
1 Like

Thanks for the suggestion. Sorry, but I didn't notice that my code chunk options were not visible. Yes. I include "error=TRUE".

It works well for other errors, like object not found etc., but not for this.

Hmm, does that code chunk have any other options present? Have you tried results='asis', not sure if that would help at all.

1 Like

Thanks for the suggestion. No success so far.

Here is a complete MWE.

---
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

Thanks is advance

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.

# packages.R

library(learnr)
library(ggplot2)

Hi Matt,

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".

Best
Hans

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()
```
1 Like

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 :see_no_evil:

I still don't understand why it worked when chunk2 was excluded, but at least the issue is solved. Thanks a lot!

1 Like

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