Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling use of shinyFilters within modules #6

Open
JustinCally opened this issue Jul 14, 2020 · 0 comments · May be fixed by #7
Open

Enabling use of shinyFilters within modules #6

JustinCally opened this issue Jul 14, 2020 · 0 comments · May be fixed by #7

Comments

@JustinCally
Copy link

First of all, this is a great package with the best implementation of simple and intuitive dynamic filters for an app. I have tried to use the package's functionality inside of a custom module and have found it difficult to find a way for the UI to render and update properly. There does not seem to be an intuitive spot to wrap the ID in shiny::NS().

Below is the example used in the package, but modularized, the selection options in this app do not appear.

if(interactive()) {
  
  library(shiny)
  library(shinyjs)
  library(dplyr)
  library(shinyFilters)
  
  # create filterset in global section of app
  filterSet <- newFilterSet('FS1') %>%
    # give each filter unique id and tell it which column to filter on
    addSelectFilter('cylinders','cyl') %>%
    addSelectFilter('gears','gear') %>%
    addSliderFilter('disp',value=c(0,500)) %>%
    addSelectFilter('carb') %>%
    addCustomSliderFilter('hp',value=seq(0,500,50))
  
  ModuleUI <- function(id) {
    ns <- shiny::NS(id)
    
    shiny::fluidPage(
      #shinyjs is required to show/hide filters
      shinyjs::useShinyjs(),
      shiny::sidebarLayout(
        shiny::sidebarPanel(
          filterInputs(filterSet),
          shiny::hr(),
          filterMaster(filterSet),
          filterResetButton(filterSet)
        ),
        shiny::mainPanel(
          DT::dataTableOutput(ns("data"))
        )
      ))
  }
  
  
  ModuleServer <- function(input, output, session, data_f = data_mut, filterSet) {
    # wrap data in reactive expression in case you
    # need to do something to the data before filtering
    data <- shiny::reactive(mtcars)
    
    # initialize the filter set
    filterSet <- initializeFilterSet(filterSet, data)
    
    # the output is a reactive data.frame
    output$data <- DT::renderDataTable(filterSet$output())
    
  }
  
  
  ui <- shiny::shinyUI(ModuleUI("mod_name"))
  
  server <- function(input, output, session) {shiny::callModule(ModuleServer, "mod_name", filterSet = filterSet)}
  
  shiny::shinyApp(ui, server)
}
@JustinCally JustinCally linked a pull request Jul 16, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant