I'm building a shiny app, and I need to rebuild a dataframe that parses a json file every time the app opens (since the json file will change). Then, functions in the app will need to access this data frame. Putting the code just in front of server.R does not actually create the data frame. Another way would be to create a function that creates the data frame, and call that function every time I need the data frame, but that would re-create the data frame every time I need it.
Is there a way to create the data frame once, and save it in a variable name, that can be accessed by functions in the rest of the application when needed?
Right now my code in server.R follows this structure below. However, here the for loop that fills in myDF is never called. On the other hand, I don't want to put it in a function that is called every time I need myDF, recreating it every time. I'd just like to create myDF on start of app, and save it as myDF so that I can use it.
json_file <- "file.json"
json_data <- fromJSON(json_file)
myDF <- as.data.frame(ncol = ..., nrow = ...) #creates an empty data frame myDF
for (b in field_names) {
#code that fills in myDF
}
myFunc <- function(inputs) {
#a function that uses myDF
}
shinyServer(function(input, output, session) {
output$out1 <-renderText({
myFunc(input$inputs)
})
}