闪亮的书签如何为动态生成的列表工作?
How does shiny bookmarking work for a dynamic generated list?
我有一个复杂的闪亮应用程序,我想在其中添加闪亮的书签功能。不幸的是,书签的一个关键参数是动态生成的。它是针对特定日期、位置和类型显示的测量值列表。
enableBookmarking 功能适用于静态列表但不适用于动态列表 (radiobuttons-list)。我玩过 onBookmark 和 onRestore 函数,但我不明白。可能还有其他问题(?).
编辑1:
再次阅读 onBookmark / onRestore 文档后,我认为没有必要在这里使用它。也许 observe{updateRadioButtons}-函数会覆盖 url-bookmarking-parameters?
这是我的问题的代表:
library(dplyr, warn.conflicts = FALSE)
library(lubridate)
library(shiny)
measurements <- tibble::tribble(
~epoch, ~location, ~type, ~x, ~y,
1612008000000, "A", "type1", 1, 1,
1612072800000, "B", "type2", 2, 2,
1612101600000, "A", "type2", 3, 3
)
ui <- function(request) {
fluidPage(
fluidRow(
column(
width = 2,
dateInput(inputId = "select_date", label = "Select Date", value = (Sys.Date() - lubridate::days(1)), weekstart = 1),
selectInput(inputId = "select_location", label = "Select Location", choices = c("A", "B"), selected = "B", multiple = FALSE),
selectInput(inputId = "select_type", label = "Select Type", choices = c("type1", "type2"), selected = "type1", multiple = FALSE)
),
column(
width = 10,
radioButtons(
inputId = "radio_measurements", label = h4("Measurements"),
choices = "",
selected = NULL
),
bookmarkButton()
)
)
)
}
server <- function(input, output, session) {
srv_epoch_start <- reactive(as.numeric(lubridate::as_datetime(input$select_date, tz = "GMT")) * 1000)
srv_epoch_end <- reactive(as.numeric(lubridate::as_datetime(input$select_date, tz = "GMT") + lubridate::days(1)) * 1000)
srv_loc_selected <- reactive(as.character(input$select_location))
srv_type_selected <- reactive(as.character(input$select_type))
## List radiobuttons ------------------------------------------------------------------------------------
list_radioMeasurements <- reactive({
measurements %>%
filter(epoch >= srv_epoch_start() & epoch < srv_epoch_end()) %>%
filter(location == srv_loc_selected()) %>%
filter(type == srv_type_selected()) %>%
pull(epoch)
})
observe({
updateRadioButtons(
session = session, inputId = "radio_measurements",
choices = list_radioMeasurements(),
selected = ""
)
})
}
# Bookmarking
enableBookmarking(store = "url")
shinyApp(ui, server)
静态 R Markdown 文档不支持闪亮的应用程序
由 reprex package (v1.0.0)
创建于 2021-02-01
当然,实际上我从数据库中获得了包含数据的测量列表,并且有很多针对不同表的观察事件,我。 e.也在测量单选按钮列表上,但这显然不是问题所在。如果你给我一个具体的提示,我会很高兴我该如何解决这个问题。
我找到了问题的答案。 onRestored() 是 运行 after 一切都恢复了 - 所以在生成动态单选按钮列表之后。
onRestored(function(state) {
radio_restore <- state$input$radio_measurements
updateRadioButtons(session, "radio_measurements", selected = radio_restore)
})
由 reprex package (v1.0.0)
创建于 2021-02-03
我有一个复杂的闪亮应用程序,我想在其中添加闪亮的书签功能。不幸的是,书签的一个关键参数是动态生成的。它是针对特定日期、位置和类型显示的测量值列表。
enableBookmarking 功能适用于静态列表但不适用于动态列表 (radiobuttons-list)。我玩过 onBookmark 和 onRestore 函数,但我不明白。可能还有其他问题(?).
编辑1: 再次阅读 onBookmark / onRestore 文档后,我认为没有必要在这里使用它。也许 observe{updateRadioButtons}-函数会覆盖 url-bookmarking-parameters?
这是我的问题的代表:
library(dplyr, warn.conflicts = FALSE)
library(lubridate)
library(shiny)
measurements <- tibble::tribble(
~epoch, ~location, ~type, ~x, ~y,
1612008000000, "A", "type1", 1, 1,
1612072800000, "B", "type2", 2, 2,
1612101600000, "A", "type2", 3, 3
)
ui <- function(request) {
fluidPage(
fluidRow(
column(
width = 2,
dateInput(inputId = "select_date", label = "Select Date", value = (Sys.Date() - lubridate::days(1)), weekstart = 1),
selectInput(inputId = "select_location", label = "Select Location", choices = c("A", "B"), selected = "B", multiple = FALSE),
selectInput(inputId = "select_type", label = "Select Type", choices = c("type1", "type2"), selected = "type1", multiple = FALSE)
),
column(
width = 10,
radioButtons(
inputId = "radio_measurements", label = h4("Measurements"),
choices = "",
selected = NULL
),
bookmarkButton()
)
)
)
}
server <- function(input, output, session) {
srv_epoch_start <- reactive(as.numeric(lubridate::as_datetime(input$select_date, tz = "GMT")) * 1000)
srv_epoch_end <- reactive(as.numeric(lubridate::as_datetime(input$select_date, tz = "GMT") + lubridate::days(1)) * 1000)
srv_loc_selected <- reactive(as.character(input$select_location))
srv_type_selected <- reactive(as.character(input$select_type))
## List radiobuttons ------------------------------------------------------------------------------------
list_radioMeasurements <- reactive({
measurements %>%
filter(epoch >= srv_epoch_start() & epoch < srv_epoch_end()) %>%
filter(location == srv_loc_selected()) %>%
filter(type == srv_type_selected()) %>%
pull(epoch)
})
observe({
updateRadioButtons(
session = session, inputId = "radio_measurements",
choices = list_radioMeasurements(),
selected = ""
)
})
}
# Bookmarking
enableBookmarking(store = "url")
shinyApp(ui, server)
静态 R Markdown 文档不支持闪亮的应用程序
由 reprex package (v1.0.0)
创建于 2021-02-01当然,实际上我从数据库中获得了包含数据的测量列表,并且有很多针对不同表的观察事件,我。 e.也在测量单选按钮列表上,但这显然不是问题所在。如果你给我一个具体的提示,我会很高兴我该如何解决这个问题。
我找到了问题的答案。 onRestored() 是 运行 after 一切都恢复了 - 所以在生成动态单选按钮列表之后。
onRestored(function(state) {
radio_restore <- state$input$radio_measurements
updateRadioButtons(session, "radio_measurements", selected = radio_restore)
})
由 reprex package (v1.0.0)
创建于 2021-02-03