I update my RData file on daily basis through a cron job for my shiny apps. However, shiny apps does not pick updates most of the time and keep showing the old data from old
There is actually a function called reactiveFileReader
in the shiny
package that does exactly what you are looking for: Periodically checking if the files "last modified" time or size changed and rereading accordingly. However, this function can only be used in the server
context, so the file will be read at least once for each user that connects to your app. Options 3 and 4 in my Answer do not have these inefficiencies.
First and foremost, shiny does not have a way to keep track of filechanges AFAIK. Your implementation reloads the .RData
file whenever
shiny-server
gets restarted via bash orThere is no way of telling, when the second condition is met. Therefore, I would advocate using one of the following four options. Sorted from easy to you better know your shiny!.
Here, the image is reloaded whenever a new user connects with the app. However, this might slow down your app if your .RData
file is huge. If speed is not an issue, I would pick this solution since it is easy and clean.
# server.R
function(input, output, session) {
load("working_dataset.RData")
...
}
The data will also be reread whenever a user refreshes the page (F5)
(Also see @shosacos answer). This forces the .Rdata
file to be reloaded.
$ sudo systemctl restart shiny-server
Again, this might slow-down your production process depending on the complecity of your app. One advantage of this approach is that you can also use the imported data to build the ui if you load the data in global.R
. (I assume you don't given the code you gave).
The idea here is to check whether the .RData
has changed whenever a user connects to the app. To do this, you will have to use a "global" variable that contains a timestamp of the last imported version. The following code is untested, but should give you an idea on how to implement this feature.
# server.R
last_importet_timestamp <- reactiveVal("")
function(input,output,session){
current_timestamp <- file.info(rdata_path)$mtime
if(last_importet_timestamp() != current_timestamp){
# use parent.frame(2) to make data available in other sessions
load(rdata_path, envir = parent.fame(2))
# update last_importet_timestamp
last_importet_timestamp(current_timestamp)
}
...
}
Speed-wise, this should be more efficient than the first two versions. The data is never imported more than once per timestamp (unless shiny server gets restarted or becomes idle).
Basically, the same as option 3 but the file will be checked for changes every 50ms. Here is a full working example of this approach. Note that the data is not loaded unless a change in "last modified" is detected, so the resulting overhead is not too bad.
library(shiny)
globalVars <- reactiveValues()
rdata_path = "working_dataset.RData"
server <- function(input, output, session){
observe({
text = input$text_in
save(text = text, file = rdata_path, compress = TRUE)
})
observe({
invalidateLater(50, session)
req(file.exists(rdata_path))
modified <- file.info(rdata_path)$mtime
imported <- isolate(globalVars$last_imported)
if(!identical(imported, modified)){
tmpenv <- new.env()
load(rdata_path, envir = tmpenv)
globalVars$workspace <- tmpenv
globalVars$last_imported <- modified
}
})
output$text_out <- renderText({
globalVars$workspace$text
})
}
ui <- fluidPage(
textInput("text_in", "enter some text to save in Rdata", "default text"),
textOutput("text_out")
)
shinyApp(ui, server)
If you find it inconvenient to use globalVars$workspace$text
, you can use with
to access the contents of globalVars$workspace
directly.
output$text_out <- renderText({
with(globalVars$workspace, {
paste(text, "suffix")
})
})
The app is cached by shiny server and it does not update the app unless a change is detected in one of .R files associated with your shiny app. A hack is to make a subtle change in your app code (e.g. switch the loading order of libraries) to prompt shiny server to reset the cache and thereby include your updated .RData file.
It works if you restart your shiny server after the new *.RData is processed. I wouldn't put it into the shiny server because then
1) Its variables are not visible to the UI
2) Every user that opens the app needs to wait for RData loading to finish.