flexdashboard/Shiny 中的简单刷新按钮? R
Simple refresh button in flexdashboard/Shiny? R
我有一个 google form/survey 将响应保存到 google sheet.
来自 google sheet 的数据正在进入我的 flexdashboard/shiny 应用程序,但如果有人在使用该应用程序时保存了响应 - 查看响应的唯一方法是实际上在浏览器中点击刷新。
我想添加一个无需用户单击刷新即可重新运行应用程序的按钮吗?这可能吗?
这是我编写的一些代码 - 我将一些代码放在一起但似乎不起作用。
---
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")))
)
```
如果不喜欢重置按钮,我们可以使用 invalidateLater(10000)
每 10 秒获取一次数据。
应用程序:
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)
第三种选择是在按下按钮时直接重新呈现数据:
代码:
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)
我有一个 google form/survey 将响应保存到 google sheet.
来自 google sheet 的数据正在进入我的 flexdashboard/shiny 应用程序,但如果有人在使用该应用程序时保存了响应 - 查看响应的唯一方法是实际上在浏览器中点击刷新。
我想添加一个无需用户单击刷新即可重新运行应用程序的按钮吗?这可能吗?
这是我编写的一些代码 - 我将一些代码放在一起但似乎不起作用。
---
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")))
)
```
如果不喜欢重置按钮,我们可以使用 invalidateLater(10000)
每 10 秒获取一次数据。
应用程序:
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)
第三种选择是在按下按钮时直接重新呈现数据:
代码:
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)