【发布时间】:2018-07-17 11:03:36
【问题描述】:
我试图通过构建一个我认为非常简单但有用的应用程序来了解 RShiny。我希望应用程序做的是允许用户输入一些由日期、数字和字符组成的数据。然后,当用户按下保存/提交按钮时,此数据将附加到由先前记录组成的预先存在的数据帧上,并覆盖这些记录的 .csv。我还希望这些数据以 UI 中表格的形式呈现给用户,一旦用户按下保存/提交按钮,该表格就会更新。
我已经设法让大部分 UI 功能正常工作,但是,我遇到了真正的困难 1) 以正确的格式保存数据和 2) 更新 UI 上显示的表格。我当前保存数据的方法包括创建输入值的隔离列表并将其绑定到原始数据框。但是,输入值的格式似乎都恢复为日期特别成问题的因素,因为据我所知,输出毫无意义。在更新 UI 方面,我尝试从数据框中创建一个响应式对象,并将该对象用作 renderDataTable 中显示的数据,但这种方法似乎没有任何影响。
我在下面创建了一个虚拟的最小示例。
提前感谢您的所有帮助。
require(shiny)
require(tidyverse)
require(lubridate)
require(plotly)
#Would import the data in reality using read.csv() but to allow for an easily
#recreated example I made a dummy data frame
DateRecorded <- dmy(c("10/07/2018", "11/07/2018", "13/07/2018"))
Value <- c(1, 2, 3)
Person <- c("Bob", "Amy", "Charlotte")
df <- data.frame(DateRecorded, Value, Person)
ui <- fluidPage(
#UI Inputs
dateInput(inputId = "SessionDate", label = "Date Recorded", format = "dd-mm-yyyy"),
numericInput(inputId = "SessionValue", label = "Value Recorded", value = 0),
textInput(inputId = "SessionPerson", label = "Person Recording"),
actionButton(inputId = "Save", label = "Save"),
#UI Outputs
dataTableOutput("TheData"),
textOutput("TotRecorded")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#When "Save" is pressed should append data to df and export
observeEvent(input$Save, {
newLine <- isolate(c(input$SessionDate, input$SessionValue, input$SessionPerson))
isolate(df <- rbind(as.matrix(df), unlist(newLine)))
write.csv(df, "ExampleDataFrame.csv") #This export works but the date is saved incorrectly as "17729" not sure why
})
#Create a reactive dataset to allow for easy updating
ReactiveDf <- reactive({
df
})
#Create the table of all the data
output$TheData <- renderDataTable({
ReactiveDf()
})
#Create the totals print outs
output$TotRecorded <- renderPrint({
data <- ReactiveDf()
cat(nrow(data))
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】: