I´m trying to add rows to a dataframe based on the widget selection when a button is clicked.
I´ve put the observeEvent
inside the renderTable
, but the app returns an error: cannot coerce class "c("Observer", "R6")" to a data.frame.
This is the test dataframe to add rows to:
> DF <- data.frame(matrix(c("A","B"), ncol = 2), stringsAsFactors = FALSE)
> colnames(DF) <- c("col1", "col2")
> DF
col1 col2
1 A B
And this is the shiny app code:
library(shiny)
DF <- data.frame(matrix(c("A","B"), ncol = 2), stringsAsFactors = FALSE)
colnames(DF) <- c("col1", "col2")
ui <- fluidPage(
titlePanel("Save input"),
sidebarLayout(
sidebarPanel(
wellPanel(
h3("Widget 1"),
radioButtons("add", "Letter", c("A", "B", "C"))
),
wellPanel(
h3("Save button"),
actionButton("save", "Save")
)
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
DF
observeEvent(input$save, {
l <- nrow(DF)
DF[l+1,] <- list(input$add, input$add)
})
})
}
shinyApp(ui = ui, server = server)
Do you know what is wrong?
UPDATE:
With @Rémi answer the error is gone but the app does not add new rows to the table: app