【发布时间】:2022-12-16 17:46:30
【问题描述】:
我一直在使用操作按钮将列添加到 R Shiny 中一系列链接的 rhandsontable 表中。但是,现在,我正在尝试弄清楚如何通过单击操作按钮来生成额外的 rhandsontable 表模板。这可能吗?每个添加的表都需要独立于其他表,在允许用户输入和存储值到独立于其他表的添加表的意义上,尽管添加的“空白”新表可以共享一个公共基础模板形式。下面代码(uiTable1、hottable1)中显示的“基表”永远不能被用户删除。下面的代码显示了我开始的内容,但操作按钮还不起作用。
代码:
library(rhandsontable)
library(shiny)
rowNames1 <- c('A','B','C','Sum')
data1 <- data.frame(row.names = rowNames1, 'Col 1' = c(1,1,0,2), check.names = FALSE)
ui <- fluidPage(br(),
rHandsontableOutput('hottable1'),br(), # generates base table, can never be deleted by user
actionButton("addTbl", "Add table"),br() # adds table
)
server <- function(input, output) {
uiTable1 <- reactiveVal(data1) # base table can never be deleted by user
# records changes to base table and will need same for added tables:
observeEvent(input$hottable1,{uiTable1(hot_to_r(input$hottable1))})
output$hottable1 <- renderRHandsontable({
rhandsontable(uiTable1(),rowHeaderWidth = 100, useTypes = TRUE)
})
# counts nbr of tables added by user clicks of addTbl action button:
cntAdds = reactiveVal(0)
observeEvent(input$addTbl,{
cntAdds(cntAdds()+1)
})
# adds column summation to last row of table, will need for all added tables too:
observe({
req(input$hottable1)
DF <- hot_to_r(input$hottable1)
DF[setdiff(rowNames1, "Sum"),]
DF["Sum",] <- colSums(DF[setdiff(rowNames1, "Sum"),, drop = FALSE], na.rm = TRUE)
uiTable1(DF)
})
# Pending: observer adds new table
# observeEvent(input$addTbl, {
# newTbl1 <- data.frame(c(1,1,0,1))
# names(newTbl1) <- paste("Tbl", hot_to_r(cntAdds()))
# uiTable1(cbind(uiTable1(), newTbl1))
# })
}
shinyApp(ui,server)
【问题讨论】:
-
关于这个我建议看看shiny's modules。
标签: r shiny shiny-reactivity rhandsontable