-
Notifications
You must be signed in to change notification settings - Fork 47
/
shinyapps.R
346 lines (315 loc) · 14.6 KB
/
shinyapps.R
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Code to generate the shinyapps.io applications
# Deploy on MacOS (seems to be less prone to stupid locale and enconding issues)
devtools::install_github("joachim-gassen/ExPanDaR")
# --- expand -------------------------------------------------------------------
library(ExPanDaR)
ExPanD(export_nb_option = TRUE)
# expand_r3
ExPanD(
df = russell_3000, df_def = russell_3000_data_def,
config_list = ExPanD_config_russell_3000,
abstract = paste(
"The data for this sample has been collected from Google Finance",
"and Yahoo Finance."
),
export_nb_option = TRUE
)
# if you need a new config file - Save from within ExPanD and then
if (FALSE) {
ExPanD_config_russell_3000 <- readRDS("~/Downloads/ExPanD.RDS")
save(
ExPanD_config_russell_3000, file = "data/ExPanD_config_russell_3000.RData",
version = 2
)
}
# --- expand_wb ----------------------------------------------------------------
ExPanD(
worldbank, df_def = worldbank_data_def, var_def = worldbank_var_def,
config_list = ExPanD_config_worldbank, df_name = "World Bank Data",
abstract = paste(
"The data for this sample has been collected using the",
"<a href=https://data.worldbank.org>World Bank API</a>."
),
export_nb_option = TRUE
)
# --- expand_fuel_economy ------------------------------------------------------
# See https://joachim-gassen.github.io/2019/04/customize-your-interactive-eda-explore-the-fuel-economy-of-the-u.s.-car-market/
# for more info
# The following two chuncks borrow
# from the raw data code of the
# fueleconomy package by Hadley Wickham,
# See: https://github.com/hadley/fueleconomy
library(tidyverse)
library(ExPanDaR)
if(!file.exists("vehicles.csv")) {
tmp <- tempfile(fileext = ".zip")
download.file("http://www.fueleconomy.gov/feg/epadata/vehicles.csv.zip",
tmp, quiet = TRUE)
unzip(tmp, exdir = ".")
}
raw <- read.csv("vehicles.csv", stringsAsFactors = FALSE)
countries <- read.csv("https://joachim-gassen.github.io/data/countries.csv",
stringsAsFactors = FALSE)
vehicles <- raw %>%
mutate(car = paste(make, model, trany),
mpg_hwy = ifelse(highway08U > 0, highway08U, highway08),
mpg_city = ifelse(city08U > 0, city08U, city08)) %>%
left_join(countries) %>%
select(car, make, country, trans = trany,
year,
class = VClass, drive = drive, fuel = fuelType,
cyl = cylinders, displ = displ,
mpg_hwy, mpg_city) %>%
filter(drive != "",
year > 1985,
year < 2020) %>%
mutate(fuel = case_when(
fuel == "CNG" ~ "gas",
fuel == "Gasoline or natural gas" ~ "hybrid_gas",
fuel == "Gasoline or propane" ~ "hybrid_gas",
fuel == "Premium and Electricity" ~ "hybrid_electro",
fuel == "Premium Gas or Electricity" ~ "hybrid_electro",
fuel == "Premium Gas and Electricity" ~ "hybrid_electro",
fuel == "Regular Gas or Electricity" ~ "hybrid_electro",
fuel == "Electricity" ~ "electro",
fuel == "Diesel" ~ "diesel",
TRUE ~ "gasoline"
),
class = case_when(
grepl("Midsize", class) ~ "Normal, mid-size",
grepl("Compact", class) ~ "Normal, compact",
grepl("Small Station Wagons", class) ~ "Normal, compact",
grepl("Large Cars", class) ~ "Normal, large",
grepl("Minicompact", class) ~ "Normal, sub-compact",
grepl("Subcompact", class) ~ "Normal, sub-compact",
grepl("Two Seaters", class) ~ "Two Seaters",
grepl("Pickup Trucks", class) ~ "Pickups",
grepl("Sport Utility Vehicle", class) ~ "SUVs",
grepl("Special Purpose Vehicle", class) ~ "SUVs",
grepl("Minivan", class) ~ "(Mini)vans",
grepl("Vans", class) ~ "(Mini)vans"
),
drive = case_when(
grepl("4-Wheel", drive) ~ "4-Wheel Drive",
grepl("4-Wheel", drive) ~ "4-Wheel Drive",
grepl("All-Wheel", drive) ~ "4-Wheel Drive",
grepl("Front-Wheel", drive) ~ "Front-Wheel Drive",
grepl("Rear-Wheel", drive) ~ "Rear-Wheel Drive"
),
trans = case_when(
grepl("Automatic", trans) ~ "Automatic",
grepl("Manual", trans) ~ "Manual"
)) %>%
na.omit()
df_def <- data.frame(
var_name = names(vehicles),
var_def = c("Make, model and transition type indentifying a unique car in the data",
"Make of car",
"Country where car producing firm is loacted",
"Transition type (automatic or manual)",
"Year of data",
"Classification type of car (simplified from orginal data)",
"Drive type of car (Front Wheel, Rear Wheel or 4 Wheel)",
"Fuel type (simplified from orginal data)",
"Number of engine cylinders",
"Engine displacement in liters",
"Highway miles per gallon (MPG). For electric and CNG vehicles this number is MPGe (gasoline equivalent miles per gallon).",
"City miles per gallon (MPG). For electric and CNG vehicles this number is MPGe (gasoline equivalent miles per gallon)."),
type = c("cs_id", rep("factor", 3), "ts_id", rep("factor", 3), rep("numeric", 4))
)
html_blocks <- c(
paste("<div class='col-sm-12'>",
"By default, this display uses all data from car makes with more",
"than 100 cars in the 'fueleconomy.gov' database.",
"Above, you can limit the analysis to cars from a certain make,",
"class, country, fuel type or other factor present in the data.",
"</div>"),
paste("<div class='col-sm-12'>",
"In the display above, remove the check mark to see the absolute",
"number of cars included in the data each year.",
"Also, change the additional factor to see how the distribution",
"of cars across countries, transition types, etc. changes over time",
"</div>"),
paste("<div class='col-sm-12'>",
"In the two tables above, you can assess the distributions of the",
"four numerical variables of the data set. Which car has the",
"largest engine of all times?",
"</div>"),
paste("<div class='col-sm-12'>",
"Explore the numerical variables across factors. You will see,",
"not surprisingly, that fuel economy varies by car class.",
"Does it also vary by drive type?",
"</div>"),
paste("<div class='col-sm-12'>",
"The above panels contain good news. Fuel economy has",
"increased over the last ten years. See for yourself:",
"Has the size of engines changed as well?",
"</div>"),
paste("<div class='col-sm-12'>",
"The scatter plot documents a clear link between engine size",
"and fuel economy in term of miles per gallon.",
"Below, you can start testing for associations.",
"</div>"),
paste("<div class='col-sm-12'>",
"Probably, you will want to test for some associations that",
"require you to construct new variables. No problem. Just enter the",
"variable definitions above. Some ideas on what to do:",
"<ul><li>Define country dummies (e.g., country == 'US') to see",
"whether cars from certain countries are less fuel efficient than others.</li>",
"<li>Define a dummy for 4-Wheel drive cars to assess the penalty",
"of 4-Wheel drives on fuel economy.</li>",
"<li>If you are from a metric country, maybe your are mildly annoyed",
"by the uncommon way to assess fuel economy via miles per gallon.",
"Fix this by defining a liter by 100 km measure",
"(hint: 'l100km_hwy := 235.215/mpg_hwy').</li></ul>",
"</div>"),
paste("<div class='col-sm-12'>",
"Above, you can play around with certain regression parameters.",
"See how robust coefficients are across car classes by estimating",
"the models by car class ('subset' option).",
"Try a by year regression to assess the development of fuel economy",
"over time. <br> <br>",
"If you like your analysis, you can download a zipfile containing",
"the data and an R notebook reporting the analysis. Alternatively,",
"you can store the ExPanD configuration and reload it at a later",
"stage.",
"</div>")
)
cl <- list(
ext_obs_period_by = "2019",
bgbg_var = "mpg_hwy",
bgvg_var = "mpg_hwy",
scatter_loess = FALSE,
delvars = NULL,
scatter_size = "cyl",
bar_chart_relative = TRUE,
reg_x = c("cyl", "displ", "trans"),
scatter_x = "displ",
reg_y = "mpg_hwy",
scatter_y = "mpg_hwy",
bgvg_byvar = "class",
quantile_trend_graph_var = "mpg_hwy",
bgtg_var = "mpg_hwy",
bgtg_byvar = "class",
bgbg_byvar = "country",
scatter_color = "country", bar_chart_var2 = "class",
ext_obs_var = "mpg_hwy",
trend_graph_var1 = "mpg_hwy",
trend_graph_var2 = "mpg_city",
sample = "vehicles"
)
abstract <- paste(
"This interactive display features the",
"<a href=https://www.fueleconomy.gov/>",
"fuel economy data provided by the U.S. Environmental Protection Agency.</a>",
"It allows you to explore the fuel economy of cars in the U.S. market",
"across time and other dimensions.",
"<br> <br>",
"It is based on the 'ExPanD' display provided by the",
"<a href=https://joachim-gassen.github.io/ExPanDaR>'ExPanDaR' package</a>.",
"Click <a href=https://jgassen.shinyapps.io/expand>here</a> to explore your",
"own data with 'ExPanD'.",
"<br> <br>",
"Otherwise: Scroll down and start exploring!"
)
ExPanD(vehicles, df_def = df_def, config_list = cl,
title = "Explore the Fuel Economy of Cars in the U.S. Market",
abstract = abstract,
components = c(subset_factor = TRUE,
html_block = TRUE,
bar_chart = TRUE,
html_block = TRUE,
descriptive_table = TRUE,
ext_obs = TRUE,
html_block = TRUE,
by_group_bar_graph = TRUE,
by_group_violin_graph = TRUE,
html_block = TRUE,
trend_graph = TRUE,
quantile_trend_graph = TRUE,
by_group_trend_graph = TRUE,
html_block = TRUE,
scatter_plot = TRUE,
html_block = TRUE,
udvars = TRUE,
html_block = TRUE,
regression = TRUE,
html_block = TRUE),
html_blocks = html_blocks,
export_nb_option = TRUE
)
# --- expand_gapminder ---------------------------------------------------------
library(ExPanDaR)
library(gapminder)
data(gapminder)
df_def <- data.frame(
var_name = names(gapminder),
var_def = c("Name of the country",
"Continent where country is located",
"Year of data",
"Life expectancy in years at birth",
"Population in million",
"Gross Domestic Product (GDP) per capita"),
type = c("cs_id", "factor", "ts_id", rep("numeric", 3)),
stringsAsFactors = FALSE
)
gapminder$pop <- gapminder$pop / 1e6
clist <- list(
scatter_x = "gdpPercap",
scatter_y = "lifeExp",
scatter_size = "pop",
scatter_color = "continent",
scatter_loess = TRUE,
scatter_sample = FALSE,
reg_y = "lifeExp",
reg_x = "gdpPercap",
reg_fe1 = "country",
reg_fe2 = "year",
cluster = "4" # No this is hard to guess 1: none, 2: first FE, 3: second FE, 4: both FE
)
html_blocks <- c(
paste('<div class="col-sm-2"><h3>Variation of life expectancy',
"across regions and income levels</h3></div>",
'<div class="col-sm-10">',
"<p> </p>As you see below, life expectancy varies widely",
"across countries and continents. One potential reason for this",
"variation is the difference in income levels across countries.",
"This association is visualized by the",
"<a href=https://en.wikipedia.org/wiki/Preston_curve>",
"Preston Curve</a> that you also find below.",
"</div>"),
paste('<div class="col-sm-2"><h3>Transform variables</h3></div>',
'<div class="col-sm-10">',
"The Preston Curve is far from",
"linear. Maybe you can come up with a transformation",
"of GDP per capita that makes the association",
"a little bit more well behaved?",
"Use the dialog below to define a transformed",
"measure of GDP per capita and assess its association",
"with life expectancy in the scatter plot above.",
"</div>"),
paste('<div class="col-sm-2"><h3>Assess Robustness</h3></div>',
'<div class="col-sm-10">',
"You see below that the linear regression coefficient",
"for GDP per capita is <i>negative</i>",
"and signficant in a panel model with country and year",
"fixed effects.",
"Does this also hold when you use a log-transformed version",
"of GDP per capita?",
"</div>")
)
ExPanD(df = gapminder,
title = "Explore the Preston Curve",
abstract = paste("This interactive display uses 'gapminder' data to",
"let you explore the Preston Curve. Scroll down and enjoy!"),
components = c(descriptive_table = TRUE,
html_block = TRUE,
by_group_violin_graph = TRUE,
scatter_plot = TRUE,
html_block = TRUE,
udvars = TRUE,
html_block = TRUE,
regression = TRUE),
df_def = df_def,
config_list = clist,
html_blocks = html_blocks)