| Title: | A skeleton implementation of cs9 (Core Surveillance 9) |
|---|---|
| Description: | A skeleton implementation of cs9 (Surveillance Core 9). |
| Authors: | Richard Aubrey White [aut, cre] (ORCID: <https://orcid.org/0000-0002-6747-1726>), Folkehelseinstituttet [cph] |
| Maintainer: | Richard Aubrey White <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 26.8.23 |
| Built: | 2026-10-13 19:54:02 UTC |
| Source: | https://github.com/niphr/cs9example |
cs9example creates global empty, and fills it when the package loads. It
holds two things every task needs. global$border is the Norwegian border
year the package is configured for. global$ss is a
cs9::SurveillanceSystem_v9 object with every database table and every task
already registered.
globalglobal
cs9::SurveillanceSystem_v9 for the class global$ss is an
instance of, and make_skeleton_date() for the skeleton builder the tasks
use. cs9example has no vignettes of its own; the framework is documented in
the cs9 package.
# The border year, set when the package loads. global$border # The two tables and the three tasks the package registers. names(global$ss$tables) names(global$ss$tasks)# The border year, set when the package loads. global$border # The two tables and the three tasks the package registers. names(global$ss$tables) names(global$ss$tasks)
Creates a data skeleton with complete date sequences and location codes for the geographic granularities you specify.
make_skeleton_date( date_min, date_max, granularity_geo, location_reference = csdata::nor_locations_names() )make_skeleton_date( date_min, date_max, granularity_geo, location_reference = csdata::nor_locations_names() )
date_min |
Start date (Date or character in YYYY-MM-DD format) |
date_max |
End date (Date or character in YYYY-MM-DD format) |
granularity_geo |
Vector of granularity_geo values to include |
location_reference |
Location reference data, defaults to csdata::nor_locations_names() |
data.table with complete skeleton
weather_clean_data_action(), the task action that starts from this
skeleton, and csdata::nor_locations_names() for the default location
reference. cs9example has no vignettes of its own; the framework is
documented in the cs9 package.
Other skeleton builders:
make_skeleton_isoyearweek()
# Every county, every day of the first week of 2024, with no data merged in # yet: 15 counties x 7 days. skeleton <- make_skeleton_date( date_min = "2024-01-01", date_max = "2024-01-07", granularity_geo = "county" ) nrow(skeleton) head(skeleton)# Every county, every day of the first week of 2024, with no data merged in # yet: 15 counties x 7 days. skeleton <- make_skeleton_date( date_min = "2024-01-01", date_max = "2024-01-07", granularity_geo = "county" ) nrow(skeleton) head(skeleton)
Creates a data skeleton with complete ISO year-week sequences and location codes for the geographic granularities you specify.
make_skeleton_isoyearweek( isoyearweek_min, isoyearweek_max, granularity_geo, location_reference = csdata::nor_locations_names() )make_skeleton_isoyearweek( isoyearweek_min, isoyearweek_max, granularity_geo, location_reference = csdata::nor_locations_names() )
isoyearweek_min |
Start ISO year-week (character in YYYY-WW format) |
isoyearweek_max |
End ISO year-week (character in YYYY-WW format) |
granularity_geo |
Vector of granularity_geo values to include |
location_reference |
Location reference data, defaults to csdata::nor_locations_names() |
data.table with complete skeleton
make_skeleton_date() for the daily equivalent, and
csdata::nor_locations_names() for the default location reference.
cs9example has no vignettes of its own; the framework is documented in the
cs9 package.
Other skeleton builders:
make_skeleton_date()
# Every county, the first three ISO weeks of 2024: 15 counties x 3 weeks. skeleton <- make_skeleton_isoyearweek( isoyearweek_min = "2024-01", isoyearweek_max = "2024-03", granularity_geo = "county" ) nrow(skeleton) head(skeleton)# Every county, the first three ISO weeks of 2024: 15 counties x 3 weeks. skeleton <- make_skeleton_isoyearweek( isoyearweek_min = "2024-01", isoyearweek_max = "2024-03", granularity_geo = "county" ) nrow(skeleton) head(skeleton)
Lays out a complete municipality-by-date skeleton with
make_skeleton_date(). Merges the raw weather onto that skeleton.
Aggregates it up to county and to nation. Aggregates the whole lot up again
to ISO year-week. The action formats both granularities as
csfmt_rts_data_v2, stacks them, and writes them to
anon_example_weather_data. The write replaces whatever was there.
weather_clean_data_action(data, argset, tables)weather_clean_data_action(data, argset, tables)
data |
Data |
argset |
Argset |
tables |
DB tables |
weather_clean_data_data_selector(), which produces this action's
data argument, and make_skeleton_date() for the skeleton it starts
from. cs9example has no vignettes of its own; how cs9 pairs an action with
a data selector is documented in cs9::SurveillanceSystem_v9.
Other cs9 task actions:
weather_download_and_import_rawdata_action(),
weather_export_plots_action()
## Not run: # Needs a live cs9 PostgreSQL database: the action ends by dropping every row # of anon_example_weather_data and inserting the cleaned data in its place. global$ss$run_task("weather_clean_data") ## End(Not run)## Not run: # Needs a live cs9 PostgreSQL database: the action ends by dropping every row # of anon_example_weather_data and inserting the cleaned data in its place. global$ss$run_task("weather_clean_data") ## End(Not run)
Pulls the daily, municipality-level, total-age, total-sex rows out of
anon_example_weather_rawdata. Returns them under the name date_municip,
ordered by location and date.
weather_clean_data_data_selector(argset, tables)weather_clean_data_data_selector(argset, tables)
argset |
Argset |
tables |
DB tables |
A named list with one element, date_municip: a data.table of
granularity_time, location_code, date, temp_max, temp_min and
precip.
weather_clean_data_action(), which consumes this list, and
cs9::mandatory_db_filter() for the filter it applies. cs9example has no
vignettes of its own; how cs9 pairs a data selector with an action is
documented in cs9::SurveillanceSystem_v9.
Other cs9 task data selectors:
weather_download_and_import_rawdata_data_selector(),
weather_export_plots_data_selector()
## Not run: # Needs a live cs9 PostgreSQL database: it reads the # anon_example_weather_rawdata table. weather_clean_data_data_selector( argset = global$ss$shortcut_get_argset("weather_clean_data"), tables = global$ss$shortcut_get_tables("weather_clean_data") ) ## End(Not run)## Not run: # Needs a live cs9 PostgreSQL database: it reads the # anon_example_weather_rawdata table. weather_clean_data_data_selector( argset = global$ss$shortcut_get_argset("weather_clean_data"), tables = global$ss$shortcut_get_tables("weather_clean_data") ) ## End(Not run)
Keeps the 00, 06, 12 and 18 o'clock timepoints of the forecast that
weather_download_and_import_rawdata_data_selector() returned. Reduces them
to one row per date: maximum air temperature, minimum air temperature and
total precipitation. Upserts those rows into anon_example_weather_rawdata.
weather_download_and_import_rawdata_action(data, argset, tables)weather_download_and_import_rawdata_action(data, argset, tables)
data |
Data |
argset |
Argset |
tables |
DB tables |
weather_download_and_import_rawdata_data_selector(), which
produces this action's data argument. cs9example has no vignettes of its
own; how cs9 pairs an action with a data selector is documented in
cs9::SurveillanceSystem_v9.
Other cs9 task actions:
weather_clean_data_action(),
weather_export_plots_action()
## Not run: # Needs a live cs9 PostgreSQL database: the last thing the action does is # upsert into the anon_example_weather_rawdata table. global$ss$run_task("weather_download_and_import_rawdata") ## End(Not run)## Not run: # Needs a live cs9 PostgreSQL database: the last thing the action does is # upsert into the anon_example_weather_rawdata table. global$ss$run_task("weather_download_and_import_rawdata") ## End(Not run)
Takes the mean latitude and longitude of the municipality named in
argset$location_code. Downloads the MET Norway location forecast for that
point. Returns it under the name data.
weather_download_and_import_rawdata_data_selector(argset, tables)weather_download_and_import_rawdata_data_selector(argset, tables)
argset |
Argset |
tables |
DB tables |
A named list with one element, data: the parsed forecast.
weather_download_and_import_rawdata_action(), which consumes this
list. cs9example has no vignettes of its own; how cs9 pairs a data selector
with an action is documented in cs9::SurveillanceSystem_v9.
Other cs9 task data selectors:
weather_clean_data_data_selector(),
weather_export_plots_data_selector()
## Not run: # Needs internet access to api.met.no, and the csmaps package for the # municipality's coordinates. weather_download_and_import_rawdata_data_selector( argset = list(location_code = "municip_nor0301"), tables = NULL ) ## End(Not run)## Not run: # Needs internet access to api.met.no, and the csmaps package for the # municipality's coordinates. weather_download_and_import_rawdata_data_selector( argset = list(location_code = "municip_nor0301"), tables = NULL ) ## End(Not run)
Creates argset$output_dir, draws data$data as a ribbon between temp_min
and temp_max over date, and saves it with ggplot2::ggsave() to
argset$output_absolute_path. That path is a glue::glue() template
evaluated once against argset, so "{argset$output_dir}" resolves but a
brace left inside argset$output_filename does not.
weather_export_plots_action(data, argset, tables)weather_export_plots_action(data, argset, tables)
data |
Data |
argset |
Argset |
tables |
DB tables |
This is the one action that touches no database table, so tables is unused
and the example below passes NULL for it.
weather_export_plots_data_selector(), which produces this action's
data argument. cs9example has no vignettes of its own; how cs9 pairs an
action with a data selector is documented in cs9::SurveillanceSystem_v9.
Other cs9 task actions:
weather_clean_data_action(),
weather_download_and_import_rawdata_action()
# An argset shaped like the one cs9 builds for this task, and a stand-in for # what the data selector would have returned. d <- list(data = data.table::data.table( date = as.Date("2024-01-01") + 0:29, temp_min = seq(-8, 6, length.out = 30), temp_max = seq(-2, 12, length.out = 30) )) argset <- list( location_code = "county_nor03", output_dir = fs::path(tempdir(), "cs9example-plots"), output_filename = "weather_county_nor03.png", output_absolute_path = fs::path("{argset$output_dir}", "{argset$output_filename}"), first_analysis = TRUE, last_analysis = TRUE ) weather_export_plots_action(data = d, argset = argset, tables = NULL) fs::dir_ls(argset$output_dir)# An argset shaped like the one cs9 builds for this task, and a stand-in for # what the data selector would have returned. d <- list(data = data.table::data.table( date = as.Date("2024-01-01") + 0:29, temp_min = seq(-8, 6, length.out = 30), temp_max = seq(-2, 12, length.out = 30) )) argset <- list( location_code = "county_nor03", output_dir = fs::path(tempdir(), "cs9example-plots"), output_filename = "weather_county_nor03.png", output_absolute_path = fs::path("{argset$output_dir}", "{argset$output_filename}"), first_analysis = TRUE, last_analysis = TRUE ) weather_export_plots_action(data = d, argset = argset, tables = NULL) fs::dir_ls(argset$output_dir)
Pulls every row of anon_example_weather_data for the location named in
argset$location_code. Returns the date, temp_max and temp_min
columns under the name data, ordered by date.
weather_export_plots_data_selector(argset, tables)weather_export_plots_data_selector(argset, tables)
argset |
Argset |
tables |
DB tables |
A named list with one element, data: a data.table of date,
temp_max and temp_min.
weather_export_plots_action(), which consumes this list, and
cs9::mandatory_db_filter() for the filter it applies. cs9example has no
vignettes of its own; how cs9 pairs a data selector with an action is
documented in cs9::SurveillanceSystem_v9.
Other cs9 task data selectors:
weather_clean_data_data_selector(),
weather_download_and_import_rawdata_data_selector()
## Not run: # Needs a live cs9 PostgreSQL database: it reads the # anon_example_weather_data table. weather_export_plots_data_selector( argset = global$ss$shortcut_get_argset("weather_export_plots"), tables = global$ss$shortcut_get_tables("weather_export_plots") ) ## End(Not run)## Not run: # Needs a live cs9 PostgreSQL database: it reads the # anon_example_weather_data table. weather_export_plots_data_selector( argset = global$ss$shortcut_get_argset("weather_export_plots"), tables = global$ss$shortcut_get_tables("weather_export_plots") ) ## End(Not run)