Subset table using multiple pickerInputs from a module

I am trying to use a module that creates a UI containing an "Add" button that, once clicked, generates two pickerInputs. The first pickerInput allows to select a column name from a dataframe (the diamonds dataset here), whereas the second pickerInput should displays the unique values of the column previously selected. The user should also be able to click several times the button in order to select more columns and variables.

I am facing 2 issues:
(i) although I can generate the first pair of pickerInput with the correct values to choose from, the second time I click the button there are no choices on the second pickerInput.
(ii) I cannot manage to return the values selected to the app, and thus cannot subset the original dataset.

Any help and explanations would be greatly appreciated.

library(shiny)
library(shinyWidgets)

### MODULE ###
myModule.UI <- function(id){
  ns <- NS(id)  
  actionButton(inputId = ns("add"), label = "Add")
}

myModule.SERVER <- function(input, output, session, data, columns){
  ns <- session$ns

  module_out <- reactiveValues(var=NULL, val=NULL)  

  observeEvent(input$add, {
    insertUI(
      selector = paste0("#", ns("add")),
      where = "beforeBegin",
      ui = fluidRow(
        column(width = 5,
               pickerInput(inputId = ns("myVariable"),
                           choices = columns,
                           selected = NULL
               )
        ),
        column(width = 5,
               pickerInput(inputId = ns("myValue"),
                           choices = NULL,
                           selected = NULL,
                           options = list(`actions-box` = TRUE), 
                           multiple = TRUE
               )
        )
      )
    )
  })
  
  observeEvent(input$myVariable,{
    req(input$myVariable)
    
    var_choices <- reactive({
      as.character(unlist(unique(data[, input$myVariable])))
    })
    
    updatePickerInput(session,
                      inputId = "myValue",
                      choices = var_choices(),
                      selected = NULL,
                      choicesOpt = list(`actions-box` = TRUE)
    )
    
    module_out <- reactiveValues(var = input$myVariable, val = input$myValue)
  })
  
  return(module_out)
  
}


### APP ###
ui <- fluidPage(
  mainPanel(
    myModule.UI(id = "myID"),
    tableOutput("table_out")
  )
)

server <- function(input, output, session) {
  
  output_data <- callModule(module=myModule.SERVER, id="myID", data = diamonds, columns = colnames(diamonds)[2:4])
  
  # subset table here
  output$table_out <- renderTable({
    # I dont know how `output_data` will look like...
  })
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.