使用 R shiny 包保存插入的数据(在数据 table 中)

Save data instered (in the data table) using the R shiny package

我正在制作一个 R shiny 应用程序(我对 R shiny 很陌生)它获取数据作为输入并将数据投影为 table(使用 DT 包)。但是,当插入和提交新数据时,不会添加新行,而是添加之前的观察结果(之前提交的数据只是更新。因此,我该怎么做才能保存新数据数据?

您代码中的主要问题是您创建了一个新的数据框并将其替换为之前的数据框。我稍微修改了你的服务器代码。我已经添加了一个全局变量 TabData 来保存数据 table 中以前的数据,并且 rbind 它与要添加到 table 的新数据一起保存。请看下面修改后的代码:

library(shiny)
library(car)    # Import library to use recode() function

shinyServer(function(input, output) {
  #Global variable to save the previous data in the data table
  TabData <- data.frame()

  values <- reactiveValues()
  # Calculate damage and loss (just ab experiment)
  observe({
    input$action_Calc
    values$int <- isolate({input$reduction * 10})

    values$amt <- isolate({input$lost}) + values$int

    values$com<-isolate({input$community})

    values$disaster <- isolate({input$disaster})

    values$name <- isolate({input$name})

  })

  # Display values entered
  output$text_principal <- renderText({
    input$action_Calc
    paste("reduction: ", isolate(input$reduction))
  })

  output$text_intrate <- renderText({
    input$action_Calc
    paste("Vegetation: ", isolate(input$veg))
  })

  output$text_int <- renderText({
    if(input$action_Calc == 0) ""
    else
      paste("Damage:", values$int)
  })

  output$text_amt <- renderText({
    if(input$action_Calc == 0) ""
    else 
      paste("Loss:", values$amt)
  })

  Data <- reactive({
    browser()
    if (input$action_Calc > 0) {
      df <- data.frame(
        Community= values$com,

        Disaster = values$disaster,

        Name = values$name,

        Damage=values$amt,

        Loss=values$int



      )
      return(list(df=df))
    }
  })



  output$responses <- DT::renderDataTable(DT::datatable({
    if (is.null(Data())) {return()}
    #Row bind the new data to be added to the table
    TabData <<- rbind(TabData, Data()$df)
    datum<-TabData

    datum


  }))
}) 

希望对您有所帮助!