-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
114 lines (92 loc) · 3.37 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
---
author: "Marc Gumowski"
categories:
- JavaScript
- R
- D3.js
layout: Bubble Chart
output: html_document
css: src/styles.css
tags:
- D3
- Bubble Chart
---
```{r setup, echo = FALSE, warning = FALSE, error = FALSE, message = FALSE}
knitr::opts_chunk$set(echo = TRUE)
# Install packages
list.of.packages <- c("readxl", "RODBC", "magrittr", "plyr", "zoo", "data.table", "extrafont", "maps", "ggplot2",
"ggrepel", "RColorBrewer", "viridis", "dplyr", "sp", "circlize", "gridSVG", "ggiraph",
"htmlwidgets", "shiny", "knitr", "xtable", "jsonlite")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[ ,"Package"])]
if(length(new.packages)) {
install.packages(new.packages)
}
# Load packages
invisible(lapply(list.of.packages, library, character.only = TRUE))
# Load data
dbdata <- read_xlsx("data.xlsx")
# Subset data
dbplotFull <- subset(dbdata, select = c(Region, Country, `ISO-3A`, Total_Imports, Products, Product_Description, Avg_Product_Tariffs, Imports_Product_Share))
dbplotFull$Imports_Product <- dbplotFull$Total_Imports * dbplotFull$Imports_Product_Share / 100
dbplotFull <- data.table(dbplotFull)[order(Products)]
dbplotFull[`ISO-3A` == 'EEC', 'ISO-3A'] <- 'EU'
dbplotFull[Country == "Côte d'Ivoire", 'Country'] <- "Ivory Coast"
# Add Total
dbplotTotal <- dbplotFull[ , list(Total_Imports = mean(Total_Imports), Products = "00", Product_Description = "Total",
Avg_Product_Tariffs = mean(Avg_Product_Tariffs), Imports_Product_Share = sum(Imports_Product_Share),
Imports_Product = sum(Imports_Product)), by = list(Country, `ISO-3A`, Region)]
# Merge
dbplot <- rbind(dbplotTotal, dbplotFull)
# List by product
dbplotEach <- list()
for (i in 1:length(unique(dbplot$Products))) {
dbplotEach[[i]] <- subset(dbplot, Products == unique(Products)[i], c(Region, Country, `ISO-3A`, Imports_Product, Avg_Product_Tariffs))
names(dbplotEach)[i] <- paste(as.character(unique(subset(dbplot,
Products == unique(Products)[i], Products))), '-',
as.character(unique(subset(dbplot,
Products == unique(Products)[i], Product_Description))))
}
```
```{r echo = FALSE, results = 'asis'}
# Head
cat('
<!DOCTYPE html>
<html>
<head>
<title> Bubble Chart </title>
</head>
')
# Body
cat('<body>')
#Title
cat("<center><h1>Imports and applied tariffs by product groups</h1></center>")
# Button
cat('
<div class=buttons>
<button class="myButton" type="button" id="combine">World</button>
<button class="myButton" type="button" id="region">By Region</button>
<button class="myButton" type="button" id="orderImports">By Imports</button>
<button class="myButton" type="button" id="orderTariffs">By Tariffs</button>
')
# Dropdown menu
cat("
<select class='myButton' id='opts'>",
paste0("<option value = '", paste(names(dbplotEach)), "'>", noquote(names(dbplotEach)), "</option>"),"</select></div>
")
# Div Plot 1
cat('
<div id="bubbleInteractive" style="text-align:center; width:100%;"></div>
')
# Script
# Data, d3, Chart
cat(
paste(
'<script>
var dataBubble = ', toJSON(dbplotEach), ';
</script>'
, sep=""),
'<script src="src/d3v4/d3.min.js"></script>
<script src="src/bubble.js"></script>
')
cat('</body></html>')
```