I have a database and I need to make that in the first selecInput shows the name of 4 diferents columns and according whit the selection, in the next selecIpunt show the categories that correpond to the column selected. I have something like this in my server:
output$SelectCategory <-renderUI({
selectInput("SelectCategory", "Select Category",
choices = c("A"="colname1",
"B"="colname2",
"C"="colname3",
"D"="colname4"))
})
output$SelectProc <-renderUI({
selectInput("SelectProc", "Select Procedure",
choices=input$SelectCategory, multiple = T, selected = TRUE)
CodePudding user response:
Something like this shows you how you can update the choices for the second selectInput:
library(shiny)
df = data.frame(
colname1=c("apple","banana", "pear"),
colname2=c("water", "juice", "milk")
)
ui <- fluidPage(
selectInput("SelectCategory", "Select Category",
choices = c("fruits"="colname1",
"drinks"="colname2")),
uiOutput("SelectProc")
)
server <- function(input, output, session) {
output$SelectProc <- renderUI(
selectInput("SelectProc", "Select Procedure",
choices=df[[input$SelectCategory]])
)
}
shinyApp(ui, server)
CodePudding user response:
I added UI parts and with a minor change of choices= c()
, it is working but user need to select items when they appear in the dropdown Select Procedure.
ui <- fluidPage(
uiOutput('SelectCategory'),
uiOutput('SelectProc')
)
server <- function(input,output,session){
output$SelectCategory <-renderUI({
selectInput("SelectCategory", "Select Category",
choices = c("A"="colname1",
"B"="colname2",
"C"="colname3",
"D"="colname4"))
})
output$SelectProc <-renderUI({
selectInput("SelectProc", "Select Procedure",
choices=c(input$SelectCategory), multiple = T,selected = T)
})
#end of server
}
shinyApp(ui = ui, server = server)
However, if you wish to update upon selection in Category, more change in server needed (See comments)
server <- function(input,output,session){
output$SelectCategory <-renderUI({
selectInput("SelectCategory", "Select Category",
choices = c("A"="colname1",
"B"="colname2",
"C"="colname3",
"D"="colname4"))
})
output$SelectProc <-renderUI({
selectInput("SelectProc", "Select Procedure",
choices=NULL, #c(input$SelectCategory), #choices be made NULL
multiple = F,selected = T) #Multiple be made FALSE
})
# added an observer with updateSelectInput
observe(
{input$SelectCategory
updateSelectInput(session = session,
inputId = 'SelectProc',label = "Select Procedure",
choices = c(input$SelectCategory)
)
})
# end of server
}