Simple refresh button in flexdashboard/Shiny? R

Viewed 241

I have a google form/survey that saves the responses into a google sheet.

Data from the google sheet is going into my flexdashboard/shiny app but if a response is saved while someone is using the app - the only way to see the response is by actually clicking refresh in the browser.

I would like to add a button that can rerun the application without the user clicking refresh? Is this possible?

Here is some code I have written - I have some code that I put together but it doesn't seem to work.

---
title: "Test "
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(DT)
library(gsheet)
```



Inputs {.sidebar data-width=300}
-----------------------------------------------------------------------

```{r}

jscode <- "shinyjs.refresh = function() { history.go(0); }"


actionButton(jscode, "Refresh")

observeEvent(input$reset, {

})

```

Column
-------------------------------------


### Chart A

```{r}

data <- gsheet2tbl('https://docs.google.com/spreadsheets/d/1CB2IQHb3g5d8KsEr-_lJNXPKnTBn2nhz_-AzBjRyRK4/edit#gid=0')

datatable(data
  ,
          editable = TRUE,
  options = list(
    columnDefs = list(list(className = 'dt-center', targets = "_all")))
)
```
1 Answers

We can use invalidateLater(10000) to fetch the data every 10 seconds if the reset button is not preferred.

app:

library(shiny)
library(shinyjs)
library(DT)
library(gsheet)

ui <- fluidPage(
  useShinyjs(),
  actionButton("reset", inputId = "Refresh"),
  DTOutput("table")
)

server <- function(input, output, session) {
  observeEvent(input$Refresh, {
    refresh()
  })

  data <- reactive({
    #invalidateLater(10000)
    gsheet2tbl("https://docs.google.com/spreadsheets/d/1CB2IQHb3g5d8KsEr-_lJNXPKnTBn2nhz_-AzBjRyRK4/edit#gid=0")
  })

  output$table <- renderDataTable({
    datatable(data(),
      editable = TRUE,
      options = list(
        columnDefs = list(list(className = "dt-center", targets = "_all"))
      )
    )
  })
}

shinyApp(ui, server)

A third alternative is to directly re-render the data when the button is pressed:

code:

library(shiny)
library(shinyjs)
library(DT)
library(gsheet)

data <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1CB2IQHb3g5d8KsEr-_lJNXPKnTBn2nhz_-AzBjRyRK4/edit#gid=0")

ui <- fluidPage(
  useShinyjs(),
  actionButton("reset", inputId = "Refresh"),
  DTOutput("table")
)

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


   observeEvent(input$Refresh, {
    
    df <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1CB2IQHb3g5d8KsEr-_lJNXPKnTBn2nhz_-AzBjRyRK4/edit#gid=0")
    
    output$table <- renderDT({
    datatable(df,
              editable = TRUE,
              options = list(
                  columnDefs = list(list(className = "dt-center", targets = "_all"))
              )
    )})
    
  })
  
  #this will render once at the start of the app
  output$table <- renderDataTable({
    
    datatable(data,
      editable = TRUE,
      options = list(
        columnDefs = list(list(className = "dt-center", targets = "_all"))
      )
    )
  })
}

shinyApp(ui, server)
Related