shinylive-r: cannot include R file

I am trying to use external code (data, functions, etc.) in a shinylive-r code chunk inside a Quarto document. The same code in a shiny app works but the shinylive-r code chunk results in a blank space.

1. Code that works in a shiny app.R file

source(
    paste0(here::here(), "/R/test-shiny.R"),
    local = TRUE
)
#> Warning in file(filename, "r", encoding = encoding): cannot open file
#> '/private/var/folders/sd/g6yc4rq1731__gh38rw8whvc0000gr/T/Rtmp2E6nLw/reprex-dc57115f4950-girly-unau/R/test-shiny.R':
#> No such file or directory
#> Error in file(filename, "r", encoding = encoding): cannot open the connection

library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)
#> Error: object 'msg1' not found

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)
#> Error: object 'ui' not found

Created on 2025-06-05 with reprex v2.1.1

Despite the warning of the reprex this code works inside a shiny app.

2. Code that works in a shinylive-r code chunk

msg1 <-  "What's your name? "
msg2 <-  "Hello "


library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:4689

Created on 2025-06-05 with reprex v2.1.1

3. Code that doesn't work in a shinylive-r code chunk

If I replace the first two lines of the previous shinelive-r code with the reference to an external R file, it does not work anymore.

source(
    paste0(here::here(), "/R/test-shiny.R"),
    local = TRUE
)
#> Warning in file(filename, "r", encoding = encoding): cannot open file
#> '/private/var/folders/sd/g6yc4rq1731__gh38rw8whvc0000gr/T/Rtmp2E6nLw/reprex-dc577a492a7-waspy-bunny/R/test-shiny.R':
#> No such file or directory
#> Error in file(filename, "r", encoding = encoding): cannot open the connection


library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)
#> Error: object 'msg1' not found

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)
#> Error: object 'ui' not found

Created on 2025-06-05 with reprex v2.1.1

In spite of the Reprex warning that there is no such file, the here::here() construction returns to the correct file. The warning from Reprex disappears if I fill in the absolute file name, but with the same result: a white empty canvas.

The content of "test-shiny.R" is:

msg1 <-  "What's your name? "
msg2 <-  "Hello "

Created on 2025-06-05 with reprex v2.1.1

My question: How can I refer to an external file inside a shinylive.r code chunk?

Everything in R/ gets sourced automatically, including in shinylive, so you should be okay if you just remove the source part entirely. In a shinylive app I've developed I've also got this which works fine: modules <- gsub("_module.R", "", list.files("R/", "*module*")) so it may also be that here::here() is unnecessary or causing a problem.

Edit: now I noticed you're trying to do this inside quarto - can you post a full example of the quarto document?

@Simon_Smart: Yes, I am trying to write a shiny app inside a quarto document.

My quarto documents is this:

---
title: "Shiny Source Test"
filters: 
    - shinylive
---


## Without external file (worked!)

```{shinylive-r}
#| standalone: true
#| viewerHeight: 300


msg1 <-  "What's your name? "
msg2 <-  "Hello "


library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)


```

## With external file (does not work)

```{shinylive-r}
#| standalone: true
#| viewerHeight: 300


source(
    paste0(here::here(), "/R/test-shiny.R"),
    local = TRUE
)


library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)


```

I tried it without the source() command as well, because I read that in the newest version, all first level files under the "R" directory will be loaded into the shinylive-r code chunk.

Maybe also of interest: I am using shinylive 0.3.0.9000. My Quarto installation (copied from the terminal after quarto check:

Quarto 1.8.4
[โœ“] Checking environment information...
Quarto cache location: /Users/petzi/Library/Caches/quarto
[โœ“] Checking versions of quarto binary dependencies...
Pandoc version 3.6.3: OK
Dart Sass version 1.87.0: OK
Deno version 2.3.1: OK
Typst version 0.13.0: OK
[โœ“] Checking versions of quarto dependencies......OK
[โœ“] Checking Quarto installation......OK
Version: 1.8.4
Path: /Applications/quarto/bin

[โœ“] Checking tools....................OK
TinyTeX: (not installed)
Chromium: (not installed)

[โœ“] Checking LaTeX....................OK
Using: Installation From Path
Path: /Library/TeX/texbin
Version: 2025

[โœ“] Checking Chrome Headless....................OK
Using: Chrome found on system
Path: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Source: MacOS known location

[โœ“] Checking basic markdown render....OK

[โœ“] Checking Python 3 installation....OK
Version: 3.10.9 (Conda)
Path: /Users/petzi/anaconda3/bin/python
Jupyter: 5.2.0
Kernels: python3

[โœ“] Checking Jupyter engine render....OK

(/) Checking R installation...........R version 4.5.0 (2025-04-11)
[โœ“] Checking R installation...........OK
Version: 4.5.0
Path: /Library/Frameworks/R.framework/Resources
LibPaths:
- /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library
- /Library/Frameworks/R.framework/Versions/4.5-arm64/library
knitr: 1.50
rmarkdown: 2.29

[โœ“] Checking Knitr engine render......OK

Thanks. here::here() points to /home/web_user/app_fyy7531oyzwa8xo5gqi5 and doing list.files(here::here()) only shows app.R so clearly the contents of R/ aren't being bundled like when a normal shinylive app is built. I noticed that there's an open issue asking the same thing in python: is there a way to utilize one or more python files in addition to the python that is included in the qmd file? ยท Issue #68 ยท quarto-ext/shinylive ยท GitHub

The only solution I can come up with is putting your R/ file on GitHub and sourcing it from there, e.g. this works:

#| standalone: true
#| viewerHeight: 300

library(shiny)

source("https://raw.githubusercontent.com/simon-smart88/disagapp/refs/heads/main/R/helper_functions.R")

ui <- fluidPage(
    textOutput("greeting")
)

server <- function(input, output, session) {
    output$greeting <- renderText({
      paste0(printVecAsis(c("a", "b", "c")))
    })
}

shinyApp(ui, server)


@Simon_Smart Thank you. Yes, an external URL works for me as well. It is strange that it also works without local = TRUE. But anyway, it is a good workaround!

But I think there is (or should) also another solution, referencing the file locally. Otherwise, it would be cumbersome with complex functions that must be modified several times. So, I won't close this posting, hoping other people will step in with different solutions.

To include files in a Shinylive app in a Quarto document, the files need to be included in the shinylive-r chunk using the ## file: {filename} comment header. In your case, this would look something like this:

```{shinylive-r}
#| standalone: true
#| viewerHeight: 300

source("R/test-shiny.R", local = TRUE)


library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)

## file: R/test-shiny.R
msg1 <-  "What's your name? "
msg2 <-  "Hello "
```

You can think of each shinylive cell as a complete, self-contained app, and all of the apps files need to live in the cell.

When you have multiple files, I find it helpful to take advantage of Quarto's {{< include >}} shortcode. For multi-file apps, I usually store the app source code, including the app.R file, in a subdirectory next to the post. This way, I can run the app directly outside of Quarto in the usual way during development, while also being able to bundle the app into the Quarto document with shinylive:

```{shinylive-r}
#| standalone: true
#| viewerHeight: 300

## file: app.R
{{< include demo-app/app.R >}}

## file: R/test-shiny.R
{{< include demo-app/R/test-shiny.R >}}
```
2 Likes

The second example solved my problem!!

But still there is a question that bothers me: Why does a shinylive-r chunk not work with the file comment header, the {{< include >}} shortcode AND the code of the shiny app in it?

For instance, the following code produces just a white pane:

```{shinylive-r}
#| standalone: true
#| viewerHeight: 300

## file: R/test-shiny.R
{{< include R/test-shiny.R >}}

library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)

```

Moreover, I did not understand your first example, because it works also without the source line and the file comment header. The last two lines with msg1/msg2 are enough, as they provide the values explicitly. Or do I miss something?

Your suggestion to store the app source code and to include both (app.R and other necessary files) with the {{< include >}} short code was very valuable for me! I struggled with the Shiny app development, because inside the Quarto document with shinylive.r I didn't get all the necessary error messages. Therefore, I did the Shiny development outside the Quarto document and then โ€” after it worked โ€” I copied the external code into the Quarto document. But your procedure is much simpler!

The ## file: header splits the chunk contents into files, so in your example, you're only creating one R/test-shiny.R file. You'd need something like this for it to work:

```{shinylive-r}
#| standalone: true
#| viewerHeight: 300

## file: R/test-shiny.R
{{< include R/test-shiny.R >}}

## file: app.R
library(shiny)

ui <- fluidPage(
    textInput("name",  msg1),
    textOutput("greeting"),
)

server <- function(input, output, session) {
    output$greeting <- renderText({
        paste0(msg2, input$name)
    })
}

shinyApp(ui, server)
```

shinylive does assume that code that appears before any file headers is app.R, though, so if you put the Shiny app code first, you wouldn't need the ## file: app.R bit.

That's true, it was just an example showing how to create multiple files in shinylive.

1 Like

Ah, now I understand! The examples in the docs are mostly in Python, a programming language I am not so comfortable with.

This topic was automatically closed 7 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.