我想实现一个“当用户单击一次操作按钮时,在数据框中添加一行”的功能。例如点击一次添加一行[1,2,3]。我尝试了代码
res <- reactive({NULL})
res <- eventReactive({input$button,
rbind(res(), t(c(1,2,3)))
})
但是,这是行不通的。那么,有什么办法可以达到目的呢?
最佳答案
我个人会使用 reactiveVal
和 observeEvent
来实现此目的,如下所示:
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)
关于r - 有没有什么方法可以使用 Shiny 的操作按钮递归地将行添加到 data.frame 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48397713/