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.