2

I have a shiny app that outputs data from a data.frame imported with rdrop2 from my personal Dropbox.

The .csv file in my Dropbox from which the data.frame is built updates itself regularly. Therefore, I would like my Shiny App to regularly reload the .csv file and to update the data outputs if the values have changed.

I've already read these two posts: How to trigger a data refresh in shiny? and Update a data frame in shiny server.R without restarting the App, but I really can't make it happen entirely.

For now, I have this code, which seems to do the job, the only thing is that it updates the outputed data all the time, not only if it has changed:

shinyServer(function(input,output,session){
 sourceData <- reactive({
 invalidateLater(1000,session)
 loadData()
 })

 output$text1 <- reactive({
 df<-sourceData()
 df$SEX[2]
 })
})

The loadData() function is :

loadData <- function(){  
filesInfo <- drop_dir("J-Stats")
filePaths <- filesInfo$path[2]
data <- lapply(filePaths, drop_read_csv, stringsAsFactors = FALSE)
df <- do.call(rbind, data)
df
}

I've also made attempts with reactiveValues, without success. Basically, I would like the users of this app to see values that aren't flicking all the time, but just update when the app notices a change while it reloads the data "in the background".

Thanks a lot

Community
  • 1
  • 1
pollytatouin
  • 81
  • 1
  • 6
  • Can you tell if the values have changed in the file by looking at some timestamp somewhere? Or you may have to just download it every n seconds (using `invalidateLater`), and do the comparison in R, but only update the dataframe in shiny once you've detected a change. – cory Mar 14 '16 at 15:19
  • As you can see in my code, I have indeed used `invalidateLater` but I can't manage to have my app update the data only if the values have changed in the data frame. – pollytatouin Mar 14 '16 at 15:39
  • Yes, but I assume you are just updating the dataframe that shiny is using. I would instead, do the comparison between the new data and the current data and only update the one shiny is using if there's a change. – cory Mar 14 '16 at 15:41
  • Ok I see, and how would you code that ? – pollytatouin Mar 14 '16 at 15:43
  • Or are you looking for new files... then just save the length of `filePaths`, run the `invalidateLater` as usual, but if the length of `filePaths` is larger, then update the data. – cory Mar 14 '16 at 15:43
  • When the file in the dropbox changes, it will be overwritten so there won't be any new file. Same file, different content – pollytatouin Mar 14 '16 at 15:53
  • You can probably do something like what editing a question is like on Stackoverflow. When the data changes, instead of reloading the file, just popup a banner that says "file has changed, click here to reload". – Xiongbing Jin Mar 15 '16 at 00:21
  • This is an option indeed. A bit unsatisfying because I would have liked that the users do not need to do anything, but it's worth a try ! – pollytatouin Mar 15 '16 at 12:25

0 Answers0