Revision | 0230ea9f3e21c2e5e70adee400a0078675027710 (tree) |
---|---|
Time | 2020-01-16 22:45:07 |
Author | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
A script to make a dashboard of the balance of payments data.
@@ -0,0 +1,223 @@ | ||
1 | +rm(list=ls()) | |
2 | + | |
3 | +library(shiny) | |
4 | +library(Cairo) # For nicer ggplot2 output when deployed on Linux | |
5 | +library(tidyverse) | |
6 | +library(scales) | |
7 | +library(DT) | |
8 | +library(patchwork) | |
9 | +library(viridis) | |
10 | +library(scales) | |
11 | + | |
12 | +my_pal <- viridis(4)[1:3] | |
13 | + | |
14 | +my_ggplot_theme2 <- function(legend_coord){ | |
15 | + theme_bw()+ | |
16 | + | |
17 | + theme(legend.title = element_text(vjust=1,lineheight=1, size=14 ), | |
18 | + panel.grid.minor = element_blank(), | |
19 | + plot.title = element_text(lineheight=.8, size=24, face="bold", | |
20 | + vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ), | |
21 | + axis.title.x = element_text(size = 20, vjust=1), | |
22 | + axis.title.y = element_text(size = 20, angle=90, vjust=1), | |
23 | + axis.text.x = element_text(size=15, colour="black", vjust=1), | |
24 | + axis.text.y = element_text(size=15, colour="black", hjust=1), | |
25 | + legend.position = legend_coord, | |
26 | + strip.background = element_rect(colour = 'blue', | |
27 | + fill = 'white', size = 1, linetype=1), | |
28 | + strip.text.x = element_text(colour = 'red', angle = 0, | |
29 | + size = 12, hjust = 0.5, | |
30 | + vjust = 0.5, face = 'bold'), | |
31 | + strip.text.y = element_text(colour = 'red', angle = 0, | |
32 | + size = 12, hjust = 0.5, | |
33 | + vjust = 0.5, face = 'bold'), | |
34 | + | |
35 | + ) | |
36 | +} | |
37 | + | |
38 | + | |
39 | + | |
40 | +df_ini <- readRDS("quarterly_BOP.RDS") %>% | |
41 | + mutate(Value=as.numeric(Value)) %>% | |
42 | + filter(complete.cases(.)) %>% | |
43 | + arrange(desc(quarter)) | |
44 | + | |
45 | + | |
46 | +reporters <- df_ini$Reporter %>% unique %>% sort | |
47 | +partners <- df_ini$Partner %>% unique %>% sort | |
48 | + | |
49 | + | |
50 | + | |
51 | +ui <- fluidPage( | |
52 | + | |
53 | +titlePanel("Balance of Payments (BOP): Quarterly Data"), | |
54 | + | |
55 | + sidebarLayout( | |
56 | + sidebarPanel( | |
57 | + selectInput("reporterlabel", | |
58 | + "Reporter:", | |
59 | + reporters, | |
60 | + selected="European Union - 28 countries" | |
61 | + ## , multiple=T | |
62 | + ), | |
63 | +selectInput("partnerlabel", | |
64 | + "Partner:", | |
65 | + partners , | |
66 | + selected="China except Hong Kong" | |
67 | + ), | |
68 | + | |
69 | +downloadButton("downloadData", "Download data selection"), | |
70 | + downloadButton("save", "Download plot"), | |
71 | + downloadButton("saveall", "Download full dataset"), | |
72 | + | |
73 | +h3("Data source: EUROSTAT"), | |
74 | +h4("Last updated: 16/01/2020") | |
75 | + | |
76 | + | |
77 | +), | |
78 | + | |
79 | +mainPanel( | |
80 | + ## h2("Evolution of Stocks and Flows"), | |
81 | + plotOutput("tradeplot" ## , | |
82 | + ## hover = "plot_hover" | |
83 | + ## click = "plot_click" | |
84 | + ) , | |
85 | + ## DTOutput("info") #### <- CHANGED THIS #### | |
86 | + tableOutput("table") | |
87 | + | |
88 | +) | |
89 | + | |
90 | +) | |
91 | +) | |
92 | + | |
93 | + | |
94 | + | |
95 | + | |
96 | +server <- function(input, output) { | |
97 | + | |
98 | + | |
99 | + dataset <- reactive({ df_ini %>% | |
100 | + select(-quarter) %>% | |
101 | + arrange(Reporter, Partner) | |
102 | + }) | |
103 | + | |
104 | + | |
105 | + filtered_data <- reactive({ | |
106 | + | |
107 | + df_ini %>% filter(Reporter %in% input$reporterlabel, | |
108 | + Partner %in% input$partnerlabel) %>% | |
109 | + arrange(desc(quarter)) | |
110 | + | |
111 | + | |
112 | + }) | |
113 | + | |
114 | + | |
115 | + filtered2 <- reactive({ | |
116 | + filtered_data() %>% | |
117 | + select(-quarter) %>% | |
118 | + rename("Value (Mio \u20ac)"="Value") | |
119 | + }) | |
120 | + | |
121 | + | |
122 | + filtered3 <- reactive({ | |
123 | + filtered_data() %>% | |
124 | + select(-quarter) %>% | |
125 | + mutate(Value=comma(Value, accuracy=1)) %>% | |
126 | + rename("Value (Mio \u20ac)"="Value") | |
127 | + | |
128 | + }) | |
129 | + | |
130 | + | |
131 | + tradeplot <- reactive({ | |
132 | + options( scipen = 16 ) | |
133 | + | |
134 | + my_rep <- filtered_data()$Reporter[1] | |
135 | + | |
136 | + my_par <- filtered_data()$Partner[1] | |
137 | + | |
138 | + | |
139 | + ### I add a trimester on the x axis to avoid ugly looking cutoff of the year. | |
140 | + my_min <- min(filtered_data()$quarter) | |
141 | + my_max <- max(filtered_data()$quarter+90) | |
142 | + | |
143 | + filtered_data() %>% | |
144 | + ggplot(aes(x =quarter , y = Value, color=Flow, shape=Flow, | |
145 | + linetype=Flow)) + | |
146 | + geom_line(size=1.)+ | |
147 | + geom_point(size=3, stroke=2)+ | |
148 | + my_ggplot_theme2("top")+ | |
149 | + scale_color_manual(NULL, ## labels=c("Inward Stocks","Outward Stocks" ), | |
150 | + values=my_pal)+ | |
151 | + scale_shape_manual(NULL, ## labels=c("Inward Stocks","Outward Stocks" ), | |
152 | + values=c(1,2,3))+ | |
153 | + scale_linetype_manual(NULL, ## labels=c("Inward Stocks","Outward Stocks" ), | |
154 | + values=c(1,2,3))+ | |
155 | + | |
156 | + | |
157 | + scale_y_continuous(labels=function(x) format(x, big.mark = ",", | |
158 | + scientific = FALSE),breaks=pretty_breaks(n=5))+ | |
159 | + | |
160 | + scale_x_date(breaks = pretty_breaks(n=6), limits=(c(my_min, my_max)))+ | |
161 | + xlab(NULL)+ | |
162 | + ylab("Value of BOP (Mio \u20ac)") + | |
163 | + labs(title = paste("Reporter: ", my_rep, "\nPartner: ", my_par)) | |
164 | + | |
165 | + }) | |
166 | + | |
167 | + | |
168 | + | |
169 | + output$tradeplot <- renderPlot({ | |
170 | +tradeplot() | |
171 | + } | |
172 | +) | |
173 | + | |
174 | + | |
175 | + # Downloadable csv of selected dataset ---- | |
176 | + output$downloadData <- downloadHandler( | |
177 | + filename = function() { | |
178 | + paste("data_extraction", ".csv", sep = "") | |
179 | + | |
180 | + }, | |
181 | + content = function(file) { | |
182 | + write.csv(filtered2(), file, row.names = FALSE) | |
183 | + } | |
184 | + ) | |
185 | + | |
186 | + | |
187 | + | |
188 | + | |
189 | +output$save <- downloadHandler( | |
190 | + filename = "save.png" , | |
191 | + content = function(file) { | |
192 | + ggsave(tradeplot(), filename = file, width=12, height=6) | |
193 | + | |
194 | + }) | |
195 | + | |
196 | + | |
197 | + | |
198 | + # Downloadable csv of full dataset ---- | |
199 | + output$saveall <- downloadHandler( | |
200 | + filename = function() { | |
201 | + ## paste(input$dataset, ".csv", sep = "") | |
202 | + paste("complete_dataset", ".csv", sep = "") | |
203 | + | |
204 | + }, | |
205 | + content = function(file) { | |
206 | + write.csv(dataset(), file, row.names = FALSE) | |
207 | + } | |
208 | + ) | |
209 | + | |
210 | + | |
211 | + | |
212 | + output$table <- renderTable(dd <- filtered3()## %>% | |
213 | + ## mutate_all(as.character) | |
214 | + ) | |
215 | + | |
216 | + | |
217 | + | |
218 | +} | |
219 | + | |
220 | + | |
221 | + | |
222 | +shinyApp(ui = ui, server = server) | |
223 | + |