jeudi 16 mars 2023

Rshiny : How to filter the columns of a dataset with the values of a checkboxinput

I have a modular Rshiny application where I want to retrieve selected values from a checkboxgroup. These values correspond to the columns of a dataset. I want to be able to filter/display these columns if they are checked or not.

The checkboxgroupinput is in a renderUi object because I want the choices offered to be automatically read by an internal variable.

library(shiny)

mod_1_ui <- function(id) {
  ns <- NS(id)
  
  fluidRow(
    column(
      width = 3,
      uiOutput(ns("my_ui_output"))
    ),
    column(
      width = 9,
      verbatimTextOutput(ns("result"), placeholder = TRUE),
      DT::DTOutput(ns('table'))
    )
  )
}


mod_1_server <- function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    
    output$my_ui_output <- renderUI({
      checkboxGroupInput(inputId = "check_col",
                         label = "Select columns to display : ",
                         choices = colnames(iris),
                         selected = colnames(iris)
                         )
    })
    
  
    selected = reactive({
      input$check_col
    })
    
    output$result = renderPrint({
      paste(selected())
    })
    
    output$table = DT::renderDataTable({
      dt = data.table(iris)
      # dt[, selected(), with = F]
    })
    
    
  })
}



ui <- fluidPage(
  mod_1_ui("mod")
)

server <- function(input, output, session) {
  r_global <- reactiveValues()
  
  mod_1_server("mod", r_global = r_global)
}

if (interactive()) {
  shinyApp(ui, server)
}



Aucun commentaire:

Enregistrer un commentaire