Skip to content

Instantly share code, notes, and snippets.

@klittle314
Last active November 18, 2015 21:36
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 klittle314/e1c0482c22348d934e71 to your computer and use it in GitHub Desktop.
Save klittle314/e1c0482c22348d934e71 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(),
textOutput("sheet_confirmation"),
tableOutput("table_output")
)
server <- function(input, output){
submit_check1 <- reactive({
champ_name <- input$champ_name
conditions_met <- champ_name != ''
if (conditions_met) {
out <- list(submit = TRUE, error_message = NULL)
}
else {
out <- list(submit = FALSE)
if(champ_name== '') error_message <- "Enter name"
out$error_message <- error_message
}
return(out)
})
#This is where I expect to need input$submit to change state before the functions
#sheet_confirmation and df1_out execute.
observeEvent(input$submit, {
sheet_confirmation <- reactive({
champ_name <- input$champ_name
conditions <- submit_check1()
if(conditions$submit) {
out <- sprintf("Table updated")
}
else {
out <- conditions$error_message
}
out
})
df1_out <- reactive({
champ_name <- input$champ_name
conditions <- submit_check1()
if(conditions$submit) {
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$sheet_confirmation <- renderText(sheet_confirmation())
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