Test Shiny app: submit and clear
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#test app to load the form and clear it for second record entry | |
#Kevin Little, Ph.D. 18 Nov 2015 | |
require(shiny) | |
require(shinyapps) | |
require(shinyjs) | |
#local df taking the place of a Google sheet | |
df1 <- data.frame(name="name1", stringsAsFactors = FALSE) | |
ui <- fluidPage( | |
useShinyjs(), | |
div(id="form_app", | |
textInput( | |
inputId = "champ_name", | |
label = "Your name", | |
value = "") | |
), | |
actionButton(inputId = "submit", label="Submit"), | |
actionButton(inputId = "ClearAll", label= "Clear"), | |
hr(), | |
# I removed sheet_confirmation from here because the table_output | |
# will display the validation message in case of validation error. | |
tableOutput("table_output") | |
) | |
server <- function(input, output){ | |
# This eventReactive is like a regular reactive but it only | |
# invalidates when input$submit is clicked. | |
champ_name <- eventReactive(input$submit, { | |
# If input$champ_name is blank, that's a validation error. | |
# Stop executing whatever's executing and show this message | |
# instead. | |
validate(need(input$champ_name, "Enter name")) | |
input$champ_name | |
}) | |
df1_out <- reactive({ | |
output_sheet_row <- data.frame(name=champ_name()) | |
#add a row to the data frame | |
df1 <- rbind.data.frame(df1,output_sheet_row) | |
return(df1) | |
}) | |
output$table_output <- renderTable(df1_out()) | |
observeEvent(input$ClearAll, { | |
reset("form_app") | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment