【发布时间】: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