RShiny应用程序中的反应性内容更新,但不会累积

RShiny应用程序中的反应性内容更新,但不会累积,r,shiny,R,Shiny,我正在尝试在我闪亮的应用程序中输出一个表,该表在每次单击提交按钮时添加额外的行。但是,它不是累加行,而是替换第一行。闪亮的文档对我帮助不大,尽管它真的很好 以下是用户界面: 这是服务器: 如何更新totalFrame,使其接受被动内容,但在totalFrame中累积,而不是一次又一次地替换第一行 样本输出: 如果我将Bob,Cleans,2017-04-25,30放入框架的相应列中,它会正确呈现表格,但是如果我尝试添加另一个条目,它只会替换Bob Cleans行 解决方案 使用 reactive

我正在尝试在我闪亮的应用程序中输出一个表,该表在每次单击提交按钮时添加额外的行。但是,它不是累加行,而是替换第一行。闪亮的文档对我帮助不大,尽管它真的很好

以下是用户界面:

这是服务器:

如何更新totalFrame,使其接受被动内容,但在totalFrame中累积,而不是一次又一次地替换第一行

样本输出: 如果我将Bob,Cleans,2017-04-25,30放入框架的相应列中,它会正确呈现表格,但是如果我尝试添加另一个条目,它只会替换Bob Cleans行

解决方案

使用

reactiveValues生成默认和反应表,与 观察是否按下了按钮,以及 根据需要,您需要将submitButton更改为actionButton。 正如HubertL提到的问题一样,这确实会更新初始表

代码

输出

以前

按5x后


如果您对如何将汇总表写入/更新为包含每个条目的.csv文件有任何想法,请给予额外奖励!我还没有开始这个项目的这一部分。关于额外的积分部分还不清楚。在函数totalFrame中就是这样。谢谢大家的帮助!
shinyUI(fluidPage(

titlePanel("CSF Payroll App"),

sidebarLayout(
    sidebarPanel(
        textInput("name", "Put your name here"),
        textInput("job", "Job completed"),
        dateInput("date", "Input the date you worked"),
        numericInput("minutes", "Input the time you worked 
            (15 minute increments)", 0,0,240,15),
        submitButton("Submit")
    ),

    mainPanel(
        tableOutput("totalHours")
        )
)
))
shinyServer(function(input, output) {

# Initialize a dataframe to hold all entries.
totalFrame <- data.frame(
    Name <- character(),
    Job <- character(),
    Date <- character(),
    Minutes <- numeric(), 
    stringsAsFactors=FALSE)
colnames(totalFrame) <- c("Name", "Job",
                          "Date", "Minutes")


# Create a temporary dataframe to take a new entry, reactively,
# then update the totalFrame with each new entry.
addNextEntry <- reactive({
    Name <- input$name
    Job <- input$job
    Date <- as.character(input$date)
    Minutes <- input$minutes
    tempFrame <- data.frame(Name, Job, Date, Minutes)
    totalFrame <- bind_rows(totalFrame, tempFrame)
    totalFrame
})

# Update the summary dataframe with new entries as they come in.
output$totalHours <- renderTable({
    addNextEntry()
})
})
library(shiny)
library(dplyr)
ui <- shinyUI(fluidPage(

  titlePanel("CSF Payroll App"),

  sidebarLayout(
    sidebarPanel(
      textInput("name", "Put your name here"),
      textInput("job", "Job completed"),
      dateInput("date", "Input the date you worked"),
      numericInput("minutes", "Input the time you worked 
                   (15 minute increments)", 0,0,240,15),
      actionButton(inputId = 'button_1',label = 'Add')
      ),

    mainPanel(
      tableOutput("totalHours")
    )
  )
  ))

server <- function(input, output) {

  # default table
  totalFrame <- reactiveValues(table = data.frame(
      Name = character(),
      Job = character(),
      Date = character(),
      Minutes = numeric(), 
      stringsAsFactors = FALSE))


  # update default table, when actionButton is pressed
  observeEvent(input$button_1, {
    totalFrame$table <- bind_rows(
      totalFrame$table,
      data.frame(
        Name = input$name,
        Job = input$job,
        Date = as.character(input$date),
        Minutes = input$minutes
      )
    )
  })
  table <- reactive({totalFrame$table})

  # just render the table
  output$totalHours <- renderTable({
    table()
  })
}

shinyApp(ui = ui, server = server)