在运行底部的简化代码时,用户通过单击"添加表"按钮生成一系列rhandsontable
表,并通过单击每个表下面相应的"删除"按钮删除添加的表。基表或第一个表永远不能删除。如下图所示:我希望每个表的列标题反映其计数。(a)计算"净"点击次数actionButton()
(点击"添加表格"的次数减去点击任何"删除"按钮的次数),并将该结果值插入每个表格的标题中,或者(b)数table的数量。我更喜欢(b),但我也不知道怎么做。有什么建议吗?
说明:
代码:
library(rhandsontable)
library(shiny)
data1 <- data.frame(row.names = c("A","B","C","Sum"),"Col 1"=c(1,1,0,2),check.names=FALSE)
ui <- fluidPage(
br(),
actionButton("addTbl","Add table"),
br(),br(),
tags$div(id = "placeholder",
tags$div(
style = "display: inline-block",
rHandsontableOutput("hottable1")
)
)
)
server <- function(input, output) {
uiTbl1 <- reactiveValues(base = data1)
rv <- reactiveValues()
observeEvent(input$hottable1, {uiTbl1$base <- hot_to_r(input$hottable1)})
output$hottable1 <- renderRHandsontable({rhandsontable(uiTbl1$base, useTypes = TRUE)})
observeEvent(input$addTbl, {
divID <- gsub("\\.", "", format(Sys.time(), "%H%M%OS3")) # system time at add used as table ID
dtID <- paste0(divID, "DT")
btnID <- paste0(divID, "rmv")
uiTbl1[[paste0(divID,"tbl")]] <- data1 # captures initial dataframe values
insertUI(
selector = "#placeholder",
ui = tags$div(
id = divID,
style = "display:inline-block;",
rHandsontableOutput(dtID),
actionButton(btnID, "Delete", class = "pull-left btn btn-danger"),
)
)
output[[dtID]] <- renderRHandsontable({
req(uiTbl1[[paste0(divID,"tbl")]])
rhandsontable(uiTbl1[[paste0(divID,"tbl")]], useTypes = TRUE)
})
observeEvent(input[[btnID]],{
removeUI(selector = paste0("#", divID))
rv[[divID]] <- NULL
uiTbl1[[paste0(divID,"tbl")]] <- NULL
},
ignoreInit = TRUE,
once = TRUE
)
})
}
shinyApp(ui, server)
1条答案
按热度按时间bkhjykvo1#
我们需要另一个观察者来修改
uiTbl1
中的列名。请检查以下内容: