简体   繁体   中英

How to create variable inside Taglist environment Shiny

I am building a shiny dashboard with different input widgets. I want the options given in one widget to be dependent on the input in previous widget. For example, I have a pickerInput item where one can specify a variable from my dataset ( df ). Next, I want to display checkboxGroupInput where you can select the unique values of the earlier chosen variable. Therefore, the choices variable in checkboxGroupInput should be a list containing all unique values from the earlier chosen variable. My code now looks like this:

 tagList(
     pickerInput(inputId = "select_add",
                 label = "Select variable:",
                 choices = list(grouplist_1),
                 selected = NULL
                ),

                list_members = c(unique(as.data.frame(df)[input$select_add])),

                conditionalPanel(condition = "input.Name_add_member1.length > 0",
                                 checkboxGroupInput(inputId = "def_members", label = "Define",
                                 choices = list_members)

 )

In the code, the list members generates a list with the unique values in the chosen variable. However, it looks like this line is not executed here. Does anyone know how to solve this?

The code that filters the df to create list_members needs to be in the server part of your Shiny app, because it involves computation using one of your inputs ( input$select_add ). You can create your checkbox group dynamically using renderUI() .

ui <- fluidPage(tagList(
  uiOutput("variables"),

  conditionalPanel(condition = "input.Name_add_member1.length > 0",
                   uiOutput("members"))

))

server <- function(input, output, session) {
  df <- ... # df needs to be defined on the server side or in the global environment

  output$variables <- renderUI({
    pickerInput(
      inputId = "select_add",
      label = "Select variable:",
      choices = colnames(df),
      selected = NULL
    )
  })

  output$members <- renderUI({
    list_members = c(unique(as.data.frame(df)[input$select_add]))
    checkboxGroupInput(inputId = "def_members",
                       label = "Define",
                       choices = list_members)
  })
}

Keep in mind that df needs to be in the global environment and/or in the server() namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM