r - 有没有什么方法可以使用 Shiny 的操作按钮递归地将行添加到 data.frame 中?

标签 r dataframe shiny

我想实现一个“当用户单击一次操作按钮时,在数据框中添加一行”的功能。例如点击一次添加一行[1,2,3]。我尝试了代码

res <- reactive({NULL})
res <- eventReactive({input$button, 
  rbind(res(), t(c(1,2,3)))
})

但是,这是行不通的。那么,有什么办法可以达到目的呢?

最佳答案

我个人会使用 reactiveValobserveEvent 来实现此目的,如下所示:

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/

相关文章:

R Shiny 将 SelectizeInput 默认值设置为 NULL

r - 表中的重复行与值具有列参数的次数相同(在 R 或 Excel 中)

r - 如何在 R 中将 30 秒数据扩展到 60 分钟数据

r - 如何将数据框转换为 R 中的列联表?

python - 将数据拆分为特征和标签后,标签列形状不一致

html - 在 Shinyapps.io 上托管时, Shiny 的 Rmarkdown selectInput 不起作用 - 选项包含 <a0>

删除特定字符串的重音符号 R

sql-server - SQL Server R 多个结果集

r - 如何在最接近的日期合并 2 个 Zoo/data.frame 对象?

r - DT 版本 0.3(最新)不渲染表