【问题标题】:Bookmarking Reactives in R Shiny在 R Shiny 中为反应物添加书签
【发布时间】:2017-02-13 18:08:26
【问题描述】:

我正在尝试找到一种保存响应式的方法,以便当我通过书签从保存状态加载时,我的响应式设置为我以前的任何值。请在下面找到 MWE:

library(shiny)

ui <- function(state){fluidPage(
  actionButton(inputId = 'LoadDataFromClippy',
               label = "Load data from clipboard"),
  bookmarkButton(),
  tableOutput(outputId = 'table')
)}


server <- function(input, output) {
  # Load data from clipboard
  LoadedData <- reactive({
    if(input$LoadDataFromClippy == 0){return()}
    input$LoadDataFromClippy
    return(read.table(file = "clipboard",header = FALSE))
  })

  # Render table for output
  output$table <- renderTable(({ LoadedData() }))

  # Bookmarking code --------------------------
  onBookmark(function(state) {
    state$values$LoadedData <- LoadedData()
  })

  onRestore(function(state) {
    LoadedData <- reactive({ state$values$LoadedData })
  })
}

# Run the application 
enableBookmarking(store = "server")
shinyApp(ui = ui, server = server)

当前行为

当您从书签链接重新加载时,它会将书签链接加载到表中。这表明它已经加载了操作按钮的状态(因此通常书签工作正常),但没有加载/立即将LoadedData() 覆盖到错误的东西。

期望的行为

加载我之前复制的任何内容。我想使用保存到服务器方法而不是保存到 URL,因为我的非最小示例是从我想保存为书签状态的外部文件加载。

我也希望这能帮助我保存 renderUI 对象。

我读过的东西

我读过这篇文章:https://shiny.rstudio.com/articles/advanced-bookmarking.html 这很好,但似乎没有从reactiveValues 跟进到reactives

最佳。

【问题讨论】:

  • 您可以选择使用reactiveValues() 吗?
  • 这真的能解决问题吗?我非常不愿意将每个响应式更改为响应式值,因为该应用程序的真实版本非常大。
  • 这就是我从链接文章中读到的内容,是的。对于反应性功能,我想不出可能的解决方案,也许 smbdy else 会找到 sthg

标签: r shiny bookmarks


【解决方案1】:

编辑:

  • uihttps://shiny.rstudio.com/articles/bookmarking-state.html)中使用function(request)
  • 创建reactiveValues来存储之前的状态;根据@BigDataScientist,状态书签目前似乎适用于reactiveValues
  • 本质上,从reactive 对象保存,加载到reactiveValues 对象
  • LoadData() 定义中,如果用户尚未从剪贴板复制,则分配先前的状态
library(shiny)

ui <- function(request) {
  fluidPage(
    actionButton(
      inputId = "LoadDataFromClippy",
      label = "Load data from clipboard"
    ),
    bookmarkButton(),
    tableOutput(outputId = "table")
  )
}


server <- function(input, output, session) {
  vals <- reactiveValues()
  # dummy value to initialize
  vals$sum <- NULL

  # Load data from clipboard
  LoadedData <- reactive({
    # return saved state if user hasn't copy from clipboard
    if (input$LoadDataFromClippy == 0) {
      return(vals$sum)
    }
    input$LoadDataFromClippy
    return(read.table(file = "clipboard", header = FALSE))
  })

  # Render table for output
  output$table <- renderTable(({
    LoadedData()
  }))

  # Bookmarking code --------------------------
  onBookmark(function(state) {
    state$values$LoadedData <- LoadedData()
  })

  onRestore(function(state) {
    vals$sum <- state$values$LoadedData
  })

  # Exclude the add button from bookmarking to avoid copying bookmark link to
  # clipboard
  setBookmarkExclude("LoadDataFromClippy")
}

# Run the application
enableBookmarking(store = "server")
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2017-11-30
    • 2019-04-07
    • 2018-11-28
    • 2020-02-12
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2021-06-28
    • 2017-10-21
    相关资源
    最近更新 更多