【问题标题】:r shiny: Load data in to form fields from previously persistent stored datar闪亮:从以前持久存储的数据中加载数据以形成字段
【发布时间】:2020-08-09 17:50:10
【问题描述】:

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()
    })     
  }
)

【问题讨论】:

    标签: r forms shiny storage persistent


    【解决方案1】:

    我现在完全找到了我要找的东西:https://www.r-bloggers.com/shiny-crud-app/ 这篇文章将逐步向您展示如何构建一个闪亮的应用程序,让您可以创建、更新和删除表中的数据。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多