I want to have something like a while loop over the conditional panels in Shiny, where the condition for the loop is based on the previous inputs.
In details: a user chooses a country from which he wants to start and another where he wants to finish. They are also required to choose which borders the route should go through. What I want to achieve, is first to update the choices in Select Input so the first contains only borders of the country "From", then the borders of the country they chose to go etc. Also, I want the second and following Select Inputs to show only up to the point where user selects a border with the country "To".
I thought about the following pseudocode:
bord_to=sub(".*-", "", input$b_dir_1)
i=2
while(bord_to!=input$cou_to){
conditionalPanel(condition = 'bord_to != input$cou_to',
selectInput("b_dir[i]",
"",
"")
bord_to=sub(".*-", "", input$b_dir[i])
i=i+1
}
I want also to name the inputs like "b_dir_i".
I created the following code, which at least handles updating the input, but I still miss a way to conditionally show the inputs:
library(shiny)
countries <- c( "AT", "BG", "CH", "CZ", "DE", "FR", "GR", "HR", "HU", "IT-GREC", "IT-NORD", "PL", "RO", "RS", "SI", "SK", "TR" )
borders <- c( "MK-RS", "RS-MK", "AL-GR", "GR-AL", "AL-ME", "ME-AL", "BA-HR", "HR-BA", "BA-ME", "ME-BA", "GR-MK", "MK-GR", "GR-TR", "TR-GR", "RS-BA", "BA-RS", "HU-RS", "RS-HU", "RO-RS", "RS-RO", "HU-UA", "UA-HU", "SK-UA", "UA-SK", "AT-CZ", "CZ-AT", "AT-HU", "HU-AT", "DETE-CZ", "CZ-DETE", "HR-HU" , "HU-HR", "HR-SI", "SI-HR" , "HR-RS", "RS-HR", "GR-IT", "IT-GR", "CH-IT", "IT-CH", "CH-FR" , "FR-CH", "CH-AT", "AT-CH", "CH-DE", "DE-CH", "DE50-CZ", "CZ-DE50", "PL-CZ", "CZ-PL", "DE50-PL", "PL-DE50", "PL-SK", "SK-PL", "BG-MK", "BG-TR", "MK-BG", "TR-BG", "BG-RS", "RS-BG", "BG-GR", "GR-BG", "RO-BG", "BG-RO" )
ui<-fluidPage(
fluidRow( column( 6,
h4("Select countries:"),
selectInput("cou_from", "From:", c("", countries)),
selectInput("cou_to", "To:", c("", countries)) ) ),
h4("Select borders:"),
fluidRow(
column( 3, selectInput("b_dir_1", "", ""), ),
# this is the fragment I want to go on the loop
column( 3,
conditionalPanel(
condition = 'sub(".*-", "", input$b_dir_1) != input$cou_to ',
selectInput("b_dir_2", "", "") ) ) ),
actionButton( "run_anl",
"Run Analysis",
width = "100%",
style = "color: #fff; background-color: #337ab7;
border-color: #2e6da4" )
)
server<-function(input, output, session){
observe({
cou_1<-input$cou_from
choice1<-c("", borders[grepl(paste0("^", cou_1), borders)])
updateSelectInput(session, "b_dir_1", choices=choice1)
})
observe({
bor_ch_1<-input$b_dir_1
choice2<-c("", borders[grepl(paste0("^", sub(".*-", "", bor_ch_1)), borders)])
updateSelectInput(session, "b_dir_2", choices=choice2)
})
}
shinyApp(ui = ui, server = server)
I want the UI to look similar to this:


shinyjspackage has convenientshowandhidefunctions. - anotherfred