1

I am trying to update values in a dropdown for a shiny app dynamically. So if value is selected in the dropdown classification it should updates the values in the product family dropdown. What am I missing in the code below. It doenst seem to be pciking any value and takes values from the else condition only.

library(shiny)
library(shinyWidgets)

# Define the UI
ui <- bootstrapPage(
  column(3, uiOutput("class_level")),
  column(3,uiOutput("product"))
)


# Define the server code
server <- function(input, output) {

  output$class_level <- renderUI({
    selectInput(
      "selected_class",
      label = h4("Classification Level"),
      choices = list(
        "Brand" = "Brand",
        "Brand1" = "Brand1",
        "Brand2" = "Brand2"
      ),
      selected = "Brand"
    )
  })


  getFlavor <- reactive({

    if (input$selected_class =="Brand") {
      c( "a " = "a",
         "b" = "b",
         "c" = "c"
        )
    }
    else if  (input$selected_class =="Brand1")
    {
      c(
        "1" = "1",
        "2" = "2",
        "3" = "3"
      )
    }
    else   (input$selected_class =="Brand2")
    {
      c(
        "x" = "x",
        "y" = "y",
        "z" = "z"
      )
    }

  })

  output$product <- renderUI({
    pickerInput(
      "selected_product",
      label = h4("Product Family"),
      choices = as.list(getFlavor()),
      selected = as.list(getFlavor()),
      options = list(
        `deselect-all-text` = "None",
        `select-all-text` = "Total",
        `actions-box` = TRUE
      ),
      multiple = F,
      width = "100%"
    )
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

3 Answers 3

4

You need to return the content within your if statements. You can do this using return

library(shiny)
library(shinyWidgets)

# Define the UI
ui <- bootstrapPage(
  column(3, uiOutput("class_level")),
  column(3,uiOutput("product"))
)


# Define the server code
server <- function(input, output) {

  output$class_level <- renderUI({
    selectInput(
      "selected_class",
      label = h4("Classification Level"),
      choices = list(
        "Brand" = "Brand",
        "Brand1" = "Brand1",
        "Brand2" = "Brand2"
      ),
      selected = "Brand"
    )
  })


  getFlavor <- reactive({

    if (input$selected_class =="Brand") {
      return(c( "a " = "a",
         "b" = "b",
         "c" = "c"
        ))
    }
    else if  (input$selected_class =="Brand1")
    {
      return(c(
        "1" = "1",
        "2" = "2",
        "3" = "3"
      ))
    }
    else   (input$selected_class =="Brand2")
    {
      return(
      c(
        "x" = "x",
        "y" = "y",
        "z" = "z"
      ))
    }

  })

  output$product <- renderUI({
    pickerInput(
      "selected_product",
      label = h4("Product Family"),
      choices = as.list(getFlavor()),
      selected = as.list(getFlavor()),
      options = list(
        `deselect-all-text` = "None",
        `select-all-text` = "Total",
        `actions-box` = TRUE
      ),
      multiple = F,
      width = "100%"
    )
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)
Sign up to request clarification or add additional context in comments.

Comments

1

Your approach is somewhat complicated and updatePickerInput would make your job way easier. Here is one possible implementation:

library(shiny)
library(shinyWidgets)

ui <- bootstrapPage(

  column(
    width = 3, 
    selectInput(
      inputId = "selected_class",
      label = h4("Classification Level"),
      choices = c("Brand", "Brand1", "Brand2"),
      selected = "Brand"
    )
  ),
  column(
    width = 3, 
    pickerInput(
      inputId = "selected_product",
      label = h4("Product Family"),
      choices = c("a", "b", "c"),
      options = list(
        `deselect-all-text` = "None",
        `select-all-text` = "Total",
        `actions-box` = TRUE
      ),
      width = "100%"
    )
  )
)

server <- function(input, output, session) {

  observeEvent(input$selected_class, {
    if(input$selected_class =="Brand") {
      choices <- c("a", "b", "c")
    } else if(input$selected_class =="Brand1") {
      choices <- c("1", "2", "3")
    } else {
      choices <- c("x", "y", "z")
    }
    updatePickerInput(
      session,
      inputId = "selected_product",
      choices = choices
    )
  })

}

shinyApp(ui = ui, server = server)

Comments

0

Bit late to the party but if your data can be in a data frame or tibble then this is more streamlined than existing answers and needs no "widgets".

I needed this kind of thing for column value preview. One select input for column names, another providing column values when user selects another column name.

library(shiny)

firstOutputID <- "selCol"
secondOutputID <- "colDat"

shapeID <- "SHAPE_ID"
shapeName <- "NAME"

ui <- bootstrapPage(
  column(
    width = 3, 
    uiOutput(outputId = firstOutputID),
    uiOutput(outputId = secondOutputID)
  )
)

server <- function(input, output, session) {
  firstSelectID <- "comboCol"
  secondSelectID <- "comboDat"
  
  sampleDF <-
    data.frame(
      col1 = c(1, 2),
      col2 = c("Name 1", "Name 2")
    )
  colnames(sampleDF) <- c(shapeID, shapeName)

  output[[firstOutputID]] <- renderUI({
    selectInput(
      inputId = firstSelectID,
      label = "Column name",
      choices = colnames(sampleDF),
      selected = shapeName
    )
  })
  
  output[[secondOutputID]] <- renderUI({
    # or your own filtering mechanism here:
    filteredChoices <- sampleDF[ , input[[firstSelectID]]]
    
    selectInput(
      inputId = secondSelectID,
      label = "Column data",
      selectize = FALSE,
      size = 5,
      choices = filteredChoices
    )
  })
}

shinyApp(ui = ui, server = server)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.