When can I use a prefix created with `shiny::addResourcePath()` to access shared resources?

A learnr tutorial involves two primary contexts:

  1. The pre-render step that generates static content and prepares the tutorial content.
  2. The Shiny app step that runs the tutorial interactively for the user.

Since shiny::addResourcePath() is a function that has an affect on files and folders served by Shiny apps, it only affects the second context. In other words, you can expect

![](/images/rstudio_logo.png)

to work because the image is found by the user's browser when they interact with the running tutorial, after you've configured Shiny to make those images available at /images/.

OTOH, the examples in this R chunk do not work

```{r}
knitr::include_graphics("/images/rstudio_logo.png") # Does not work
test <- readr::read_file("/scripts/example_script.R") # Does not work
```

because, as standard R chunks, they are evaluated during the pre-render step just as if you were running those commands in your R console. In those cases ,you'll need to adjust the file paths to locate the image and script on your disk relative to the source Rmd file.

1 Like