【发布时间】: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