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?