繁体   English   中英

将值添加到闪亮的响应表中

[英]Add values to a reactive table in shiny

我希望我的闪亮应用程序的用户能够迭代地向表中添加元素,但我无法弄清楚如何保存值。

在此示例中,我希望用户能够在文本框中添加值,这些值应添加到主面板中表格的底部。 目前,之前添加的值已丢失。

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) {
    tableStart <- data.frame(Column1 = NA, Column2 = NA)
    newEntry <- reactive({
      input$update
      newLine <- isolate(c(input$text1, input$text2))
      })
    output$table1 <- renderTable({rbind(tableStart, newEntry())})
  }))

我想你想使用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})
}))

编辑

要避免创建空行,请创建一个空的data.frame而不是NA

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

并且索引似乎比添加行更好,而不是rbind() (这会弄乱列名...不确定原因):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM