Code for this example is available here.

shufflecards::use_polyfill()

```{css, echo=FALSE} .main-container { max-width: 95% !important; }

```r
knitr::opts_chunk$set(echo = FALSE)
## PACKAGES ----
library(shufflecards)
library(shiny)
library(crosstalk)
library(ggplot2)
library(gapminder)
library(dplyr)

## DATA ---- 
# Sample of countries
samp_c <- c("Portugal", "Cuba", "Tunisia", "Lesotho", "Mauritania", "Chile",
            "Bangladesh", "Slovenia", "Syria", "Poland", "Djibouti", "Myanmar")

gapminder_summary <- gapminder %>% 
  filter(country %in% samp_c) %>% 
  group_by(country = as.character(country), continent = as.character(continent)) %>%
  summarise(lastLifeExp = last(lifeExp))

# crosstalk instance
gapminder_shared <- SharedData$new(gapminder_summary, key = as.character(gapminder_summary$country))

## FUNS ----
plot_data <- function(cntr) {
  dat <- gapminder %>%
    filter(country %in% cntr)
  ggplot(dat) +
    aes(year, y = lifeExp, fill = country) +
    geom_area() +
    coord_cartesian(xlim = c(1948, 2011), ylim = c(10, 95)) +
    theme_minimal() +
    scale_fill_manual(values = country_colors, guide = "none") +
    labs(
      title = cntr, 
      subtitle = sprintf("Life expectancy in 2007: %s years", last(dat$lifeExp)),
      x = "Year", y = "Life expectancy (in years)"
    ) +
    theme(plot.title = element_text(size = 22, face = "bold"), plot.subtitle = element_text(size = 16))
}
bscols(widths = c(8, 4),
  rmd_group_buttons(
    shuffleId = "grid", label = "Click to sort cards !",
    arrange_button("Sort by life expectancy", "lifeExp", numeric = TRUE, icon = icon("sort-numeric-asc")),
    arrange_button("Sort by life expectancy (decreasing)", "lifeExp", numeric = TRUE, desc = TRUE, icon = icon("sort-numeric-desc")),
    arrange_button("Sort by country", "key", icon = icon("sort-alpha-asc")),
    arrange_button("Random!", "random", icon = icon("random"))
  ),
  filter_checkbox("continent_filter", "Continent:", gapminder_shared, ~continent, inline = TRUE)
)
shuffle_widget(
  shuffleId = "grid",
  shared_data = gapminder_shared,
  options = shuffle_options(is_centered = TRUE),
  no_card = "No plot in selection",
  card_list = lapply(
    X = samp_c,
    FUN = function(x) {
      # Last life expectancy value
      lastLifeExp <- gapminder_summary %>%
        filter(country == x) %>%
        pull(lastLifeExp)
      shuffle_card(
        key = x, # for sort & filter
        lifeExp = lastLifeExp, # for sorting
        as_girafe(plot_data(x))
      )
    }
  )
)


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