简体   繁体   English

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

[英]Add values to a reactive table in shiny

I want users of my shiny app to be able to add elements to a table iteratively, but I can't work out how to hold the values. 我希望我的闪亮应用程序的用户能够迭代地向表中添加元素,但我无法弄清楚如何保存值。

In this example, I want the user to be able to add values in the text boxes, which should get added to the bottom of the table in the main panel. 在此示例中,我希望用户能够在文本框中添加值,这些值应添加到主面板中表格的底部。 At the moment the previously added values are lost. 目前,之前添加的值已丢失。

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())})
  }))

I think you want to use reactiveValues() to store your data frame. 我想你想使用reactiveValues()来存储你的数据框。 Here is a possible solution: 这是一个可能的解决方案:

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})
}))

Edit 编辑

To avoid creating an empty row, create an empty data.frame rather than one with NA : 要避免创建空行,请创建一个空的data.frame而不是NA

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

And indexing seems better for adding the rows than rbind() (which messes up the column names... not sure why): 并且索引似乎比添加行更好,而不是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