I've been using action buttons to a开发者_如何学Pythondd columns to a series of linked rhandsontable
tables in R Shiny. Now, however, I'm trying to figure out how to generate additional rhandsontable
table templates with the click of an action button. Is this possible? Each added table needs to be independent of the other tables, in the sense of allowing the user to input and store values into the added tables that are independent of the other tables although the "blank" new tables that are added can share a common base template form. The "base table" shown in the below code (uiTable1
, hottable1
) can never be deleted by a user. The below code shows what I started but the action button doesn't yet work.
Code:
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)
精彩评论