Skip to content
Snippets Groups Projects
Commit f9c4c3fa authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Allow loading mortality table datasets from other packages

The mortalityTAbles.list and mortalityTables.load functions now have an additional parameter package, where one can give a third-party package that holds the dataset in extdata/MortalityTables_....R
parent 95cd06a0
No related branches found
No related tags found
No related merge requests found
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
#' An existing life table can then be loaded with \link{mortalityTables.load}. #' An existing life table can then be loaded with \link{mortalityTables.load}.
#' #'
#' @param pattern Restrict the results only to life table sets that match the pattern (default: "*" to show all sets) #' @param pattern Restrict the results only to life table sets that match the pattern (default: "*" to show all sets)
#' @param package The package that contains the desired dataset in its \code{extdata/}
#' directory. Defaults to the "MortalityTables" package.
#' #'
#' @export #' @export
mortalityTables.list = function(pattern="*") { mortalityTables.list = function(pattern = "*", package = "MortalityTables") {
filepath = system.file("extdata", package="MortalityTables"); filepath = system.file("extdata", package = package);
files = Sys.glob(file.path(filepath, paste("MortalityTables_", pattern, ".R", sep=""))) files = Sys.glob(file.path(filepath, paste("MortalityTables_", pattern, ".R", sep = "")))
gsub('^MortalityTables_(.*).R$', '\\1', basename(files)) gsub('^MortalityTables_(.*).R$', '\\1', basename(files))
} }
......
...@@ -4,23 +4,24 @@ ...@@ -4,23 +4,24 @@
#' data sets is provided by the function \code{\link{mortalityTables.list}}. #' data sets is provided by the function \code{\link{mortalityTables.list}}.
#' @param wildcard Whether the dataset name contains wildcard. If TRUE, all #' @param wildcard Whether the dataset name contains wildcard. If TRUE, all
#' datasets matching the pattern will be loaded #' datasets matching the pattern will be loaded
#' @param package The package that contains the dataset in its \code{extdata/}
#' directory. Defaults to the "MortalityTables" package.
#' #'
#' @export #' @export
mortalityTables.load = function(dataset, wildcard=FALSE) { mortalityTables.load = function(dataset, wildcard=FALSE, package="MortalityTables") {
if (wildcard) { if (wildcard) {
sets = mortalityTables.list(dataset); sets = mortalityTables.list(dataset, package = package);
} else { } else {
sets = c(dataset); sets = c(dataset);
} }
for (set in sets) { for (set in sets) {
sname = gsub("[^-A-Za-z0-9_.]", "", set); sname = gsub("[^-A-Za-z0-9_.]", "", set);
message("Loading mortality table data set '", sname, "'"); message("Loading mortality table data set '", sname, "'");
filename = system.file("extdata", paste("MortalityTables_", sname, ".R", sep = ""), package="MortalityTables"); filename = system.file("extdata", paste("MortalityTables_", sname, ".R", sep = ""), package = package);
if (filename != "") { if (filename != "") {
sys.source(filename, envir = globalenv()) sys.source(filename, envir = globalenv())
#envir=topenv())
} else { } else {
warning(sprintf("Unable to locate dataset '%s' provided by the MortalityTables package!", sname)); warning(sprintf("Unable to locate dataset '%s' provided by the %s package!", sname, package));
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment