在R Shiny

时间:2017-02-13 18:08:26

标签: r shiny bookmarks

我正在尝试找到一种保存被动方式的方法,这样当我从保存的状态通过书签加载时,我的被动设置被设置为我以前的任何东西。请在下面找到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()错误的东西。

所需行为

加载我之前复制的内容。我想使用save to server方法而不是保存到URL,因为我的非最小例子是从我想要保存在书签状态的外部文件加载。

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

我读过的内容

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

贝斯茨。

1 个答案:

答案 0 :(得分:0)

编辑:

  • function(request)https://shiny.rstudio.com/articles/bookmarking-state.html)中使用ui
  • 创建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)