【发布时间】: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 <- bind_rows(totalFrame, tempFrame)不会替换全局变量totalFrame的值