-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.Rmd
126 lines (108 loc) · 3.42 KB
/
index.Rmd
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
title: "shufflecards with flexdashboard & crosstalk"
output:
flexdashboard::flex_dashboard:
self_contained: false
---
<!-- content will not fit in the page -->
```{css, echo=FALSE}
html, body {
height: auto !important;
overflow-y: scroll !important;
}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
## PACKAGES ----
library(shufflecards)
library(shiny)
library(crosstalk)
library(highcharter)
library(gapminder)
library(dplyr)
## DATA ----
# sample of countries from gapminder dataset
countries_samp <- sample(unique(gapminder$country), 15)
# min & max from gdpPercap
range_gdpPercap <- gapminder %>%
filter(country %in% countries_samp) %>%
pull(gdpPercap) %>%
range %>%
pretty %>% # only to take inferior & superior bound
range
# Data we'll use with crosstalk to filter
gapminder_summary <- gapminder %>%
filter(country %in% countries_samp) %>%
group_by(country, continent) %>%
summarise(max_gdpPercap = round(max(gdpPercap)))
# crosstalk object, it's important to define a key !
gapminder_shared <- SharedData$new(gapminder_summary, key = as.character(gapminder_summary$country))
## FUNS ----
# function to make plots with highcharter
plot_data <- function(cntr, range) {
highchart(height = "250px") %>%
hc_chart(type = "area") %>%
hc_add_series(
name = "GDP",
color = unname(country_colors[cntr]),
data = gapminder %>%
filter(country == cntr) %>%
select(x = year, y = gdpPercap)
%>% list_parse()
) %>%
hc_legend(enabled = FALSE) %>%
hc_plotOptions(area = list(
marker = list(enabled = FALSE),
fillOpacity = 1
)) %>%
hc_title(text = cntr) %>%
hc_xAxis(title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "GDP per capita (US$)"),
min = range[1], max = range[2])
}
```
Inputs {.sidebar}
-------------------------------------
<!-- crosstalk inputs to filter grid -->
```{r}
filter_slider("gdp", "Filter by GDP:", gapminder_shared, ~max_gdpPercap, pre = "$")
filter_select("continent", "Continent:", gapminder_shared, ~continent)
```
Column
-------------------------------------
Code for this example is available [here](https://github.com/dreamRs/shufflecards/blob/master/inst/examples/gapminder-flexdashboard/index.Rmd).
<!-- buttons to arrange grid -->
```{r}
rmd_group_buttons(
shuffleId = "mygrid", label = "Click to arrange charts:",
arrange_button("Sort by name", "key", icon = icon("sort-alpha-asc")),
arrange_button("Sort by continent", "continent", icon = icon("sort-alpha-asc")),
arrange_button("Sort by maximal GDP", "maxGDP", desc = TRUE, icon = icon("sort-numeric-asc")),
arrange_button("Random!", "random", icon = icon("random"))
)
```
<!-- grid of plots -->
```{r}
shuffle_widget(
shuffleId = "mygrid",
shared_data = gapminder_shared,
no_card = "No chart to display!",
options = shuffle_options(
is_centered = FALSE,
column_width = I("function(containerWidth) {return 0.32 * containerWidth;}"),
gutter_width = I("function(containerWidth) {return 0.01 * containerWidth;}")
),
card_list = lapply(
X = countries_samp,
FUN = function(x) {
shuffle_card(
width = "32%",
key = x, # for filtering
continent = gapminder_summary %>% filter(country == x) %>% pull(continent), # for sorting
maxGDP = gapminder_summary %>% filter(country == x) %>% pull(max_gdpPercap), # for sorting
plot_data(x, range_gdpPercap)
)
}
)
)
```