I am obviously still not fully understanding req() despite reading the documentation, reviewing examples, and mucking about with it. I think it is pretty obvious in the following reprex what I am trying to do, choose a type of submission form then only allow submission of that form if all the inputs are complete, but it only works as desired if both types have been "touched". I was under the impression that req() only affected the block of code it is placed in, in this case the case_when functions, but the two req()'s are obviously affecting each other somehow. Please help me unscramble this in my brain!
I edited the code to a simpler example.
On start up the button is not shown, even when the criteria are satisfied:
If the Admin button is then selected and the criteria satisfied, the button is displayed:
If we then click back to User and enter both inputs, the button appears this time:
library(shiny)
ui <- fluidPage(
br(),
radioButtons("person_type", "Type",
choices = c("User" = "user",
"Admin" = "admin"), inline = T),
br(),
uiOutput("person_info_ui"),
br(),
uiOutput("add_button"),
br(),br()
)
server <- function(input, output, session) {
output$person_info_ui <- renderUI({
case_when(input$person_type == "user" ~ tagList(
textInput("name","Name"),
selectInput("group","Group",
choices = c("",'Red','Blue'),
selectize=F)),
input$person_type == "admin" ~ tagList(
textInput("name","Name"),
textInput("id_num","ID Number"))
)
})
output$add_button <- renderUI({
case_when(input$person_type == "user" ~ {
req(input$name, input$group)
tagList(
actionButton("add_user_db_button","Add User to Database"))
},
input$person_type == "admin" ~ {
req(input$name, input$id_num)
tagList(
actionButton("add_admin_db_button","Add Admin to Database"))
})
})
observeEvent(input$add_user_db_button, {
# ADD TO DATABASE
removeModal()
})
observeEvent(input$add_admin_db_button, {
# ADD TO DATABASE
removeModal()
})
}
shinyApp(ui, server)