plan.R 9.47 KB
Newer Older
1
2
3
4
# # Interactive workflow
# source("src/packages.R")
# source("src/functions.R")
# loadd()
Facundo Muñoz's avatar
Facundo Muñoz committed
5
6

# The workflow plan data frame outlines what you are going to do.
7
plan <- drake_plan(## raw_data = read_excel(
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
    ##   file_in(here("data/..."))
    ## ),
    ## clean_data = raw_data %>% cleanup(),

    ## SOURCE DATA -------------------------------------------------------------

    ## Europe and neighbour countries
    ## EUrope 27 + UK
    eu28 = c("Austria", "Belgium", "Bulgaria", "Croatia",
             "Cyprus", "Czech Republic", "Denmark", "Estonia",
             "Finland", "France", "Germany", "Greece", "Hungary",
             "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg",
             "Malta", "Netherlands", "Poland", "Portugal",
             "Romania", "Slovakia", "Slovenia", "Spain", "Sweden",
             "United Kingdom"),

    ## European free trade association
    efta = c("Iceland", "Liechtenstein", "Norway", "Switzerland"),

    ## Candidate countries
    candi = c("Albania", "Montenegro", "North Macedonia",
              "Serbia", "Turkey"),

    ## Other countries
    noneu = c("Bosnia and Herzegovina", "Kosovo"),

    ## neighbour countries
    enp = c("Algeria", "Armenia", "Azerbaijan", "Belarus", "Egypt",
            "Georgia", "Israel", "Jordan", "Lebanon", "Libya",
            "Moldova", "Morocco",  "Syria", "Tunisia", "Ukraine"),

39
    wca = c("Cameroon", "Central African Republic", "Chad",
40
41
42
43
44
45
            "Congo DR", "Congo", "Equatorial Guinea", "Gabon",
            "Sao Tome and Principe", "Benin", "Burkina Faso",
            "Cape Verde", "The Gambia", "Ghana", "Guinea",
            "Guinea Bissau", "Côte d'Ivoire", "Liberia", "Mali",
            "Mauritania", "Niger", "Nigeria", "Senegal",
            "Sierra Leone", "Togo"),
46

47
48
49
50
51
52
53
54
55
    ## Total population counts and by Country for 2020 from the WorlPop dtabase
    ## world_population = read.table(
    ##     file_in("https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv"),
    ##     header = T,
    ##     sep = ",",
    ##     as.is = T,
    ##     check.names = F,
    ##     quote = "\""
    ## ),
56

57
    ## ECDC Covid19 geographic distribution data
58
59
     last_day = Sys.Date() - 1,
    #last_day = as.Date("2020-07-22"),
60
    ## string for today
61
    sdt = format(last_day, "%d %b %Y"),
62
63
64
    
    ## date range for lockdown
    date_range_lock = paste0("2020-01-01/",last_day),
65

66
    covid = ecdc_covid19(last_day) %>%
Facundo Muñoz's avatar
Facundo Muñoz committed
67
68
69
        # rm some negative records that are obvious errors
        # should we use NA instead?
        filter(deaths >= 0),
70
71
72
73
74
75
76
77
78
79
80
81

    ## ## String notice for data update
    ## data_update_date = format(Sys.Date() - 1,
    ##                           "%d %b %Y"),

    ## Lock-down dates
    lockdown_dates = read_excel(file_in("data/lockdown.xlsx")) %>%
        transmute(
            name = standardise_country_names(raster::trim(name)),
            lockdown = as.Date(lockdown)
        ),

82
83
84
85
86
87
    ## Coronavirus Government Response Tracker

    ## Governments are taking a wide range of measures in response to
    ## the COVID-19 outbreak.
    ## This tool aims to track and compare
    ## policy responses around the world, rigorously and consistently.
88

89
    lock = stringency(date_range_lock),
90

91
    ## CARTOGRAPHY ---------------------------------------------------
92

93
94
95
96
97
98
99
100
101
102
    ## Coordinate Reference Systems
    wgs84 = CRS("+proj=longlat +ellps=WGS84",
                doCheckCRSArgs = FALSE),

    merc = CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs",
               doCheckCRSArgs = FALSE),

    eust = "MOOD has received funding from the EU Horizon 2020 Research and Innovation programme under grant agreement No 874850.",

    ## Country borders and population data
Facundo Muñoz's avatar
Facundo Muñoz committed
103
    world_map = get_world_map(file_in("data/moodgaulcountrywithkos2013wpop.gpkg")),
104

105
106
107
    ## GIS vector layer for European countries (EU 27 + UK + EFTA +
    ## candidate countries + non-EU countries). Source GADM.org
    ## version 3.6
108
    geo_europe_light = geosimplify(
109
110
        world_map[world_map$name %in% xeurope_countries$name, ]
    ),
111

112
113
    roi_europe_light = region_of_interest(
        geo_europe_light, world_map),
114
115

    ## GIS vector layer for West and Central African countries
116
117
    geowca = geosimplify(world_map[world_map$name %in%
                       standardise_country_names(wca),]),
118

119
    roiwca = region_of_interest(geowca, world_map),
120
121
122

    ## DERIVED DATA ----------------------------------------

123
124
125
126
127
128
129
130
131

    world_population = world_map %>%
        as_tibble %>%
        mutate(
            name = standardise_country_names(ADM0_NAME)
        ) %>%
        select(name, pop = TOTPOP) %>%
        filter(pop > 0),

132
133
134
135
136
137
138
139
    ## ## Deaths by country and date
    ## deaths_by_ctry_dt =
    ##   covid %>%
    ##   group_by(date, name) %>%
    ##   summarise(deaths = sum(deaths)) %>%
    ##   ungroup() %>%
    ##   mutate(date = as.Date(date)),

140
    ## Lock-down dates and population counts by country of the european
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    ## and neighbouring countries
    xeurope_countries =
        enframe(
            list(EU28 = eu28, EFTA = efta, Candidates = candi, Non_EU = noneu),
            name = "group",
            value = "name"
        ) %>%
        unnest(cols = name) %>%
        mutate(
            name = standardise_country_names(name),
            group = fct_inorder(group)
        ) %>%
        select(name, group) %>%
        arrange(name) %>%
        left_join(
            ## Lock-down dates
            lockdown_dates,
            by = "name"
        ) %>%
        left_join(
            ## Population counts in number of people
            world_population,
            by = "name"
        ),
165

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    # ## Check: compare two sources of population data
    # ## 1. From the world population file, and from ECDC covid data
    # inner_join(
    #     world_population %>%
    #         rename(pop_wp = pop),
    #     covid %>%
    #         select(name, pop_cv = pop) %>%
    #         unique(),
    #     by = "name"
    # ) %>%
    #     ggplot(aes(pop_wp, pop_cv)) +
    #     geom_point() +
    #     geom_abline(intercept = 0, slope = 1)

    # REPORTS -----------------------------------------------------------------
181
182
183

    report_euro_html =
        rmarkdown::render(
184
185
186
187
188
189
190
191
192
193
194
195
196
197
            knitr_in("src/Covid19MortalityEurope.Rmd"),
            # knit_root_dir = "../public",  # Is this necessary or convenient?
            output_dir = "public",  # https://github.com/ropensci/drake/issues/742
            output_format =
                rmdformats::readthedown(
                    dev = "png",
                    lightbox = T,
                    gallery = T,
                    use_bookdown = T,
                    number_sections = T),
            output_file =
                file_out("public/covid19MortalityEurope.html"),
            quiet = FALSE
        ),
198
199
200

    report_euro_pdf =
        rmarkdown::render(
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
            knitr_in("src/Covid19MortalityEurope.Rmd"),
            # knit_root_dir = "../public",  # Is this necessary or convenient?
            output_dir = "public",  # https://github.com/ropensci/drake/issues/742
            output_format =
                bookdown::pdf_document2(
                    dev = "pdf",
                    includes = list(
                        in_header = "preamble.tex",
                        before_body = "before_body.tex"),
                    classoption = "a4paper",
                    number_sections = T,
                    latex_engine = "xelatex"
                ),
            output_file = file_out("public/covid19MortalityEurope.pdf"),
            quiet = FALSE
        ),
217
218
219

    report_euro_docx =
        rmarkdown::render(
220
221
222
223
224
225
226
227
228
229
230
            knitr_in("src/Covid19MortalityEurope.Rmd"),
            # knit_root_dir = "../public",  # Is this necessary or convenient?
            output_dir = "public",  # https://github.com/ropensci/drake/issues/742
            output_format =
                bookdown::word_document2(
                    dev = "png",
                    number_sections = T
                ),
            output_file = file_out("public/covid19MortalityEurope.docx"),
            quiet = FALSE
        ),
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

    ## report_wcafrica_html =
    ##     rmarkdown::render(
    ##                    knitr_in("src/Covid19MortalityWCAfrica.Rmd"),
    ##                    ## knit_root_dir = "../public",  # Is this necessary or convenient?
    ##                    output_dir = "public",  # https://github.com/ropensci/drake/issues/742
    ##                    output_format =
    ##                        rmdformats::readthedown(
    ##                                        toc_depth = 3,
    ##                                        lightbox = T,
    ##                                        gallery = T,
    ##                                        use_bookdown = T,
    ##                                        number_sections = T),
    ##                    output_file = file_out("public/covid19MortalityWCAfrica.html"),
    ##                    quiet = FALSE
    ##                ),

248
    # WEBSITE AND README ------------------------------------------------------
249
250
    readme_md =
        rmarkdown::render(
251
252
253
254
            knitr_in("README.Rmd"),
            output_file = file_out("README.md"),
            quiet = FALSE
        ),
255
256
257
    index_html =
        ## Render to html within public
        rmarkdown::render(
258
259
260
261
262
263
            knitr_in("README.Rmd"),
            output_dir = "public",  # https://github.com/ropensci/drake/issues/742
            output_file = file_out("public/index.html"),
            quiet = FALSE
        ),
)
Facundo Muñoz's avatar
Facundo Muñoz committed
264
265

future::plan(future.callr::callr)