【问题标题】:Follow up to "add values to a reactive table in Shiny"跟进“向 Shiny 中的反应式表添加值”
【发布时间】:2014-04-24 23:49:36
【问题描述】:

关注 alexwhan 的“向 Shiny 中的反应式表添加值”线程:

有没有办法避免打印第一个空行?

我尝试将values$df 修改为values$df(-(1:1),),但这会打印表中索引为“2”的第一行。

谢谢!

【问题讨论】:

    标签: r dataframe shiny


    【解决方案1】:

    我的解决方案是不创建第一行,而是创建一个带有空行的 data.frame。顺便说一句,使用索引似乎比rbind() 更好:

    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()
        #Create 0 row data.frame
        values$df <- data.frame(Column1 = numeric(0), Column2 = numeric(0))
        newEntry <- observe({
          if(input$update > 0) {
            isolate(values$df[nrow(values$df) + 1,] <- c(input$text1, input$text2))
          }
        })
        output$table1 <- renderTable({values$df})
      }))
    

    【讨论】:

    • 很好的解决方案!谢谢!
    猜你喜欢
    • 2020-02-12
    • 2016-04-28
    • 2017-08-22
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多