update_filters: Update the filter options in each filter when the selection...

Description Usage Arguments Details Value Examples

View source: R/shinyfilter.r

Description

Updates all filters linked to a reactable. As shinyfilter filters are interdependent, update_filters() makes sure that each filter (selectizeInput widget) only shows the filter options currently available, given the selection in all other filters.

Usage

1
update_filters(input, session, react_id)

Arguments

input

The input object provided as an argument to the server function.

session

The session variable provided as an argument to the server function.

react_id

The output variable/ID of the reactable for which filters will be updated.

Details

See below for a full example of a shiny app using shinyfilter. See the README.md file or the GitHub repo on https://github.com/jsugarelli/shinyfilter for a comprehensive shinyfilter tutorial.

Value

The filtered dataframe to be presented in the reactable widget. Ideally, this is captured in a reactive value so that the reactable updates automatically.

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
if(interactive()) {
  library(shiny)
  library(reactable)
  library(shinyfilter)

  cars_csv <- system.file("cars.csv", package="shinyfilter")

  cars <- read.csv(cars_csv, stringsAsFactors = FALSE, header = TRUE, encoding = "UTF-8")

  app = shinyApp(
    ui <- fluidPage(
      titlePanel("Cars Database"),
      sidebarLayout(
        sidebarPanel(
          width = 2,

          selectizeInput(inputId = "sel_manufacturer", label = "Manufacturer",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$manufacturer))),

          selectizeInput(inputId = "sel_year", label = "Year",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$year))),

          selectizeInput(inputId = "sel_fuel", label = "Fuel",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$fuel))),

          selectizeInput(inputId = "sel_condition", label = "Condition",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$condition))),

          selectizeInput(inputId = "sel_size", label = "Size",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$size))),

          selectizeInput(inputId = "sel_transmission", label = "Transmission",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$transmission))),

          selectizeInput(inputId = "sel_color", label = "Color",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$paint_color))),

          selectizeInput(inputId = "sel_type", label = "Type",
                         multiple = TRUE, options = list(onChange = event("ev_click")),
                         choices = sort(unique(cars$type))),
          use_tooltips(background = "#'  1B3F8C", foreground = "#'  FFFFFF")
        ),
        mainPanel(
          reactableOutput(outputId = "tbl_cars")
        )
      )
    ),


    server = function(input, output, session) {

      r <- reactiveValues(mycars = cars)

      define_filters(input,
                     "tbl_cars",
                     c(sel_manufacturer = "manufacturer",
                       sel_year = "year",
                       sel_fuel = "fuel",
                       sel_condition = "condition",
                       sel_size = "size",
                       sel_transmission = "transmission",
                       sel_color = "paint_color",
                       sel_type = "type"),
                     cars)


      observeEvent(input$ev_click, {
        r$mycars <- update_filters(input, session, "tbl_cars")
        update_tooltips("tbl_cars",
                        session,
                        tooltip = TRUE,
                        title_avail = "Available is:",
                        title_nonavail = "Currently not available is:",
                        popover_title = "My filters",
                        max_avail = 10,
                        max_nonavail = 10)
      })


      output$tbl_cars <- renderReactable({
        reactable(data = r$mycars,
                  filterable = TRUE,
                  rownames = FALSE,
                  selection = "multiple",
                  showPageSizeOptions = TRUE,
                  paginationType = "jump",
                  showSortable = TRUE,
                  highlight = TRUE,
                  resizable = TRUE,
                  rowStyle = list(cursor = "pointer"),
                  onClick = "select"
        )
      })

    }
   )

   runApp(app)
 }

shinyfilter documentation built on May 11, 2021, 1:07 a.m.