【问题标题】:Updating a data frame in real time in RShiny在 R Shiny 中实时更新数据帧
【发布时间】: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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我做了一些小调整。

    • observeEvent 的正文中不需要isolate;它不会对其主体中的值产生反应性依赖。
    • 我将ReactiveDf 设为reactiveVal 而不是reactive。这允许您从 observeEvent 中写入其值。
    • 而不是对矩阵进行行绑定并取消列出列表 - 问题是所有新值都被解析为同一个类,而它们显然不是 - rbind 两个data.frames 可能更容易,所以用newLine &lt;- data.frame(DateRecorded = input$SessionDate, Value = input$SessionValue, Person = input$SessionPerson)

    所以一个工作示例如下所示。希望这会有所帮助!

    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 <- data.frame(DateRecorded = input$SessionDate, Value = input$SessionValue, Person = input$SessionPerson)
        df <- rbind(df, newLine)
        ReactiveDf(df) # set reactiveVal's value.
        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 <- reactiveVal(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)
    

    【讨论】:

    • @Csim063 很高兴我能帮上忙 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2021-02-13
    • 1970-01-01
    • 2019-05-21
    • 2016-09-15
    • 1970-01-01
    相关资源
    最近更新 更多