I am running a Shiny app which is supposed to load some data and create a function which then uses this data. But I get an error.
I try to give a minimal example:
Before actually running the app, I save an object called "some_data":
some_data <- c(1,2,3,4)
save(some_data, file = here::here("mini/some_data.RData"))
I also save a function as foo.R in the "R" folder of the app's repository ("mini") (so Shiny will automatically run foo.R when the app starts:
foo <- function () {
d <- some_data
return(d)
}
Before starting the app, make sure the local environment is empty:
rm(list = ls())
The app looks like this:
library(shiny)
load("some_data.RData")
ui <- fluidPage(
textOutput("test")
)
server <- function(input, output, session) {
output$test <- renderText(foo())
}
shinyApp(ui = ui, server = server)
When running the app, I get the following error message:
Error: Objekt 'some_data' not found
What am I doing wrong?
Two additional comments on this: The above app will work, if I load the data by hand into my local environment before I run the app. What is more, if I don't use the function foo, the app will also work, so the data seems to be loaded correctly at the start of the app:
library(shiny)
load("some_data.RData")
ui <- fluidPage(
textOutput("test")
)
server <- function(input, output, session) {
output$test <- renderText(paste(some_data))
}
shinyApp(ui = ui, server = server)
Hope it is clear what I am trying to do. I really need the approach with the function.