r闪亮:从以前持久存储的数据中加载数据以形成字段 [英] r shiny: Load data in to form fields from previously persistent stored data

查看:35
本文介绍了r闪亮:从以前持久存储的数据中加载数据以形成字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Gist from Dean Attali: https://gist.github.com/daattali/c4db11d81f3c46a7c4a5 In this example: data can be entered, submitted and saved (persistent data storage). You can see the submitted data in a table that appears after submitting. By time the table is growing with different entries. So far there is no possibility to load the data in the submitted form to change or update the submission?!

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

My question: Is it possible to load the data from previous submissions not only to the table but also to the fields (name, used shiny, r num years)? I would like to update the entries and save them back. I am aware of CRUD. I am wondering if it is possible with Dean Attali's persistent data storage example. Thanks!

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

推荐答案

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

I now exactly found what I was looking for: https://www.r-bloggers.com/shiny-crud-app/ This post shows you step by step how to build a Shiny app that lets you create, update and delete data in a table.

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

You can see the app in action here: https://gluc.shinyapps.io/crud

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

The full source-code is in this gist: https://gist.github.com/gluc/d39cea3d11f03542970b

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

Now the next step is to combine: submit, new, delete and save to a for example csv file. I will try it out!

这篇关于r闪亮:从以前持久存储的数据中加载数据以形成字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆