'Object which is loaded at the start of the app is not found by a function inside the app
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.
Solution 1:[1]
Not sure if I'm right, but let's imagine this what described below could be a problem:
Object some_data lives in the child environment of environment, where lives function foo.
When thinking about shiny app, we can say that we have:
- Environment where lives objects from app.R
- Environment where lives objects from files which are in
R/directory. - Global Environment.
Object some_data is created (loaded) in app.R, so it will live in a child environment (from perspective of objects from R/ directory). And there is no access to objects from child environments - foo can't search there, so it won't find some_data. However, it can search in the same environment and in Global Environment. Please try this:
load("some_data.RData", envir = .GlobalEnv)
and let me know if now is OK. If you don't want to load data in Global Environment for some reason, we can think about other solution, but not sure now what will be possible.
I was inspired by this post: https://stackoverflow.com/a/70490294/13032723
Solution 2:[2]
@gss provided the correct solution above.
I tried the following and it also works, not sure why though:
foo <- function (data = NULL) {
d <- data
return(d)
}
library(shiny)
load("some_data.RData", envir = .GlobalEnv)
ui <- fluidPage(
textOutput("test")
)
server <- function(input, output, session) {
output$test <- renderText(foo(data = some_data))
}
shinyApp(ui = ui, server = server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gss |
| Solution 2 | Zello |
