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(), | |
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