-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.r
627 lines (576 loc) · 25.3 KB
/
backup.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#backup
#Project initial
library(shiny)
library(shinythemes)
library(readxl)
library(rgdal)
library(tidyverse)
library(stringr)
library(debkeepr)
library(leaflet)
#source to function file
source('functions.R')
latLongZoom.original <- data.frame("Area" = c("World", "Europe", "Africa",
"Middle East", "Pacfic Islands", "Asia"),
"Lat" = c(30, 49.8, -6, 27, 0, 32),
"Long" = c(53, 15.47, 30, 72.5, 116, 115),
"Magnify" = c(2, 4.25, 2.5, 4, 4, 3.25))
latLongZoom <- latLongZoom.original
#Read in the data
joined.data.original <- read_csv("joined.csv")
map.data.original <- readOGR("filteredCountries.GeoJSON")
#make copies of original data
joined.data <- joined.data.original
map.data <- map.data.original
#Creating the UI
ui <- fluidPage(theme = shinytheme("darkly"),
titlePanel("Interactive Textile Explorer"),
sidebarPanel(#All inputs will go in this sidebarPanel
radioButtons(inputId = "dataSet",
label = "Choose company of interest",
choices = c("WIC", "VOC", "Both"),
selected = "Both"),
radioButtons(inputId = "dataType",
label = "Choose data type of interest",
choices = c("Quantity", "Value"),
selected = "Quantity"),
radioButtons(inputId = "regionChoice",
label = "Select one",
choices = c("Origin", "Destination"),
selected = "Origin"),
selectizeInput(inputId = "zoomTo",
label = "Zoom to:",
choices = levels(factor(latLongZoom$Area)),
selected = "World"),
selectizeInput(inputId = "textileName",
label = "Choose textile(s) of interest",
choices = levels(factor(joined.data$textile_name)),
multiple = TRUE),
# uiOutput(outputId = 'filtered_inputs'),
selectizeInput(inputId = "colors",
label = "Choose color(s) of interest",
choices = levels(factor(joined.data$colorGroup)),
multiple = TRUE),
selectizeInput(inputId = "patterns",
label = "Choose pattern(s) of interest",
choices = levels(factor(joined.data$textile_pattern_arch)),
multiple = TRUE),
selectizeInput(inputId = "process",
label = "Choose process(es) of interest",
choices = levels(factor(joined.data$textile_process_arch)),
multiple = TRUE),
selectizeInput(inputId = "fibers",
label = "Choose fiber(s) of interest",
choices = levels(factor(joined.data$textile_fiber_arch)),
multiple = TRUE),
selectizeInput(inputId = "geography",
label = "Choose geography of interest",
choices = levels(factor(joined.data$textile_geography_arch)),
multiple = TRUE),
selectizeInput(inputId = "qualities",
label = "Choose quality(s) of interest",
choices = levels(factor(joined.data$textile_quality_arch)),
multiple = TRUE),
selectizeInput(inputId = "inferredQualities",
label = "Choose inferred quality(s) of interest",
choices = levels(factor(joined.data$textile_quality_inferred)),
multiple = TRUE),
actionButton(inputId = "updateBtn",
label = "Click to update map!"),
actionButton(inputId = 'table_updateBtn',
label = 'Click to update table!'),
br(), br(), #The inputs for the pie chart and bar chart
selectInput(inputId = "pieChart",
label = "Choose a modifier for the pie chart:",
choices = c(colnames(joined.data[,c(19:26)])),
selected = "textile_name"),
checkboxInput(inputId = "omitNAs",
label = "Omit NAs in charts"),
selectInput(inputId = "barChart",
label = "Choose a modifier for the bar chart:",
choices = c(colnames(joined.data[,c(19:26)])),
selected = "textile_name"),
checkboxInput(inputId = "facet",
label = "Facet by modifier")
),
mainPanel(
tabsetPanel(#All of the outputs go here (introduction, map/graphs, data tables)
tabPanel(title= "Introduction",
h2("Dutch Textile Trade from 1710 to 1715"),
h5("Interact with Dutch West India Company (WIC) and East India Company (VOC) textile shipments from 1710 to 1715, with data compiled by Kehoe and Anderson. The Map Explorer allows the user to choose a company and data type of interest, while filtering by textile modifiers, and displays an interactive world map with a complementary pie chart and bar chart when a specific country is selected. The Table Explorer displays the compiled and cleaned dataset.")),
tabPanel(title = "Map Explorer",
leafletOutput(outputId = "countriesMap"),
plotOutput(outputId = "pieChart"),
plotOutput(outputId = "barChart")
),
tabPanel(title = "Table Explorer",
dataTableOutput('update_inputs'))
)
)
)
server <- function(input, output, session) {
#can we filter by origin or dest yr?
#and maybe we can put bubbles on the graph at lat/long for orig and dest, highlight the orig or dest
#button when the other is clicked
#Render the data table based on the given search
#let's modify this to allow hiding of inputs as well
output$update_inputs <- renderDataTable(searchDelay = 1000,{
input$table_updateBtn
isolate(filter_by_inputs(joined.data.original,isolate(input)))}) #filters the data for what has been searched
# output$filtered_inputs <- renderUI({
#
# filtered <- filter_by_inputs(joined.data.original,input)
#
# mainPanel(selectizeInput(inputId = "colors",
# label = "Choose color(s) of interest",
# choices = levels(factor(filtered$colorGroup)),
# multiple = TRUE),
# selectizeInput(inputId = "patterns",
# label = "Choose pattern(s) of interest",
# choices = levels(factor(filtered$textile_pattern_arch)),
# multiple = TRUE),
# selectizeInput(inputId = "process",
# label = "Choose process(es) of interest",
# choices = levels(factor(filtered$textile_process_arch)),
# multiple = TRUE),
# selectizeInput(inputId = "fibers",
# label = "Choose fiber(s) of interest",
# choices = levels(factor(filtered$textile_fiber_arch)),
# multiple = TRUE),
# selectizeInput(inputId = "geography",
# label = "Choose geography of interest",
# choices = levels(factor(filtered$textile_geography_arch)),
# multiple = TRUE),
# selectizeInput(inputId = "qualities",
# label = "Choose quality(s) of interest",
# choices = levels(factor(filtered$textile_quality_arch)),
# multiple = TRUE))
#
#
# })
#The map of countries to be rendered
output$countriesMap <- renderLeaflet({
#We only want it to update when the updateBtn is pushed
input$updateBtn
#reading in all of the inputs, isolating them
dataSet <- isolate(input$dataSet)
dataType <- isolate(input$dataType)
regionChoice <- isolate(input$regionChoice)
textileName <- isolate(input$textileName)
colors <- isolate(input$colors)
patterns <- isolate(input$patterns)
process <- isolate(input$process)
fibers <- isolate(input$fibers)
geography <- isolate(input$geography)
qualities <- isolate(input$qualities)
inferredQualities <- isolate(input$inferredQualities)
area <- isolate(input$zoomTo)
table_update <- isolate(input$table_updateBtn)
#Every time, we want to start with all of the data to filter through
joined.data <- joined.data.original
#Use the function to filter the inputs
joined.data <- filter_by_inputs(joined.data,input)
####WHY IS THIS NOT ISOLATED?####
if(regionChoice == "Destination"){ #Decide which to filter by
joined.data <- isolate(filter_by_inputs(joined.data,isolate(input))) #filter by the chosen inputs
if(dataSet != "Both"){ #if they are interested in a specific company
totalValues <- joined.data %>% #Total values to graph things later on and color the map
filter(company == dataSet) %>% filter_totalValue()
# group_by(dest_country) %>%
# select(dest_country, textile_quantity, deb_dec) %>%
# na.omit() %>% #If it is missing these columns, it will cause issues
# summarise(total_Quant = sum(textile_quantity),
# total_Dec = sum(deb_dec))
}
else{ #This will use data from both companies
totalValues <- filter_totalValue(joined.data)
#joined.data %>%
# group_by(dest_country) %>%
# select(dest_country, textile_quantity, deb_dec) %>%
# na.omit() %>%
# summarise(total_Quant = sum(textile_quantity),
# total_Dec = sum(deb_dec))
}
map.data@data <- left_join(map.data.original@data, #Join with the map data, using the original map data each time
totalValues,
by = c("ADMIN" = "dest_country"))
}
else{ #Redo everything from before, except now using orig_country
if(dataSet != "Both"){
totalValues <- joined.data %>%
filter(company == dataSet) %>%
group_by(orig_country) %>%
select(orig_country, textile_quantity, deb_dec) %>%
na.omit() %>%
summarise(total_Quant = sum(textile_quantity),
total_Dec = sum(deb_dec))
}
else{
totalValues <- joined.data %>%
group_by(orig_country) %>%
select(orig_country, textile_quantity, deb_dec) %>%
na.omit() %>%
summarise(total_Quant = sum(textile_quantity),
total_Dec = sum(deb_dec))
}
map.data@data <- left_join(map.data.original@data,
totalValues,
by = c("ADMIN" = "orig_country"))
}
#This will be used to zoom to a specific region on the map
latLongZoom <- latLongZoom.original %>%
filter(Area == area)
viewLat <- latLongZoom[,"Lat"]
viewLong <- latLongZoom[,"Long"]
viewZoom <- latLongZoom[,"Magnify"]
# if(dataType == "Quantity"){ #This will show the total quantity of textiles
# bins <- totalValues$total_Quant %>%
# auto_bin() #Custom function for determining the bins we will use
#
# country.colors <- colorBin(palette = "YlOrRd",
# domain = totalValues$total_Quant,
# bins = bins)
# #Mapping the data
# map.data %>%
# leaflet() %>%
# addTiles() %>%
# addPolygons(fillColor = ~country.colors(total_Quant), #We only use polygons for countries we may have actually used
# fillOpacity = .7,
# color = "black",
# opacity = 1,
# weight = 1,
# label = ~ADMIN,
# popup = ~paste("Total Quantity:", format(ifelse(is.na(total_Quant), 0, total_Quant), big.mark = ",", scientific = FALSE), sep = " "),
# layerId = ~ADMIN) %>%
# setView(lat = viewLat, lng = viewLong, zoom = viewZoom) %>%
# addLegend(pal = country.colors,
# values = map.data@data$ADMIN,
# title = "Quantities of Textiles Shipped")
# }
#
if(dataType == "Quantity"){ #This will show the total quantity of textiles
create_leaflet_map(map.data,totalValues,dataType,c(viewLat,viewLong,viewZoom))
# country.colors <- get_binByDataType(totalValues,dataType)
#
#
# #Mapping the data
# map.data %>%
# leaflet() %>%
# addTiles() %>%
# addPolygons(fillColor = ~country.colors(return_colByDataType(map.data@data,dataType)), #We only use polygons for countries we may have actually used
# fillOpacity = .7,
# color = "black",
# opacity = 1,
# weight = 1,
# label = ~ADMIN,
# popup = ~return_popupByDataType(map.data@data,dataType),
# layerId = ~ADMIN) %>%
# setView(lat = viewLat, lng = viewLong, zoom = viewZoom) %>%
# addLegend(pal = country.colors,
# values = map.data@data$ADMIN,
# title = return_titleByDataType(dataType))
}
else if(dataType == "Value"){ #Redo everything the same except for total value
bins <- totalValues$total_Dec %>%
auto_bin()
country.colors <- colorBin(palette = "YlOrRd",
domain = totalValues$total_Quant,
bins = bins)
map.data %>%
leaflet() %>%
addTiles() %>%
addPolygons(fillColor = ~country.colors(total_Dec),
fillOpacity = .7,
color = "black",
opacity = 1,
weight = 1,
label = ~ADMIN,
layerId = ~ADMIN,
popup = ~paste("Total Value:", format(ifelse(is.na(total_Dec), 0, total_Dec), big.mark = ",", scientific = FALSE), "guilders", sep = " ")) %>%
setView(lat = viewLat, lng = viewLong, zoom = viewZoom) %>%
addLegend(pal = country.colors,
values = map.data@data$ADMIN,
title = "Value of Textiles Shipped")
}
})
#Used to render the plot for pie chart
output$pieChart <- renderPlot({
input$updateBtn
name <- input$countriesMap_shape_click$id
#only want to do this if they clicked on a country
if(length(name) != 0){
#Read in all of the inputs, but isolated
modifier <- isolate(input$pieChart)
dataSet <- isolate(input$dataSet)
regionChoice <- isolate(input$regionChoice)
textileName <- isolate(input$textileName)
colors <- isolate(input$colors)
patterns <- isolate(input$patterns)
process <- isolate(input$process)
fibers <- isolate(input$fibers)
geography <- isolate(input$geography)
qualities <- isolate(input$qualities)
inferredQualities <- isolate(input$inferredQualities)
#Again, reusing the original data
joined.data <- joined.data.original
#Filter all the inputs
if(length(textileName) != 0){
joined.data <- joined.data %>%
filter(textile_name %in% textileName)
}
if(length(colors) != 0){
joined.data <- joined.data %>%
filter(colorGroup %in% colors)
}
if(length(patterns) != 0){
joined.data <- joined.data %>%
filter(textile_pattern_arch %in% patterns)
}
if(length(process) != 0){
joined.data <- joined.data %>%
filter(textile_process_arch %in% process)
}
if(length(fibers) != 0){
joined.data <- joined.data %>%
filter(textile_fiber_arch %in% fibers)
}
if(length(geography) != 0){
joined.data <- joined.data %>%
filter(textile_geography_arch %in% geography)
}
if(length(qualities) != 0){
joined.data <- joined.data %>%
filter(textile_quality_arch %in% qualities)
}
if(length(inferredQualities) != 0){
joined.data <- joined.data %>%
filter(textile_quality_inferred %in% inferredQualities)
}
#We care specifically about the destination here
if(regionChoice == "Destination"){ #Only dest_country
pie.data <- joined.data %>%
filter(dest_country == name) %>%
select(textile_quantity,
deb_dec,
all_of(modifier),
company)
}
else { #Only orig_country
pie.data <- joined.data %>%
filter(orig_country == name) %>%
select(textile_quantity,
deb_dec,
all_of(modifier),
company)
}
#Omit na of the selected columns to avoid errors
if(input$omitNAs){
pie.data <- pie.data %>%
na.omit()
}
else{ #Fix a problem for if NA is the only data point
pie.data[3][is.na(pie.data[3])] <- "None indicated"
}
if(dataSet != "Both"){ #Controlling for company selection
pie.data <- pie.data %>%
filter(company == dataSet)
}
if(isolate(input$dataType) == "Quantity"){ #If they're interested in quantity
if(nrow(pie.data) != 0){ #check to see if there are values left to publish
pie.data %>%
ggplot(aes(x="",
y = textile_quantity)) +
geom_bar(stat="identity",
width=1,
aes_string(fill=modifier))+
coord_polar("y", start=0) + #This line in particular changes the bar chart to a pie chart
labs(x = NULL,
y = NULL,
fill = NULL) +
scale_fill_discrete(name = paste(modifier)) +
theme_void() +
ggtitle(label = paste(modifier, "distribution for", name, "with these filters."))
}
else{ #No rows were found
ggplot() +
ggtitle(label = paste(name, " has no data for these filters and ", modifier, ".", sep = ""))
}
}
else{ #This will do total value the same way, except graphing deb_dec
if(nrow(pie.data) != 0){
pie.data %>%
ggplot(aes(x="",
y = deb_dec)) +
geom_bar(stat="identity",
width=1,
aes_string(fill=modifier))+
coord_polar("y", start=0) +
labs(x = NULL,
y = NULL,
fill = NULL) +
scale_fill_discrete(name = paste(modifier)) +
theme_void() +
ggtitle(label = paste(modifier, "monetary distribution for", name, "with these filters."))
}
else{
ggplot() +
ggtitle(label = paste(name, " has no data for these filters and ", modifier, ".", sep = ""))
}
}
}
else{ #This comes up if they have not clicked any countries
ggplot() +
ggtitle(label = "Select a country with data for these textiles in order to display a pie chart here.")
}
})
#Rendering the bar chart - this works nearly the exact same way as the pie chart
#except when it is graphing the outputs, it is doing so with a bar chart instead of a pie chart
output$barChart <- renderPlot({
input$updateBtn
name <- input$countriesMap_shape_click$id
if(length(name) != 0){
modifier <- isolate(input$barChart)
modifierObj <- paste("`", modifier, "`", sep = "")
dataSet <- isolate(input$dataSet)
regionChoice <- isolate(input$regionChoice)
textileName <- isolate(input$textileName)
colors <- isolate(input$colors)
patterns <- isolate(input$patterns)
process <- isolate(input$process)
fibers <- isolate(input$fibers)
geography <- isolate(input$geography)
qualities <- isolate(input$qualities)
inferredQualities <- isolate(input$inferredQualities)
orig_yr <- isolate(input$orig_yr)
joined.data <- joined.data.original
if(length(textileName) != 0){
joined.data <- joined.data %>%
filter(textile_name %in% textileName)
}
if(length(colors) != 0){
joined.data <- joined.data %>%
filter(colorGroup %in% colors)
}
if(length(patterns) != 0){
joined.data <- joined.data %>%
filter(textile_pattern_arch %in% patterns)
}
if(length(process) != 0){
joined.data <- joined.data %>%
filter(textile_process_arch %in% process)
}
if(length(fibers) != 0){
joined.data <- joined.data %>%
filter(textile_fiber_arch %in% fibers)
}
if(length(geography) != 0){
joined.data <- joined.data %>%
filter(textile_geography_arch %in% geography)
}
if(length(qualities) != 0){
joined.data <- joined.data %>%
filter(textile_quality_arch %in% qualities)
}
if(length(inferredQualities) != 0){
joined.data <- joined.data %>%
filter(textile_quality_inferred %in% inferredQualities)
}
if(regionChoice == "Destination"){
bar.data <- joined.data %>%
filter(dest_country == name) %>%
select(textile_quantity,
deb_dec,
orig_yr,
all_of(modifier),
company)
}
else{
bar.data <- joined.data %>%
filter(orig_country == name) %>%
select(textile_quantity,
deb_dec,
orig_yr,
all_of(modifier),
company)
}
if(input$omitNAs){
bar.data <- bar.data %>%
na.omit()
}
else{
bar.data[4][is.na(bar.data[4])] <- "None indicated"
}
if(dataSet != "Both"){
bar.data <- bar.data %>%
filter(company == dataSet)
}
if(isolate(input$dataType) == "Quantity"){
if(nrow(bar.data) != 0){
if(isolate(input$facet)){
bar.data %>%
ggplot(aes(x = orig_yr, y = textile_quantity)) +
geom_bar(stat="identity",
aes_string(fill=modifier)) +
labs(x = "Original Year",
y = "Textile Quantity",
fill = NULL) +
scale_fill_discrete(name = paste(modifier)) +
theme_bw() +
ggtitle(label = paste(modifier, "distribution for", name, "with these filters.")) +
facet_wrap(~get(modifier))
}
else{
bar.data %>%
ggplot(aes(x = factor(orig_yr), y = textile_quantity)) +
geom_bar(stat="identity",
aes_string(fill=modifier)) +
labs(x = "Original Year",
y = "Textile Quantity",
fill = NULL) +
scale_fill_discrete(name = paste(modifier)) +
theme_bw() +
ggtitle(label = paste(modifier, "distribution for", name, "with these filters."))
}}
else{
ggplot() +
ggtitle(label = paste(name, " has no data for these filters and ", modifier, ".", sep = ""))
}}
else{
if(nrow(bar.data) != 0){
if(isolate(input$facet)){
bar.data %>%
ggplot(aes(x = factor(orig_yr), y = textile_quantity)) +
geom_bar(stat="identity",
aes_string(fill=modifier)) +
labs(x = "Original Year",
y = "Textile Quantity",
fill = NULL) +
scale_fill_discrete(name = paste(modifier)) +
theme_bw() +
ggtitle(label = paste(modifier, "distribution for", name, "with these filters.")) +
facet_wrap(~get(modifier))
}
else{
bar.data %>%
ggplot(aes(x = orig_yr, y = deb_dec)) +
geom_bar(stat="identity",
aes_string(fill=modifier)) +
labs(x = "Original Year",
y = "Textile Value (guilders)",
fill = NULL) +
scale_fill_discrete(name = paste(modifier)) +
theme_bw() +
ggtitle(label = paste(modifier, "monetary distribution for", name, "with these filters."))
}}
else{
ggplot() +
ggtitle(label = paste(name, " has no data for these filters and ", modifier, ".", sep = ""))
}}
}
else{
ggplot() +
ggtitle(label = "Select a country with data for these textiles in order to display a bar chart here.")
}
})
}
shinyApp(ui, server)