首页 > 解决方案 > r闪亮:从以前持久存储的数据中加载数据以形成字段

问题描述

Dean Attali 的要点:https ://gist.github.com/daattali/c4db11d81f3c46a7c4a5 在此示例中:可以输入、提交和保存数据(持久数据存储)。您可以在提交后出现的表格中看到提交的数据。随着时间的推移,表格随着不同的条目而增长。到目前为止还没有可能加载提交表单中的数据来更改或更新提交?!

我的问题:是否可以将以前提交的数据不仅加载到表中,还加载到字段(名称、使用闪亮、r num 年)?我想更新条目并将它们保存回来。我知道 CRUD。我想知道 Dean Attali 的持久数据存储示例是否可行。谢谢!

library(shiny)

# Define the fields we want to save from the form
fields <- c("name", "used_shiny", "r_num_years")

# Save a response
# ---- This is one of the two functions we will change for every storage type ----
saveData <- function(data) {
  data <- as.data.frame(t(data))
  if (exists("responses")) {
    responses <<- rbind(responses, data)
  } else {
    responses <<- data
  }
}

# Load all previous responses
# ---- This is one of the two functions we will change for every storage type ----
loadData <- function() {
  if (exists("responses")) {
    responses
  }
}

# Shiny app with 3 fields that the user can submit data for
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("responses", width = 300), tags$hr(),
    textInput("name", "Name", ""),
    checkboxInput("used_shiny", "I've built a Shiny app in R before", FALSE),
    sliderInput("r_num_years", "Number of years using R", 0, 25, 2, ticks = FALSE),
    actionButton("submit", "Submit")
  ),
  server = function(input, output, session) {
    
    # Whenever a field is filled, aggregate all form data
    formData <- reactive({
      data <- sapply(fields, function(x) input[[x]])
      data
    })
    
    # When the Submit button is clicked, save the form data
    observeEvent(input$submit, {
      saveData(formData())
    })
    
    # Show the previous responses
    # (update with current response when Submit is clicked)
    output$responses <- DT::renderDataTable({
      input$submit
      loadData()
    })     
  }
)

标签: rformsshinystoragepersistent

解决方案


我现在确切地找到了我正在寻找的东西:https ://www.r-bloggers.com/shiny-crud-app/ 这篇文章逐步向您展示如何构建一个闪亮的应用程序,让您创建、更新和删除数据在一张桌子上。

您可以在此处查看该应用程序的运行情况:https ://gluc.shinyapps.io/crud

完整的源代码在这个要点中:https ://gist.github.com/gluc/d39cea3d11f03542970b

现在下一步是合并:提交、新建、删除并保存到例如 csv 文件。我会试试看!


推荐阅读