【问题标题】:Reactive content in RShiny app updates, but does not accumulateR Shiny 应用程序中的反应性内容会更新,但不会累积
【发布时间】:2017-09-23 00:41:05
【问题描述】:

我试图在我的 Shiny 应用程序中输出一个表格,每次单击“提交”按钮时都会添加额外的行。但是,它不是累积行,而是替换第一个。 Shiny 文档对我帮助不大(尽管它真的很好)。

用户界面如下:

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()
})
})

如何以一种采用响应式内容的方式更新 totalFrame,但在 totalFrame 中累积,而不是一次又一次地替换第一行?

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

【问题讨论】:

  • 如果您提出有关如何将汇总表写入/更新到每个条目的 .csv 文件的任何想法,则可加分!我还没有开始项目的那部分。
  • Extra credit部分不清楚。
  • 函数内部totalFrame &lt;- bind_rows(totalFrame, tempFrame)不会替换全局变量totalFrame的值

标签: r shiny


【解决方案1】:

解决方案

使用

  • reactiveValues 生成默认和响应式表,结合
  • observeEvent 检查您的按钮是否被按下,并且
  • 根据this post,需要将submitButton改为actionButton

这确实更新了初始表(正如 HubertL 提到的问题)。

代码

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)

输出

之前

之后(按 5x)

【讨论】:

  • 就是这样。感谢大家的帮助!
猜你喜欢
  • 2022-11-04
  • 2020-06-26
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多