This vignette builds three plans
on nor_covid19_cases_by_time_location, a dataset of
Covid-19 cases in Norway. vignette("plnr") defines the
terms: argset, action function, analysis and plan.
A single-function plan applies one action function to many argsets.
Add the argsets first. Then give every argset the same action function
with apply_action_fn_to_all_argsets().
This plan draws one graph for each location.
##
## Attaching package: 'data.table'
## The following object is masked from 'package:base':
##
## %notin%
p <- plnr::Plan$new()
data_fn <- function() {
return(plnr::nor_covid19_cases_by_time_location)
}
p$add_data(name = "covid19_cases", fn_name = "data_fn")
location_codes <- unique(p$get_data()$covid19_cases$location_code)
location_codes## [1] "county_nor03" "county_nor11" "county_nor15" "county_nor18" "county_nor30"
## [6] "county_nor34" "county_nor38" "county_nor42" "county_nor46" "county_nor50"
## [11] "county_nor54" "nation_nor"
p$add_argset_from_list(
plnr::expand_list(
location_code = location_codes,
granularity_time = "isoweek"
)
)
p$get_argsets_as_dt()## name_analysis index_analysis location_code
## <char> <int> <list>
## 1: 8381e61c-505a-4010-b2d1-4b216c08dbaa 1 county_nor03
## 2: 7e6d140d-5c8e-45a6-942a-4bc89580d339 2 county_nor11
## 3: 892d0597-e450-46c4-98f8-9d390179b1bb 3 county_nor15
## 4: 03869a65-7c6d-47f2-810a-9840da9627d5 4 county_nor18
## 5: 21e7ad90-3e5b-4b19-a032-297809c6f4dc 5 county_nor30
## 6: c30e426e-416d-464b-92b8-238fdf687e8e 6 county_nor34
## 7: a1166bff-ee37-48d1-8943-ee66d54cf124 7 county_nor38
## 8: d657d8c1-4a42-48f2-a74e-8fd72b77565d 8 county_nor42
## 9: ef98c8d5-586d-4739-94e5-ea57b692b608 9 county_nor46
## 10: 0caeb042-50f7-43a1-a76a-27ed45e0a603 10 county_nor50
## 11: 9cac7a57-455c-4bd8-8406-d1d4df9390b1 11 county_nor54
## 12: db760ccc-505d-49ee-a8ca-bd0f2d15563e 12 nation_nor
## granularity_time
## <list>
## 1: isoweek
## 2: isoweek
## 3: isoweek
## 4: isoweek
## 5: isoweek
## 6: isoweek
## 7: isoweek
## 8: isoweek
## 9: isoweek
## 10: isoweek
## 11: isoweek
## 12: isoweek
action_fn <- function(data, argset) {
if (plnr::is_run_directly()) {
data <- p$get_data()
argset <- p$get_argset(1)
}
pd <- data$covid19_cases[
location_code == argset$location_code &
granularity_time == argset$granularity_time
]
q <- ggplot(pd, aes(x = date, y = covid19_cases_testdate_n))
q <- q + geom_line()
q <- q + labs(title = argset$location_code)
q
}
p$apply_action_fn_to_all_argsets(fn_name = "action_fn")
q <- p$run_all()
q[[1]]This plan crosses two choices: cases or cases per 100 000 population, and weekly or daily data. It draws one graph for each of the four combinations.
p <- plnr::Plan$new()
data_fn <- function() {
return(plnr::nor_covid19_cases_by_time_location[location_code == "nation_nor"])
}
p$add_data(name = "covid19_cases", fn_name = "data_fn")
p$add_argset_from_list(
plnr::expand_list(
variable = c("covid19_cases_testdate_n", "covid19_cases_testdate_pr100000"),
granularity_time = c("isoweek", "day")
)
)
p$get_argsets_as_dt()## name_analysis index_analysis
## <char> <int>
## 1: 588095f6-8b39-41e2-b558-77c003bbff3a 1
## 2: c0fe0f9f-7589-4b7d-bef7-1f7a5755a0e7 2
## 3: 83807d25-dea9-466c-b1b1-c2fbe7a906b9 3
## 4: 28115455-460a-4e4c-bd04-909da1a01ee7 4
## variable granularity_time
## <list> <list>
## 1: covid19_cases_testdate_n isoweek
## 2: covid19_cases_testdate_n day
## 3: covid19_cases_testdate_pr100000 isoweek
## 4: covid19_cases_testdate_pr100000 day
action_fn <- function(data, argset) {
if (plnr::is_run_directly()) {
data <- p$get_data()
argset <- p$get_argset(1)
}
pd <- data$covid19_cases[granularity_time == argset$granularity_time]
q <- ggplot(pd, aes(x = date, y = .data[[argset$variable]]))
q <- q + geom_line()
q <- q + labs(title = paste(argset$variable, argset$granularity_time))
q
}
p$apply_action_fn_to_all_argsets(fn_name = "action_fn")
p$run_one(1)A multi-function plan gives each analysis its own action function. This plan makes the figures of a short report. Figure 1 has its own action function, and figures 2 and 3 share one.
p <- plnr::Plan$new()
data_fn <- function() {
return(plnr::nor_covid19_cases_by_time_location)
}
p$add_data(name = "covid19_cases", fn_name = "data_fn")
figure_1 <- function(data, argset) {
if (plnr::is_run_directly()) {
data <- p$get_data()
argset <- p$get_argset("figure_1")
}
pd <- data$covid19_cases[granularity_time == "isoweek"]
q <- ggplot(pd, aes(x = date, y = covid19_cases_testdate_pr100000))
q <- q + geom_line()
q <- q + facet_wrap(~location_code)
q <- q + labs(title = "Weekly Covid-19 cases per 100 000 population")
q
}
plot_epicurve_by_location <- function(data, argset) {
if (plnr::is_run_directly()) {
data <- p$get_data()
argset <- p$get_argset("figure_2")
}
pd <- data$covid19_cases[
granularity_time == "isoweek" &
location_code == argset$location_code
]
q <- ggplot(pd, aes(x = date, y = covid19_cases_testdate_n))
q <- q + geom_line()
q <- q + labs(title = argset$location_code)
q
}
p$add_analysis(name = "figure_1", fn_name = "figure_1")
p$add_analysis(
name = "figure_2",
fn_name = "plot_epicurve_by_location",
location_code = "nation_nor"
)
p$add_analysis(
name = "figure_3",
fn_name = "plot_epicurve_by_location",
location_code = "county_nor03"
)
p$run_one("figure_1")