Reputation: 6874
I'm trying to use persistent data storage to allow a user to load up a previously entered bit of text data into a text box. I'd also like the user to be able to enter some new data and for this to be stored.
The app works but it doesn't allow the user to enter new data into the text box. I've tried using selectizeInput with the options create=TRUE but with no success. Here is my code:
library(shiny)
library(here)
outputDir <- here("responses")
saveData <- function(data) {
data <- t(data)
# Create a unique file name
fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))
# Write the file to the local system
write.csv(
x = data,
file = file.path(outputDir, fileName),
row.names = FALSE, quote = TRUE
)
}
loadData <- function() {
# Read all the files into a list
files <- list.files(outputDir, full.names = TRUE)
data <- lapply(files, read.csv, stringsAsFactors = FALSE)
# Concatenate all data together into one data.frame
data <- do.call(rbind, data)
data
}
# Define the fields we want to save from the form
fields <- c("name", "used_shiny", "r_num_years")
# Shiny app with 3 fields that the user can submit data for
shinyApp(
ui = fluidPage(
DT::dataTableOutput("responses", width = 300), tags$hr(),
selectizeInput("MyChoices","Select from ui delimiters", choices=
loadData()[,1],options = list(create = TRUE)),
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()
})
}
)
Upvotes: 1
Views: 71
Reputation: 6874
Aha this was a simple answer. You have to allow multiple =TRUE if you want to load stored entries as well as allow new entries to be inputted. The following needs to be added as part of the ui:
selectizeInput("name", "Name", "",multiple = TRUE,options = list(create = TRUE),choices=loadData()[,1])
Upvotes: 1