4

I am reading in real-time data from two urls into Shiny and display the last most updated 8 records. However, the data are not the most updated in Shiny,

i.e. displaying previous days' records, inconsistent with real-time records from the url,

unless I paste and open/refresh the url in my browser again. I wonder if this is a problem with cache, and how should I alter my code?

library(shiny)
shinyApp(
  ui <- fluidPage(
    column(3,
    selectInput("station", "Select station",
                c("a", "b")),
    tableOutput("table")
    )
  ),


  server <- function(input, output) {
    df <- eventReactive(input$station, {
     if (input$station == "a") {
      tail(read.csv("https://datagarrison.com/users/300234062103550/300234062107550/temp/Dawson_Creek__008.txt",
                       sep = "\t", skip = 2)[, c("Date_Time", "Rain_2440445_mm")], 8)


     } else {
    tail(read.csv("http://datagarrison.com/users/300234062103550/300234064336030/temp/10839071_004.txt",
             sep = "\t", skip = 2)[, c("Date_Time", "Rain_2007476_mm")], 8)
      }})
    output$table <- renderTable(
    df()
  )
})

Update: It turned out that it is the server itself experiencing the update issue instead of the code. However, the answer shows a helpful approach.

micstr
  • 5,080
  • 8
  • 48
  • 76
Jane
  • 579
  • 5
  • 17
  • Does the code update when you change the choice of the inputs? Say a is outdated, if you switch the input to b, does an updated table show up? – Chabo Jun 07 '18 at 19:07
  • @Chabo I have tried that, the updated table does not show up upon new click (table a or b). – Jane Jun 07 '18 at 19:51
  • Could you add a button? That would be the easy was to force the update to happen. – Chabo Jun 07 '18 at 20:13
  • I will explore that option. Would that be an action button? – Jane Jun 07 '18 at 20:35
  • I have updated my answer to show that approach – Chabo Jun 08 '18 at 20:55
  • Thank you, the code works great. However, it does not refresh the page and I am starting to wonder that there is some issue with the url, not the shiny app. I will now mark the question as solved. – Jane Jun 11 '18 at 16:53

1 Answers1

2

See http://shiny.rstudio.com/gallery/timer.html , using this we can continue to refresh shiny every second, therefore keeping up with any updates. Note I would clear your work session to make sure you are not reading in any hidden variables.

  server <- function(input, output,session) {

  ....

  output$table <- renderTable({
  invalidateLater(1000, session)
  df()
  })

 }

Note that output$table<- renderTable needs a ({ not just a ( to be considered reactive in shiny, your current code may be showing you previously created tables.

Here is your code forcing a refresh with an action button (note: replace the URL)

library(shiny)
 shinyApp(
  ui <- fluidPage(
    column(3,
       selectInput("station", "Select station",
                   c("a", "b")),
       tableOutput("table"),
       #add action button
       actionButton("button","Refresh")
  )
 ),


server <- function(input, output) {
#trigger rest of code based on button being pressed.
observeEvent(input$button,{

  if (input$station == "a") {
    df<-tail(read.csv("URL",sep = "\t", skip = 2)[, c("Date_Time", 
    "Rain_2440445_mm")], 8)  

 } else {
    df<-tail(read.csv("URL",sep = "\t", skip = 2)[, c("Date_Time", 
    "Rain_2007476_mm")], 8)

}#close if/else()



output$table <- renderTable({
  df
   }) # close renderTable()

  })#close observeEvent()
 } #close server
) #close shiny app
Chabo
  • 2,842
  • 3
  • 17
  • 32