闪亮:下载剧情
Shiny: Download Plot
我知道这是一个反复出现的问题,但我一定是沉浸在一杯茶中了。
看看下面的独立脚本(诚然有点冗长)。
除了下载生成的图的能力之外,一切都像一个魅力。
我肯定修复一定是单行的,但到目前为止运气不好。
我可以创建一个按钮来下载情节,但不知何故我无法让代码理解 gpl_fin 是它应该保存的情节。
非常感谢任何帮助!
谢谢!
library(shiny)
library(Cairo) # For nicer ggplot2 output when deployed on Linux
library(tidyverse)
library(scales)
library(DT)
library(patchwork)
library(viridis)
my_pal <- viridis(3)[1:2]
my_ggplot_theme2 <- function(legend_coord){
theme_bw()+
theme(legend.title = element_text(vjust=1,lineheight=1, size=14 ),
panel.grid.minor = element_blank(),
plot.title = element_text(lineheight=.8, size=24, face="bold",
vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ),
axis.title.x = element_text(size = 20, vjust=1),
axis.title.y = element_text(size = 20, angle=90, vjust=1),
axis.text.x = element_text(size=15, colour="black", vjust=1),
axis.text.y = element_text(size=15, colour="black", hjust=1),
legend.position = legend_coord,
strip.background = element_rect(colour = 'blue',
fill = 'white', size = 1, linetype=1),
strip.text.x = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
strip.text.y = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
)
}
df_ini <- structure(list(year = c(2013L, 2013L, 2014L, 2014L, 2015L, 2015L,
2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L,
2017L, 2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L,
2016L, 2016L, 2016L, 2016L, 2017L, 2017L), entity = c("TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "TOTAL", "TOTAL"), IN_STOCKS = c(432,
125429, 1651, 125153, 953, 2056, 19674, 125519, 880, 2153, 17157,
134931, 251, 1192, 13749, 124002, 2800, 47661, 2591, 49980, 0,
3246, 0, 53401, 0, 3134, 0, 53078, 3419, 54270), OUT_STOCKS = c(532,
34303, 677, 34692, 0, 640, 1584, 34808, 0, 603, 443, 37696, 0,
199, 797, 38092, 1903, 148787, 1756, 152491, 0, 2557, 0, 152812,
0, 2375, 0, 159034, 3046, 148449), IN_FLOWS = c(354, 13737, 1244,
39, -197, 226, 1121, 2111, -302, 83, 710, 10095, -563, -733,
-3598, -9440, -570, -7988, -241, -448, 0, -355, 0, 3722, 0, 133,
0, -3950, 324, -23), OUT_FLOWS = c(NA, -5521, 23, 241, 0, -76,
369, -375, 0, 9, -255, 4695, 0, -370, 0, 3458, 432, 13504, 19,
-2956, 0, 1023, 0, -1730, 0, -129, 0, 9227, 713, -10335), Reporter = c("Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "France", "France", "France",
"France", "France", "France", "France", "France", "France", "France",
"France", "France", "France", "France"), Partner = c("Austria",
"France", "Austria", "France", "Austria", "Austria", "France",
"France", "Austria", "Austria", "France", "France", "Austria",
"Austria", "France", "France", "Austria", "Belgium", "Austria",
"Belgium", "Austria", "Austria", "Belgium", "Belgium", "Austria",
"Austria", "Belgium", "Belgium", "Austria", "Belgium")), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))
reporters <- df_ini$Reporter %>% unique %>% sort
partners <- df_ini$Partner %>% unique %>% sort
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("reporterlabel",
"Reporter:",
reporters ## , multiple=T
),
selectInput("partnerlabel",
"Partner:",
partners),
# Button
downloadButton("downloadData", "Download the data"),
downloadButton("save", "save")
),
mainPanel(
plotOutput("tradeplot"
) ,
tableOutput("table")
)
)
)
server <- function(input, output) {
filtered_data <- reactive({
df_ini %>% filter(Reporter %in% input$reporterlabel,
Partner %in% input$partnerlabel) %>%
arrange(desc(year)) %>%
group_by(year,Reporter, Partner) %>%
summarise(IN_STOCKS=sum(IN_STOCKS),
OUT_STOCKS=sum(OUT_STOCKS),
IN_FLOWS=sum(IN_FLOWS),
OUT_FLOWS=sum(OUT_FLOWS)) %>%
ungroup() %>%
mutate(Entity="Special Entity plus Total",
NACE="All NACE Actitivities") %>%
select(year, Reporter, Partner, Entity, NACE, everything()) %>%
arrange(desc(year))
})
output$tradeplot <- renderPlot({
options( scipen = 16 )
df1 <- filtered_data() %>%
select(-c(IN_FLOWS, OUT_FLOWS)) %>%
pivot_longer(c(OUT_STOCKS, IN_STOCKS), names_to="direction",
values_to="val")
df2 <- filtered_data() %>%
select(-c(IN_STOCKS, OUT_STOCKS)) %>%
pivot_longer(c(OUT_FLOWS, IN_FLOWS), names_to="direction",
values_to="val")
my_rep <- df1$Reporter[1]
my_par <- df1$Partner[1]
gpl12 <- df1 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
geom_bar(stat="identity", position="dodge")+
my_ggplot_theme2("top")+
scale_fill_manual(NULL, labels=c("Inward Stocks","Outward Stocks" ), values=my_pal)+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Stocks\n(Mio \u20ac)")+
labs(title = paste("Reporter: ", my_rep, "\nPartner: ", my_par))
gpl34 <- df2 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
## geom_point(size=3) +
## geom_line(size=1) +
geom_bar(stat="identity", position="dodge")+
scale_fill_manual(NULL, labels=c("Inward Flows","Outward Flows" ), values=my_pal)+
my_ggplot_theme2("top")+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Flows\n(Mio \u20ac)")+
labs(title = NULL)
gpl_fin <- gpl12/gpl34
gpl_fin
}
)
output$table <- renderTable(filtered_data())
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
## paste(input$dataset, ".csv", sep = "")
paste("data_extraction", ".csv", sep = "")
},
content = function(file) {
write.csv(filtered_data(), file, row.names = FALSE)
}
)
output$save <- downloadHandler(
filename = "save.png" ,
content = function(file) {
ggsave(tradeplot(), filename = file)
})
}
shinyApp(ui = ui, server = server)
我不确定是否可以 post 这个。这正是a.suliman提到的
library(shiny)
library(Cairo) # For nicer ggplot2 output when deployed on Linux
library(tidyverse)
library(scales)
library(DT)
library(patchwork)
library(viridis)
my_pal <- viridis(3)[1:2]
my_ggplot_theme2 <- function(legend_coord){
theme_bw()+
theme(legend.title = element_text(vjust=1,lineheight=1, size=14 ),
panel.grid.minor = element_blank(),
plot.title = element_text(lineheight=.8, size=24, face="bold",
vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ),
axis.title.x = element_text(size = 20, vjust=1),
axis.title.y = element_text(size = 20, angle=90, vjust=1),
axis.text.x = element_text(size=15, colour="black", vjust=1),
axis.text.y = element_text(size=15, colour="black", hjust=1),
legend.position = legend_coord,
strip.background = element_rect(colour = 'blue',
fill = 'white', size = 1, linetype=1),
strip.text.x = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
strip.text.y = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
)
}
df_ini <- structure(list(year = c(2013L, 2013L, 2014L, 2014L, 2015L, 2015L,
2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L,
2017L, 2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L,
2016L, 2016L, 2016L, 2016L, 2017L, 2017L), entity = c("TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "TOTAL", "TOTAL"), IN_STOCKS = c(432,
125429, 1651, 125153, 953, 2056, 19674, 125519, 880, 2153, 17157,
134931, 251, 1192, 13749, 124002, 2800, 47661, 2591, 49980, 0,
3246, 0, 53401, 0, 3134, 0, 53078, 3419, 54270), OUT_STOCKS = c(532,
34303, 677, 34692, 0, 640, 1584, 34808, 0, 603, 443, 37696, 0,
199, 797, 38092, 1903, 148787, 1756, 152491, 0, 2557, 0, 152812,
0, 2375, 0, 159034, 3046, 148449), IN_FLOWS = c(354, 13737, 1244,
39, -197, 226, 1121, 2111, -302, 83, 710, 10095, -563, -733,
-3598, -9440, -570, -7988, -241, -448, 0, -355, 0, 3722, 0, 133,
0, -3950, 324, -23), OUT_FLOWS = c(NA, -5521, 23, 241, 0, -76,
369, -375, 0, 9, -255, 4695, 0, -370, 0, 3458, 432, 13504, 19,
-2956, 0, 1023, 0, -1730, 0, -129, 0, 9227, 713, -10335), Reporter = c("Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "France", "France", "France",
"France", "France", "France", "France", "France", "France", "France",
"France", "France", "France", "France"), Partner = c("Austria",
"France", "Austria", "France", "Austria", "Austria", "France",
"France", "Austria", "Austria", "France", "France", "Austria",
"Austria", "France", "France", "Austria", "Belgium", "Austria",
"Belgium", "Austria", "Austria", "Belgium", "Belgium", "Austria",
"Austria", "Belgium", "Belgium", "Austria", "Belgium")), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))
reporters <- df_ini$Reporter %>% unique %>% sort
partners <- df_ini$Partner %>% unique %>% sort
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("reporterlabel",
"Reporter:",
reporters ## , multiple=T
),
selectInput("partnerlabel",
"Partner:",
partners),
# Button
downloadButton("downloadData", "Download the data"),
downloadButton("save", "save")
),
mainPanel(
plotOutput("tradeplot"
) ,
tableOutput("table")
)
)
)
server <- function(input, output) {
filtered_data <- reactive({
df_ini %>% filter(Reporter %in% input$reporterlabel,
Partner %in% input$partnerlabel) %>%
arrange(desc(year)) %>%
group_by(year,Reporter, Partner) %>%
summarise(IN_STOCKS=sum(IN_STOCKS),
OUT_STOCKS=sum(OUT_STOCKS),
IN_FLOWS=sum(IN_FLOWS),
OUT_FLOWS=sum(OUT_FLOWS)) %>%
ungroup() %>%
mutate(Entity="Special Entity plus Total",
NACE="All NACE Actitivities") %>%
select(year, Reporter, Partner, Entity, NACE, everything()) %>%
arrange(desc(year))
})
tradeplot <- reactive({
options( scipen = 16 )
df1 <- filtered_data() %>%
select(-c(IN_FLOWS, OUT_FLOWS)) %>%
pivot_longer(c(OUT_STOCKS, IN_STOCKS), names_to="direction",
values_to="val")
df2 <- filtered_data() %>%
select(-c(IN_STOCKS, OUT_STOCKS)) %>%
pivot_longer(c(OUT_FLOWS, IN_FLOWS), names_to="direction",
values_to="val")
my_rep <- df1$Reporter[1]
my_par <- df1$Partner[1]
gpl12 <- df1 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
geom_bar(stat="identity", position="dodge")+
my_ggplot_theme2("top")+
scale_fill_manual(NULL, labels=c("Inward Stocks","Outward Stocks" ), values=my_pal)+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Stocks\n(Mio \u20ac)")+
labs(title = paste("Reporter: ", my_rep, "\nPartner: ", my_par))
gpl34 <- df2 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
## geom_point(size=3) +
## geom_line(size=1) +
geom_bar(stat="identity", position="dodge")+
scale_fill_manual(NULL, labels=c("Inward Flows","Outward Flows" ), values=my_pal)+
my_ggplot_theme2("top")+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Flows\n(Mio \u20ac)")+
labs(title = NULL)
gpl_fin <- gpl12/gpl34
gpl_fin
})
output$tradeplot <- renderPlot({
tradeplot()
}
)
output$table <- renderTable(filtered_data())
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
## paste(input$dataset, ".csv", sep = "")
paste("data_extraction", ".csv", sep = "")
},
content = function(file) {
write.csv(filtered_data(), file, row.names = FALSE)
}
)
output$save <- downloadHandler(
filename = "save.png" ,
content = function(file) {
ggsave(tradeplot(), filename = file)
})
}
shinyApp(ui = ui, server = server)
我知道这是一个反复出现的问题,但我一定是沉浸在一杯茶中了。 看看下面的独立脚本(诚然有点冗长)。 除了下载生成的图的能力之外,一切都像一个魅力。 我肯定修复一定是单行的,但到目前为止运气不好。 我可以创建一个按钮来下载情节,但不知何故我无法让代码理解 gpl_fin 是它应该保存的情节。 非常感谢任何帮助! 谢谢!
library(shiny)
library(Cairo) # For nicer ggplot2 output when deployed on Linux
library(tidyverse)
library(scales)
library(DT)
library(patchwork)
library(viridis)
my_pal <- viridis(3)[1:2]
my_ggplot_theme2 <- function(legend_coord){
theme_bw()+
theme(legend.title = element_text(vjust=1,lineheight=1, size=14 ),
panel.grid.minor = element_blank(),
plot.title = element_text(lineheight=.8, size=24, face="bold",
vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ),
axis.title.x = element_text(size = 20, vjust=1),
axis.title.y = element_text(size = 20, angle=90, vjust=1),
axis.text.x = element_text(size=15, colour="black", vjust=1),
axis.text.y = element_text(size=15, colour="black", hjust=1),
legend.position = legend_coord,
strip.background = element_rect(colour = 'blue',
fill = 'white', size = 1, linetype=1),
strip.text.x = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
strip.text.y = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
)
}
df_ini <- structure(list(year = c(2013L, 2013L, 2014L, 2014L, 2015L, 2015L,
2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L,
2017L, 2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L,
2016L, 2016L, 2016L, 2016L, 2017L, 2017L), entity = c("TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "TOTAL", "TOTAL"), IN_STOCKS = c(432,
125429, 1651, 125153, 953, 2056, 19674, 125519, 880, 2153, 17157,
134931, 251, 1192, 13749, 124002, 2800, 47661, 2591, 49980, 0,
3246, 0, 53401, 0, 3134, 0, 53078, 3419, 54270), OUT_STOCKS = c(532,
34303, 677, 34692, 0, 640, 1584, 34808, 0, 603, 443, 37696, 0,
199, 797, 38092, 1903, 148787, 1756, 152491, 0, 2557, 0, 152812,
0, 2375, 0, 159034, 3046, 148449), IN_FLOWS = c(354, 13737, 1244,
39, -197, 226, 1121, 2111, -302, 83, 710, 10095, -563, -733,
-3598, -9440, -570, -7988, -241, -448, 0, -355, 0, 3722, 0, 133,
0, -3950, 324, -23), OUT_FLOWS = c(NA, -5521, 23, 241, 0, -76,
369, -375, 0, 9, -255, 4695, 0, -370, 0, 3458, 432, 13504, 19,
-2956, 0, 1023, 0, -1730, 0, -129, 0, 9227, 713, -10335), Reporter = c("Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "France", "France", "France",
"France", "France", "France", "France", "France", "France", "France",
"France", "France", "France", "France"), Partner = c("Austria",
"France", "Austria", "France", "Austria", "Austria", "France",
"France", "Austria", "Austria", "France", "France", "Austria",
"Austria", "France", "France", "Austria", "Belgium", "Austria",
"Belgium", "Austria", "Austria", "Belgium", "Belgium", "Austria",
"Austria", "Belgium", "Belgium", "Austria", "Belgium")), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))
reporters <- df_ini$Reporter %>% unique %>% sort
partners <- df_ini$Partner %>% unique %>% sort
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("reporterlabel",
"Reporter:",
reporters ## , multiple=T
),
selectInput("partnerlabel",
"Partner:",
partners),
# Button
downloadButton("downloadData", "Download the data"),
downloadButton("save", "save")
),
mainPanel(
plotOutput("tradeplot"
) ,
tableOutput("table")
)
)
)
server <- function(input, output) {
filtered_data <- reactive({
df_ini %>% filter(Reporter %in% input$reporterlabel,
Partner %in% input$partnerlabel) %>%
arrange(desc(year)) %>%
group_by(year,Reporter, Partner) %>%
summarise(IN_STOCKS=sum(IN_STOCKS),
OUT_STOCKS=sum(OUT_STOCKS),
IN_FLOWS=sum(IN_FLOWS),
OUT_FLOWS=sum(OUT_FLOWS)) %>%
ungroup() %>%
mutate(Entity="Special Entity plus Total",
NACE="All NACE Actitivities") %>%
select(year, Reporter, Partner, Entity, NACE, everything()) %>%
arrange(desc(year))
})
output$tradeplot <- renderPlot({
options( scipen = 16 )
df1 <- filtered_data() %>%
select(-c(IN_FLOWS, OUT_FLOWS)) %>%
pivot_longer(c(OUT_STOCKS, IN_STOCKS), names_to="direction",
values_to="val")
df2 <- filtered_data() %>%
select(-c(IN_STOCKS, OUT_STOCKS)) %>%
pivot_longer(c(OUT_FLOWS, IN_FLOWS), names_to="direction",
values_to="val")
my_rep <- df1$Reporter[1]
my_par <- df1$Partner[1]
gpl12 <- df1 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
geom_bar(stat="identity", position="dodge")+
my_ggplot_theme2("top")+
scale_fill_manual(NULL, labels=c("Inward Stocks","Outward Stocks" ), values=my_pal)+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Stocks\n(Mio \u20ac)")+
labs(title = paste("Reporter: ", my_rep, "\nPartner: ", my_par))
gpl34 <- df2 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
## geom_point(size=3) +
## geom_line(size=1) +
geom_bar(stat="identity", position="dodge")+
scale_fill_manual(NULL, labels=c("Inward Flows","Outward Flows" ), values=my_pal)+
my_ggplot_theme2("top")+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Flows\n(Mio \u20ac)")+
labs(title = NULL)
gpl_fin <- gpl12/gpl34
gpl_fin
}
)
output$table <- renderTable(filtered_data())
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
## paste(input$dataset, ".csv", sep = "")
paste("data_extraction", ".csv", sep = "")
},
content = function(file) {
write.csv(filtered_data(), file, row.names = FALSE)
}
)
output$save <- downloadHandler(
filename = "save.png" ,
content = function(file) {
ggsave(tradeplot(), filename = file)
})
}
shinyApp(ui = ui, server = server)
我不确定是否可以 post 这个。这正是a.suliman提到的
library(shiny)
library(Cairo) # For nicer ggplot2 output when deployed on Linux
library(tidyverse)
library(scales)
library(DT)
library(patchwork)
library(viridis)
my_pal <- viridis(3)[1:2]
my_ggplot_theme2 <- function(legend_coord){
theme_bw()+
theme(legend.title = element_text(vjust=1,lineheight=1, size=14 ),
panel.grid.minor = element_blank(),
plot.title = element_text(lineheight=.8, size=24, face="bold",
vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ),
axis.title.x = element_text(size = 20, vjust=1),
axis.title.y = element_text(size = 20, angle=90, vjust=1),
axis.text.x = element_text(size=15, colour="black", vjust=1),
axis.text.y = element_text(size=15, colour="black", hjust=1),
legend.position = legend_coord,
strip.background = element_rect(colour = 'blue',
fill = 'white', size = 1, linetype=1),
strip.text.x = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
strip.text.y = element_text(colour = 'red', angle = 0,
size = 12, hjust = 0.5,
vjust = 0.5, face = 'bold'),
)
}
df_ini <- structure(list(year = c(2013L, 2013L, 2014L, 2014L, 2015L, 2015L,
2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L,
2017L, 2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L,
2016L, 2016L, 2016L, 2016L, 2017L, 2017L), entity = c("TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "TOTAL",
"TOTAL", "TOTAL", "TOTAL", "SPE", "TOTAL", "SPE", "TOTAL", "SPE",
"TOTAL", "SPE", "TOTAL", "TOTAL", "TOTAL"), IN_STOCKS = c(432,
125429, 1651, 125153, 953, 2056, 19674, 125519, 880, 2153, 17157,
134931, 251, 1192, 13749, 124002, 2800, 47661, 2591, 49980, 0,
3246, 0, 53401, 0, 3134, 0, 53078, 3419, 54270), OUT_STOCKS = c(532,
34303, 677, 34692, 0, 640, 1584, 34808, 0, 603, 443, 37696, 0,
199, 797, 38092, 1903, 148787, 1756, 152491, 0, 2557, 0, 152812,
0, 2375, 0, 159034, 3046, 148449), IN_FLOWS = c(354, 13737, 1244,
39, -197, 226, 1121, 2111, -302, 83, 710, 10095, -563, -733,
-3598, -9440, -570, -7988, -241, -448, 0, -355, 0, 3722, 0, 133,
0, -3950, 324, -23), OUT_FLOWS = c(NA, -5521, 23, 241, 0, -76,
369, -375, 0, 9, -255, 4695, 0, -370, 0, 3458, 432, 13504, 19,
-2956, 0, 1023, 0, -1730, 0, -129, 0, 9227, 713, -10335), Reporter = c("Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "Belgium", "Belgium", "Belgium",
"Belgium", "Belgium", "Belgium", "France", "France", "France",
"France", "France", "France", "France", "France", "France", "France",
"France", "France", "France", "France"), Partner = c("Austria",
"France", "Austria", "France", "Austria", "Austria", "France",
"France", "Austria", "Austria", "France", "France", "Austria",
"Austria", "France", "France", "Austria", "Belgium", "Austria",
"Belgium", "Austria", "Austria", "Belgium", "Belgium", "Austria",
"Austria", "Belgium", "Belgium", "Austria", "Belgium")), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))
reporters <- df_ini$Reporter %>% unique %>% sort
partners <- df_ini$Partner %>% unique %>% sort
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("reporterlabel",
"Reporter:",
reporters ## , multiple=T
),
selectInput("partnerlabel",
"Partner:",
partners),
# Button
downloadButton("downloadData", "Download the data"),
downloadButton("save", "save")
),
mainPanel(
plotOutput("tradeplot"
) ,
tableOutput("table")
)
)
)
server <- function(input, output) {
filtered_data <- reactive({
df_ini %>% filter(Reporter %in% input$reporterlabel,
Partner %in% input$partnerlabel) %>%
arrange(desc(year)) %>%
group_by(year,Reporter, Partner) %>%
summarise(IN_STOCKS=sum(IN_STOCKS),
OUT_STOCKS=sum(OUT_STOCKS),
IN_FLOWS=sum(IN_FLOWS),
OUT_FLOWS=sum(OUT_FLOWS)) %>%
ungroup() %>%
mutate(Entity="Special Entity plus Total",
NACE="All NACE Actitivities") %>%
select(year, Reporter, Partner, Entity, NACE, everything()) %>%
arrange(desc(year))
})
tradeplot <- reactive({
options( scipen = 16 )
df1 <- filtered_data() %>%
select(-c(IN_FLOWS, OUT_FLOWS)) %>%
pivot_longer(c(OUT_STOCKS, IN_STOCKS), names_to="direction",
values_to="val")
df2 <- filtered_data() %>%
select(-c(IN_STOCKS, OUT_STOCKS)) %>%
pivot_longer(c(OUT_FLOWS, IN_FLOWS), names_to="direction",
values_to="val")
my_rep <- df1$Reporter[1]
my_par <- df1$Partner[1]
gpl12 <- df1 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
geom_bar(stat="identity", position="dodge")+
my_ggplot_theme2("top")+
scale_fill_manual(NULL, labels=c("Inward Stocks","Outward Stocks" ), values=my_pal)+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Stocks\n(Mio \u20ac)")+
labs(title = paste("Reporter: ", my_rep, "\nPartner: ", my_par))
gpl34 <- df2 %>%
ggplot(aes(x = year, y = val, fill=direction)) +
## geom_point(size=3) +
## geom_line(size=1) +
geom_bar(stat="identity", position="dodge")+
scale_fill_manual(NULL, labels=c("Inward Flows","Outward Flows" ), values=my_pal)+
my_ggplot_theme2("top")+
scale_y_continuous(breaks=pretty_breaks(n=4))+
scale_x_continuous(breaks = function(x) unique(floor(pretty(x))))+
xlab("Year")+
ylab("Flows\n(Mio \u20ac)")+
labs(title = NULL)
gpl_fin <- gpl12/gpl34
gpl_fin
})
output$tradeplot <- renderPlot({
tradeplot()
}
)
output$table <- renderTable(filtered_data())
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
## paste(input$dataset, ".csv", sep = "")
paste("data_extraction", ".csv", sep = "")
},
content = function(file) {
write.csv(filtered_data(), file, row.names = FALSE)
}
)
output$save <- downloadHandler(
filename = "save.png" ,
content = function(file) {
ggsave(tradeplot(), filename = file)
})
}
shinyApp(ui = ui, server = server)