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() | |
}) | |
} | |
) |
This comment has been minimized.
This comment has been minimized.
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 http://127.0.0.1:4135 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!! |
This comment has been minimized.
This comment has been minimized.
Hi hansthompson, For your question about "<<-" operation, using "<<-", you can define a global variable and assign to the reactive function. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
@etwatson, |
This comment has been minimized.
This comment has been minimized.
I use exactly the same code as in the Gist and for some reason from the 3rd input the data table is lagging by one row. For example, if I submit d1 for a name it shows immediately, then d2 for a name which shows up immediately as well, but when I submit the 3rd value then it does not show up in the data table immediately and only sows up after I submit the 4th value. From now the table is lagging by one. As an alternative I used
|
This comment has been minimized.
This comment has been minimized.
Hello Dean, Love your coding style, have been reviewing your "Basic Shiny app without data storage". Q1. When I quit the RStudio session, the RESPONSE data remains in the OS RAM? |
This comment has been minimized.
This comment has been minimized.
Hi Dean,
Also the
I think there a some internals hidden to my sight I would like to understand. |
This comment has been minimized.
Sorry if I am the only one who doesn't know this but what is "<<-"?