Thanks Mike. I made a prototype using observaeEvent and updateSelectInput(), as shown below. However, even though it seems good, it still has problem. The biggest one, in my opinion, is I can't select 4 No's. You can try it and select all options in distances. It should be obvious if you try it out, but if it does not make sense, please let me know. Thank you!
yes_no_generator = function(old_list = NULL, new_value = NULL){
if(is.null(new_value)) {
new_value = "Yes" # default value when first runs
} else{
new_value = paste0(strsplit(new_value, "")[[1]][1:(length(strsplit(new_value, "")[[1]]) -1 )], collapse = "") # remove the post-index
}
# count the number of yes and no in the old_list
counter_yes = sum(names(old_list) == "Yes") # number of yes
counter_no = sum(names(old_list) == "No") # number of no
# based on the number of yes and no, we now know how to name the new yes or no
if(new_value == "Yes")
{new_option = paste0("Yes", counter_yes + 1); names(new_option) = "Yes"}
if(new_value == "No")
{new_option = paste0("No", counter_no + 1); names(new_option) = "No" }
# next, we just need to insert the new thing to the right position
browser()
new_list = append(old_list, new_option, length(old_list) - 2)
# get old selected values and append on it
if(length(old_list) == 2){
new_selected = new_option
} else{
new_selected = c(old_list[1:(length(old_list) - 2)], new_option)
}
return(list(choices = new_list, selected = new_selected))
}
shinyApp(
ui = fluidPage(
selectInput("distance", "Choose a Distance:",
c("Bray-Curtis"= "Bray-Curtis", "Jaccard" = "Jaccard", "UnWeighted" = "UnWeighted",
"Generalized" = "Generalized"),
multiple = TRUE
),
selectInput("rarefication", "Rarefy ?",
c("Yes", "No"),
multiple = TRUE
),
textInput("alpha", "Alpha", ""),
textOutput("result")
),
server = function(input, output, session) {
choices = NULL
observeEvent(input$distance, {
if(is.null(input$rarefication)){ # when the program first runs
choices = c("Yes" = "Yes1", "No" = "No1")
}
choices_n_selected = yes_no_generator(old_list = choices, new_value = input$rarefication)
choices <<- choices_n_selected$choices
updateSelectInput(session, "rarefication",
label = "Rarefy ?",
choices = choices_n_selected$choices,
selected = choices_n_selected$selected
)
})
output$result <- renderText({
paste("You chose", input$state)
})
}
)