Reputation: 195
I want to realize a function that "When the user click an action button once, a row is added to a data frame". for example, add a row of [1,2,3] when clicking once. I tried the codes
res <- reactive({NULL})
res <- eventReactive({input$button,
rbind(res(), t(c(1,2,3)))
})
but, this cannot work. so, is there any way to achieve the aim ?
Upvotes: 2
Views: 380
Reputation: 25385
I would personally use a reactiveVal
and and observeEvent
for this purpose as follows:
library(shiny)
ui <- fluidPage(
actionButton('btn1','button'),
dataTableOutput('table1')
)
server <- function(input,output){
my_df <- reactiveVal(cars[1:5,])
observeEvent(input$btn1,{
# call old value with my_df(), rbind the new row, and set the new value
# by wrapping the statement with my_df()
my_df(rbind(my_df(), c(1,2)))
})
output$table1 <- renderDataTable(
my_df())
}
shinyApp(ui,server)
Upvotes: 5
Reputation: 6325
I am not sure why this is a tough ask. This is very straightforward with storing previous dataframe in a global variable and rbinding again and again.
Code:
library(shiny)
ui <- fluidPage(
actionButton('btn1','button'),
dataTableOutput('table1')
)
global_df <- cars[1:5,]
server <- function(input,output){
new_df <- eventReactive(input$btn1,{
global_df <<- rbind(global_df, c(1,2))
global_df
})
output$table1 <- renderDataTable(
new_df())
}
shinyApp(ui,server)
Upvotes: 0