【问题标题】:Add new row to dataframe when actionButton is clicked单击 actionButton 时向数据框添加新行
【发布时间】:2019-03-29 18:49:12
【问题描述】:

我正在尝试创建一个 R Shiny 应用程序,该应用程序根据输入值创建一个 1 行数据框,当单击操作按钮时,它将该数据框作为新行添加到另一个数据框(开始为空白)。

我浏览过 StackOverflow,但找不到可以解决我的问题的内容。

我希望发生的事情如下:

input$one <- "A"
input$two <- "B"
input$three <- "C"

df1 = A | B | C 

现在当我按下 actionButton 时,我希望 df2(开头为空白)如下:

df2 = A | B | C

接下来,我希望能够添加更多行。因此,如果我将输入值更改为以下内容:

input$one <- "X"
input$two <- "Y"
input$three <- "Z"

df1 = X | Y | Z 

然后我再次单击 actionButton,df2 应该更新为以下内容:

df2 = A | B | C
      X | Y | Z

最后,最后一次更新和点击 actionButton 如下:

input$one <- "1"
input$two <- "2"
input$three <- "3"

df1 = 1 | 2 | 3

*actionButton click*

df2 = A | B | C
      X | Y | Z
      1 | 2 | 3

我想尽可能多地执行此操作,每次单击 actionButton 时,它都会将 df1 中的任何内容作为新行添加到 df2。我知道这将不得不使用 rbind,但是如何使用 actionButton 来完成呢?

【问题讨论】:

  • 使用rbind 添加行
  • @akrun 对,但是如何使用响应式数据框和 actionButton 来做到这一点?

标签: r dataframe shiny


【解决方案1】:

这对我有用。

mydata <- data.frame()

  df <- eventReactive(input$add_payer, {
      newrow <- data.frame(ALEMO())
      mydata <<- rbind(mydata, newrow)
    })

  output$all_data <- renderTable(df())

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 2021-04-17
    • 2019-11-20
    • 2014-05-04
    • 1970-01-01
    • 2022-01-02
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多