2

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)
  })
}
md1630
  • 841
  • 1
  • 10
  • 28

1 Answers1

2

Put the following in a file called global.R in your app's directory (same as server.R and ui.R), it will run once at the initialization of the app.

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
}
mlegge
  • 6,763
  • 3
  • 40
  • 67
  • is there a way to use `global.R` when the shiny is build from a rmarkdown file ? There is no `ui.R` and `server.R`, only a file .Rmd, with a yaml header than define the shiny (`runtime: shiny_prerendered`). I have a `global.R` in the same repertory, but it don't work. My objective is to load a dataframe when the shiny is launched. – demarsylvain Feb 11 '19 at 16:29
  • @demarsylvain just put the `myDF <- as.data.frame(...)` in a code chunk at the start of your document. – mlegge Feb 11 '19 at 16:32
  • I put `df <- fread(".....")` in the first chunk of the .Rmd, but when I try to use `df` in others chunks, I have the error "object df not found" ... – demarsylvain Feb 11 '19 at 16:35
  • https://stackoverflow.com/questions/54632339/r-shiny-dataset-load-in-a-first-chunk-doesnt-exist-in-a-second-chunk/54634013#54634013 – demarsylvain Feb 11 '19 at 16:35
  • @demarsylvain are you caching that code chunk? If so, remove the caching – mlegge Feb 11 '19 at 17:17
  • by caching, you mean `{r, cache = T}` ? It put this statement to FALSE in all my chunks. – demarsylvain Feb 11 '19 at 18:08
  • Yes, if that's already false then its something else. [Here](https://gist.github.com/McClellandLegge/647d7b78169269b6b213ea07f560055b) is a simple example where `df` is defined in one chunk, then used in another. If you can't make it work after seeing this, please post an example with a reproducible error -- we can't help you otherwise – mlegge Feb 11 '19 at 18:33