且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将值添加到SHINY中的反应表

更新时间:2023-11-30 20:09:46

我认为您希望使用reactiveValues()存储您的数据框。以下是可能的解决方案:

library(shiny)

runApp(list(
  ui=pageWithSidebar(headerPanel("Adding entries to table"),
                 sidebarPanel(textInput("text1", "Column 1"),
                              textInput("text2", "Column 2"),
                              actionButton("update", "Update Table")),
                 mainPanel(tableOutput("table1"))),
server=function(input, output, session) {
values <- reactiveValues()
values$df <- data.frame(Column1 = NA, Column2 = NA)
newEntry <- observe({
  if(input$update > 0) {
    newLine <- isolate(c(input$text1, input$text2))
    isolate(values$df <- rbind(values$df, newLine))
  }
})
output$table1 <- renderTable({values$df})
}))

编辑

若要避免创建空行,请创建一个空数据帧,而不是使用NA

values$df <- data.frame(Column1 = numeric(0), Column2 = numeric(0))

rbind()相比,索引似乎更适合添加行(这会弄乱列名.不确定原因):

isolate(values$df[nrow(values$df) + 1,] <- c(input$text1, input$text2))