Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Forked from klittle314/app.R
Last active November 19, 2015 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcheng5/9cadfad7751f20cd957a to your computer and use it in GitHub Desktop.
Save jcheng5/9cadfad7751f20cd957a to your computer and use it in GitHub Desktop.
Test Shiny app: submit and clear
#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