【问题标题】:How to implement shinyStore when using a table generated by the rhandsontable R package?使用rhandsontable R包生成的表时如何实现shinyStore?
【发布时间】:2022-11-03 18:36:24
【问题描述】:

我正在尝试将How to use the localStorage option for DT in R Shiny? 中的答案中的“保存”功能实现到我用 rhandsontable 呈现的表格中,但它不起作用。那篇文章涉及表包 DT,而我正在使用 rhandsontable 并且需要坚持使用 rhandsontable。通过“保存”,我的意思是保留从一个会话到下一个会话的所有累积输入/输出的表,引用的帖子为 DT 表所做的。稍后我将需要从该帖子中实现“清除”功能,但首先我想看看“保存”是如何工作的,以及我在下面的尝试中做错了什么,然后再继续调整“清除”功能。

下面的代码有 cmets # add... 用于我从参考帖子中提取的函数。

我将如何在这个 rhandsontable 示例中启用保存功能?

我收到以下错误消息:错误:无法在反应性消费者之外访问反应性值“hottable”。你需要包裹在reactive() 或observer() 里面吗?

代码:

# If not installed already, un-comment and run the below 3 lines to install shinyStore package:
# install.packages("devtools")
# library(devtools)
# install_github("trestletech/shinyStore")

library(rhandsontable)
library(shiny)
library(shinyStore)

myDF <- data.frame(x = c(1, 2, 3))

ui <- fluidPage(
  initStore("store", "shinyStore-ex1"), # add
  br(),
  fluidRow(
    column(6,
           actionButton('addCol','Add column'),
           actionButton("save", "Save", icon("save")), # add
           actionButton("clear", "Clear", icon("stop")) # add
    )
  ),
  br(),rHandsontableOutput('hottable')
)

server <- function(input, output, session) {
  EmptyTbl <- reactiveVal(myDF)
  
  rv <- reactiveValues(uiTable = hot_to_r(input$hottable)) # add
  
  observeEvent(input$hottable, {
    EmptyTbl(hot_to_r(input$hottable))
  })
  
  output$hottable <- renderRHandsontable({
    rhandsontable(EmptyTbl(),useTypes = FALSE)
  })
  
  observeEvent(input$addCol, {
    newCol <- data.frame(c(1, 2, 3))
    names(newCol) <- paste("Col", ncol(hot_to_r(input$hottable)) + 1)
    EmptyTbl(cbind(EmptyTbl(), newCol))
  })
  
  # add observeEvent() below:
  observeEvent(input$save,{
    updateStore(session,name = "uiTable",rv$uiTable)
  },ignoreInit = TRUE)
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shiny-reactivity rhandsontable shinystore


    【解决方案1】:

    请检查以下内容:

    # If not installed already, un-comment and run the below 3 lines to install shinyStore package:
    # install.packages("devtools")
    # library(devtools)
    # install_github("trestletech/shinyStore")
    
    library(rhandsontable)
    library(shiny)
    library(shinyStore)
    
    myDF <- data.frame(x = c(1, 2, 3))
    
    ui <- fluidPage(
      initStore("store", "shinyStore-ex1"),
      br(),
      fluidRow(column(
        6,
        actionButton('addCol', 'Add column'),
        actionButton("save", "Save", icon("save")),
        actionButton("clear", "Clear", icon("stop")) # add
      )),
      br(),
      rHandsontableOutput('hottable')
    )
    
    server <- function(input, output, session) {
      uiTable <- reactiveVal(myDF)
      
      output$hottable <- renderRHandsontable({
        rhandsontable(uiTable(), useTypes = FALSE)
      })
    
      observeEvent(input$hottable, {
        uiTable(hot_to_r(input$hottable))
      })
      
      observeEvent(input$addCol, {
        newCol <- data.frame(c(1, 2, 3))
        names(newCol) <-
          paste("Col", ncol(hot_to_r(input$hottable)) + 1)
        uiTable(cbind(uiTable(), newCol))
      })
      
      observeEvent(input$save, {
        updateStore(session, name = "uiTable", uiTable())
      }, ignoreInit = TRUE)
      
      observeEvent(input$clear, {
        # clear tracking table:
        uiTable(myDF)
        
        # clear shinyStore:
        updateStore(session, name = "uiTable", myDF)
      }, ignoreInit = TRUE)
      
      observeEvent(input$store$uiTable, {
        uiTable(as.data.frame(input$store$uiTable))
      })
    }
    
    shinyApp(ui, server)
    

    PS:作为一种替代方法,我们可以使用rhandsontable::set_data() 以与DT::replaceData 相同的方式来避免在基础表更改时通过renderRHandsontable 重新创建小部件。

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多