Create a gist now

Instantly share code, notes, and snippets.

@daattali /app.R
Last active Jun 26, 2017

What would you like to do?
Basic form-submission shiny app used in "Persistent data storage in shiny apps" article
# 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 <-
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")) {
# Shiny app with 3 fields that the user can submit data for
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]])
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({

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:


Listening on
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:

etwatson commented Mar 11, 2017


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

Any solves for ewatson's issue above? I'd be interested because I am having the same issue. I've tried using cast num > char > date per a suggestion and that didn't work, I still was getting the numeric values of dataInputs.

angelovangel commented Jun 26, 2017

this is a datatable (DT), so check this out (I don't know your code so this is just a general structure):
DT::renderDataTable({ datatable() %>% formatDate("date", toDateString) })
Here the helper functions of DT are explained:

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