我正在尝试实现帖子中答案中的“保存”功能如何在 R Shiny 中使用 DT 的 localStorage 选项?进入我用 rhandsontable 渲染的表,但它不起作用。那篇文章涉及表包 DT,而我正在使用 rhandsontable,并且需要坚持使用 rhandsontable。我所说的“保存”是指保留该表及其从一个会话到下一个会话的所有累积输入/输出,这就是所引用的帖子对 DT 表所做的操作。稍后我需要实现该帖子中的“清除”功能,但首先我想看看“保存”是如何工作的,以及我在下面的尝试中做错了什么,然后再继续调整“清除”功能。
下面的代码有注释# add...
对于我从参考帖子中提取的功能。
如何在此 rhandsontable 示例中启用保存功能?
我收到以下错误消息:错误:无法在反应性消费者之外访问反应性值“hottable”。你需要包裹在reactive()或observer()中吗?
Code:
# 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)