GithubHelp home page GithubHelp logo

davesteps / shinyfilters Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 10.0 50 KB

Cascading filter modules for Shiny

Home Page: https://davesteps.shinyapps.io/shinyFilters/

License: Apache License 2.0

R 100.00%
shiny shiny-apps shiny-server r

shinyfilters's Introduction

shinyFilters

The idea of shinyFilters is to allow quick and easy filtering of data.frames in Shiny.

  • The filter choices are cascading - If the user chooses 'USA' and 'Asia' in filter 1. All subsequent filters will be updated to only contain choices which meet this criteria.

  • Enable/disable child filter based on condition of parent - Filter 2 is only enabled when 'USA' or 'Asia' are selected in filter 1.

Installation

Install using the devtools package

# Install devtools, if you haven't already.
install.packages("devtools")

devtools::install_github("davesteps/shinyFilters")

Usage

Example 1 (see here)

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))


ui <- fluidPage(
  #shinyjs is required to show/hide filters
  useShinyjs(),
  sidebarLayout(
    sidebarPanel(
      filterInputs(filterSet),
      hr(),
      filterMaster(filterSet),
      filterResetButton(filterSet)
    ),
    mainPanel(
      DT::dataTableOutput("data")
    )
  )
)

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

  # wrap data in reactive expression in case you
  # need to do something to the data before filtering
  data <- reactive(mtcars)

  # initialize the filter set
  filterSet <- initializeFilterSet(filterSet, data)

  # the output is a reactive data.frame
  output$data <- DT::renderDataTable(filterSet$output())

}

shinyApp(ui, server)

shinyfilters's People

Contributors

davesteps avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

shinyfilters's Issues

dplyr::select_ is used in codebase but library dplyr is nowhere required

dplyr's select_ is used but dplyr only appears in this codebase in the examples.

I generally don't use tidyverse, so my shiny app didn't call for it, producing this error

Warning: Error in select_: could not find function "select_"
  54: unique
  53: sort
  52: %>%
  51: observe
  50: <observer>
   7: shiny::runApp

adding library(dplyr) to my shiny app solved the issue, but I would consider it a workaround.

I'm not a packaging expert, but assume this can be fixed by adding library(dplyr) to shinyFilters.R.

Hiding filterMaster

Hello,
It appears that filterMaster is not an optional function. Is it possible to hide it in the UI?

Change shinyFilters Install Instructions

Hello Dave,

Thank you for the great package. Appreciate your work.

I was re-installing the package using the instructions in the GIT repo and noticed that the current method doesn't work anymore.
devtools::install_github("davesteps/shinyFilters")

I was able to successfully install and load using the following method.
devtools::install_git("https://github.com/davesteps/shinyFilters.git")

question: horizontal filtering

Hi @davesteps,

Thank you for this nice initiative. I have tried the example 1 and it seems that the package implements cascading filtering, i.e. children filters depend on filter 1. Have you tried developing a horizontal filtering, i.e. any filter's value change updates all other filter's values and choices in the filters are always reflecting the values in the current subset of data?
Do you think this is eve possible to do?

Thank you so much for your time and help!

Best regards,
Edgar

Enabling use of shinyFilters within modules

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)
}

set default (selected) for selectizeInput

I'm trying to set the default value selected for each of my filterInputs. The docs suggest that I can pass on parameters for selectizeInput through ... but using selected='myvalue' doesn't seem to work.

This is for shinyFilters==0.1.0.

filter output

Is it possible to only show certain columns in the output? For example, using your example script, if I change the call to DT::renderDataTable to just show the mpg and cyl columns, the table is indeed filtered, but is no longer reactive or responding to the filters selected.

  output$data <- filterSet$output() %>%
    select(mpg, cyl) %>%
    DT::renderDataTable()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.