filter_cards: Filter a Shuffle grid layout with custom values

Description Usage Arguments Examples

View source: R/update.R

Description

Use in a Shiny app to filter cards by a custom value.

Usage

1
2
3
4
5
6
filter_cards(
  shuffleId,
  by,
  filters,
  session = shiny::getDefaultReactiveDomain()
)

Arguments

shuffleId

The id of the shuffle container.

by

Key defined in shuffle_card to filter elements, can be "id" to refer to the unique ID associated with the card.

filters

Possible values are:

- named list: where names match the key used in by and values are TRUE or FALSE

- two-columns data.frame: where first column contains names and second TRUE or FALSE

- one-column data.frame: where the column contains cards key to display (those absent will be hided)

- character vector: containing cards key to display

session

The session object passed to function given to shinyServer.

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
if (interactive()) {
  library(ggplot2)
  library(dplyr)
  library(shiny)
  library(shufflecards)



  # utility fun
  is.even <- function(x) x %% 2 == 0

  ui <- fluidPage(
    tags$h2("Filter a Shuffle Grid"),
    fluidRow(
      column(
        width = 3,
        radioButtons(
          inputId = "type_num",
          label = "Odd or Even",
          choices = c("All", "Odd", "Even")
        ),
        sliderInput(
          inputId = "val_num",
          label = "Value:",
          min = 1, max = 12,
          value = c(1, 12)
        )
      ),
      column(
        width = 9,
        shuffle_container(
          shuffleId = "gridNum",
          card_list = lapply(
            X = 1:12,
            FUN = function(i) {
              shuffle_card(
                id = paste0("card", i), # set an ID to cards to use server-side
                plotOutput(outputId = paste0("plot", i), width = "250px", height = "250px")
              )
            }
          )
        )
      )
    )
  )

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

    # Make individual plots ----
    lapply(
      X = 1:12,
      FUN =  function(i) {
        output[[paste0("plot", i)]] <- renderPlot({
          ggplot() + geom_text(aes(1, 1, label = i), size = 50)
        })
      }
    )

    # Filters ----
    observe({
      if (input$type_num == "All") {
        type_num <- c("even", "odd")
      } else {
        type_num <- tolower(input$type_num)
      }

      # Create a df to filters values
      data_frame(num = 1:12) %>%
        mutate(
          id = paste0("card", num), # card's ID
          type = if_else(is.even(num), "even", "odd")
        ) %>%
        filter(
          type %in% type_num,      # filter df to keep desired cards
          num >= input$val_num[1],
          num <= input$val_num[2]
        ) %>%
        pull(id) %>% # extract only id
        filter_cards(
          session = session,
          shuffleId = "gridNum",
          by = "id",
          filters = .  # <- Vector of IDs to display
        )
    })
  }

  shinyApp(ui, server)
}

dreamRs/shufflecards documentation built on Dec. 4, 2019, 4:44 a.m.