• R/O
  • SSH

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Commit MetaInfo

修订版06b43cc2904a53c02727da88df02556b197a7842 (tree)
时间2022-09-20 04:51:43
作者Lorenzo Isella <lorenzo.isella@gmai...>
CommiterLorenzo Isella

Log Message

I improved the shiny app and the associated Rmd file to generate a downloadable report.

更改概述

差异

diff -r 68d2ff349466 -r 06b43cc2904a R-codes/shiny_scoreboard.R
--- a/R-codes/shiny_scoreboard.R Mon Sep 19 11:11:50 2022 +0200
+++ b/R-codes/shiny_scoreboard.R Mon Sep 19 21:51:43 2022 +0200
@@ -206,7 +206,9 @@
206206
207207 ui <- fluidPage(
208208 theme = bs_theme(version = 4, bootswatch = "minty"),
209-titlePanel(
209+ ## theme = bslib::bs_theme(bootswatch = "darkly"),
210+
211+ titlePanel(
210212 h1("Scoreboard 2021 Explorer", align = "center")
211213 ) ,
212214 title = "Scoreboard Explorer",
@@ -270,8 +272,9 @@
270272
271273 downloadButton("downloadData1",HTML("Download raw data" )),
272274
273-downloadButton("downloadData2",HTML("Download aggregated data" ))
274-
275+downloadButton("downloadData2",HTML("Download aggregated data" )),
276+
277+downloadButton("report", "Generate report")
275278
276279 ),
277280
@@ -396,7 +399,7 @@
396399
397400
398401
399- yearly_agg_long <- reactive({
402+ yearly_agg_long <- reactive({
400403
401404
402405 data_sel() |>
@@ -408,7 +411,22 @@
408411 })
409412
410413
411-
414+ total_aid <- reactive({
415+ yearly_agg_long() |>
416+ pull(exp_mio_eur) |>
417+ sum()
418+
419+ })
420+
421+ year_max_expenditure <- reactive({
422+ yearly_agg_long() |>
423+ filter(exp_mio_eur==max(exp_mio_eur)) |>
424+ mutate(percentage=exp_mio_eur/total_aid()*100) |>
425+ mutate(percentage=round(percentage,1)) |>
426+ mutate(percentage=paste(percentage, "%", sep=""))
427+
428+})
429+
412430 yearly_agg <- reactive({
413431
414432 yearly_agg_long() |>
@@ -877,6 +895,38 @@
877895
878896
879897
898+ output$report <- downloadHandler(
899+ # For PDF output, change this to "report.pdf"
900+ filename = "report.docx",
901+ content = function(file) {
902+ # Copy the report file to a temporary directory before processing it, in
903+ # case we don't have write permissions to the current working dir (which
904+ # can happen when deployed).
905+ tempReport <- file.path(tempdir(), "report_generator_scoreboard.Rmd")
906+ file.copy("report_generator_scoreboard.Rmd", tempReport, overwrite = TRUE)
907+
908+ # Set up parameters to pass to Rmd document
909+ ## params <- list(ms = input$countries,
910+ ## year_ini = input$year_list[1],
911+ ## year_fin = input$year_list[2],
912+ ## total_expenditure=total_aid(),
913+ ## year_max=(year_max_expenditure() |>
914+ ## pull(expenditure_year)),
915+ ## expenditure_max=(year_max_expenditure() |>
916+ ## pull(exp_mio_eur)),
917+ ## expenditure_percentage=(year_max_expenditure()
918+ ## |> pull(percentage))
919+ ## )
920+
921+ # Knit the document, passing in the `params` list, and eval it in a
922+ # child of the global environment (this isolates the code in the document
923+ # from the code in this app).
924+ rmarkdown::render(tempReport, output_file = file## ,
925+ ## params = params,
926+ ## envir = new.env(parent = globalenv())
927+ )
928+ }
929+ )
880930
881931
882932 }
diff -r 68d2ff349466 -r 06b43cc2904a markdown/report_generator_scoreboard.Rmd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/markdown/report_generator_scoreboard.Rmd Mon Sep 19 21:51:43 2022 +0200
@@ -0,0 +1,98 @@
1+---
2+title: "Report"
3+output: word_document
4+---
5+
6+
7+```{r, scoreboard, echo=FALSE, eval=TRUE}
8+
9+options( scipen = 16 )
10+options(tidyverse.quiet = TRUE)
11+suppressWarnings(suppressMessages(library(janitor)))
12+suppressWarnings(suppressMessages(library(viridis)))
13+suppressWarnings(suppressMessages(library(scales)))
14+suppressWarnings(suppressMessages(library(treemap)))
15+suppressWarnings(suppressMessages(library(ggsci)))
16+suppressWarnings(suppressMessages(library(flextable)))
17+
18+
19+
20+library(tidyverse, warn.conflicts = FALSE)
21+library(janitor)
22+library(viridis)
23+library(scales)
24+library(flextable)
25+
26+
27+my_ggplot_theme2 <- function(legend_coord){
28+ theme_bw()+
29+
30+ theme(## legend.title = element_text(vjust=1,lineheight=1, size=14 ),
31+
32+ legend.title = element_text(vjust = 1,lineheight=1,
33+ size=14, margin = margin(t = 4.5)),
34+ legend.spacing.y = grid::unit(3, "pt"),
35+ legend.text.align = 0.5,
36+
37+ panel.grid.minor = element_blank(),
38+ plot.title = element_text(lineheight=.8, size=24, face="bold",
39+ vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ),
40+ axis.title.x = element_text(size = 20, vjust=1),
41+ axis.title.y = element_text(size = 20, angle=90, vjust=1),
42+ axis.text.x = element_text(size=15, colour="black", vjust=1),
43+ axis.text.y = element_text(size=15, colour="black", hjust=1),
44+ legend.position = legend_coord,
45+ strip.background = element_rect(colour = 'blue',
46+ fill = 'white', size = 1, linetype=1),
47+ strip.text.x = element_text(colour = 'red', angle = 0,
48+ size = 12, hjust = 0.5,
49+ vjust = 0.5, face = 'bold'),
50+ strip.text.y = element_text(colour = 'red', angle = 0,
51+ size = 12, hjust = 0.5,
52+ vjust = 0.5, face = 'bold'),
53+
54+ )
55+}
56+
57+
58+
59+
60+```
61+
62+
63+
64+
65+
66+<!-- ```{r} -->
67+<!-- # The `params` object is available in the document. -->
68+<!-- params$ms -->
69+<!-- ``` -->
70+
71+
72+Following the filters applied to the data,
73+for the years from `r input$year_list[1]` to
74+`r input$year_list[2]` `r input$countries` spent in total
75+`r total_aid()` million EUR. The aid peaked in
76+`r year_max_expenditure()$expenditure_year` with an expenditure of
77+`r year_max_expenditure()$exp_mio_eur` million EUR amounting to
78+`r year_max_expenditure()$percentage` of the total aid in the period
79+under scrutiny. The evolution of the aid along
80+`r input$year_list[1]`-`r input$year_list[2]` is illustrated below.
81+
82+
83+```{r, echo=FALSE,fig.height = 6, fig.width = 12}
84+
85+
86+ggplot(data = yearly_agg_long(), aes(x = expenditure_year,
87+ y=exp_mio_eur)) +
88+ geom_bar(position=position_dodge2(preserve="single"), stat="identity", alpha=1, color="black")+
89+ my_ggplot_theme2("top")+
90+ labs(title=paste("State Aid Spending in Million EUR ",input$year_list[1],
91+ "-",
92+ input$year_list[1], sep=""))+
93+ scale_x_continuous(breaks=seq(input$year_list[1], input$year_list[2]))+
94+ xlab("Year")+
95+ ylab("State Aid Expenditure")+
96+ guides(fill=guide_legend(nrow=2,byrow=TRUE))
97+```
98+