Skip to content

Commit

Permalink
Merge pull request #329 from helseprofil/dev
Browse files Browse the repository at this point in the history
Add geo_map_multi() and update documentation
  • Loading branch information
vegardlysne authored May 29, 2024
2 parents 557a963 + 847a7ac commit 6379d95
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export(find_implicit_col)
export(find_implicit_null)
export(find_spec)
export(geo_map)
export(geo_map_multi)
export(geo_merge)
export(geo_recode)
export(get_aar)
Expand Down Expand Up @@ -87,6 +88,7 @@ export(read_file)
export(read_log)
export(reset_opt)
export(reset_options)
export(run_orgdata_hdir)
export(save_file)
export(se_fil)
export(see_data)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# orgdata 1.5.0 (dev)

# orgdata 1.4.9
- Fix `geo.merge` [d163c9a](https://github.com/helseprofil/orgdata/commit/d163c9ac08996b0a6ab0be5d6faee2bdbd5b2c1c)
- Add `geo_map_multi()`, to generate a multiyear mappig table. [9b3e1a1](https://github.com/helseprofil/orgdata/commit/9b3e1a1071af6839bdcc1eb1716da33b62867144)
- Fix `geo_merge()` [d163c9a](https://github.com/helseprofil/orgdata/commit/d163c9ac08996b0a6ab0be5d6faee2bdbd5b2c1c)
- Allow specification of encoding in `read_file` [ba980a7](https://github.com/helseprofil/orgdata/commit/ba980a791f96a18a4e2f1782f48ca0f48cf36bd2)

# orgdata 1.4.8
Expand Down
45 changes: 43 additions & 2 deletions R/norgeo.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' @title Granularity of Geographical Codes
#' @description Create a database granularity of geographical codes to aggregate
#' data accordingly. Implementation of this function is base on [norgeo::cast_geo()]
#' function in \href{https://helseprofil.github.io/norgeo/}{norgeo} package.
#' data accordingly. Implementation of this function is based on [norgeo::cast_geo()].
#' @param year Year for the valid geographical codes
#' @param write Write table to the `orgdata.geo` database. It will overwrite
#' the table if it already exists
Expand Down Expand Up @@ -60,6 +59,48 @@ geo_map <- function(year = NULL, write = FALSE, append = FALSE, table = "tblGeo"
return(geo$tblvalue[])
}

#' @title Granularity of Geographical Codes (multi-year)
#' @description A wrapper around [geo_map()] to generate a database granularity
#' of geographical codes to aggregate data accordingly, for multiple years.
#' @param from starting year
#' @param to ending year
#' @param write Write table to the `orgdata.geo` database. It will overwrite
#' the table if it already exists
#' @param table Table name to be created in the database. Default is `tblGeo`
#' @export
geo_map_multi <- function(from = NULL,
to = NULL,
write = FALSE,
table = "tblGeo") {
if (write) {
geoFile <- is_path_db(getOption("orgdata.geo"), check = TRUE)
geo <- KHelse$new(geoFile)
on.exit(geo$db_close(), add = TRUE)
} else {
geo <- listenv::listenv()
}

DT <- data.table::data.table()

for (year in from:to) {
message(paste0("Processing year: ", year))
dt <- geo_map(year, append = FALSE, write = FALSE)
DT <- data.table::rbindlist(list(DT, dt))
}

geo$tblvalue <- DT[, "batch" := is_batch("date")]
geo$tblname <- table

if (write) {
is_write_msg(msg = "write")
geo$db_write(write = write)
msgWrite <- paste0("Write table `", table, "` is completed in: \n")
is_verbose(x = geoFile, msg = msgWrite, type = "note")
}

return(geo$tblvalue[])
}

#' @title Geographical Codes to Recode
#' @description Create a table of current year geographical codes against previous
#' years geogprahical codes. This is used to recode the previous years codes to the
Expand Down
2 changes: 0 additions & 2 deletions R/utils-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ is_correct_globs <- function(x){
#' Temporary fix to run on HDIR systems
#'
#' @export
#'
#' @examples
run_orgdata_hdir <- function(){
options(orgdata.win.drive = "O:")
options(orgdata.folder.db = "Prosjekt/FHP/PRODUKSJON/STYRING/raw-khelse")
Expand Down
3 changes: 1 addition & 2 deletions man/geo_map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/geo_map_multi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/run_orgdata_hdir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6379d95

Please sign in to comment.