Domanda

Following the thread "Add values to a reactive table in Shiny" by alexwhan:

Is there a way to avoid printing the first empty line?

I tried modifying values$df to values$df(-(1:1),) but that would print the first line in the table with index "2".

Thank you!

È stato utile?

Soluzione

The solution for me was to not create that first row, but instead to make a data.frame with empty rows. As an aside it also seems to be better to use indexing rather than 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})
  }))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top