Create a gist now

Instantly share code, notes, and snippets.

@daattali /app.R
Last active Apr 10, 2017

What would you like to do?
Basic form-submission shiny app used in "Persistent data storage in shiny apps" article http://deanattali.com/blog/shiny-persistent-data-storage/
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()
})
}
)

Sorry if I am the only one who doesn't know this but what is "<<-"?

copied the script and attempted to run today (Windows 7, R V3.2.2, RStudio 0.99.473, shiny v 0.12.2 and shinyapps 0.3.63).

Error message:

shiny::runApp()

Listening on http://127.0.0.1:4135
Error in if (is.null(data) || ncol(data) == 0) { :
missing value where TRUE/FALSE needed

Since this conditional execution does not appear in the app.R script, it must be somewhere else, which I can't find...The error message suggests that the object data is missing so the condition can't be evaluated.with is.null(data) OR ncol(data)

SOLUTION: found that the object responses is the name of a function in the utils namespace. When I changed the object to "responses1" , the code executes....

So the problem seems to arise when the object data is assigned to responses which is a function object not a data object....

thanks for your blog directions, I will keep learning now!!

Hi hansthompson,

For your question about "<<-" operation, using "<<-", you can define a global variable and assign to the reactive function.

Refer here: http://stackoverflow.com/questions/15327506/r-shiny-how-to-save-input-data-to-the-server-or-access-input-variables-globally

etwatson commented Mar 11, 2017 edited

Hi,

I've made a change, adding a dateInput() to the input, but the form data is not in date format. How can i specify that the field "date" is as.Date() in the "responses" table?

screen shot 2017-03-11 at 12 15 19 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment