Commit 49e4d9cc authored by Reinhold Kainhofer's avatar Reinhold Kainhofer

Extend mortalityTables.load and .list to search all installed packages for...

Extend mortalityTables.load and .list to search all installed packages for table data (using regexp in the package namse)

Also make sure the providing package is loaded when a corresponding data set is loaded
parent 075c08d1
#' List all available sets of life tables provided by the \link[MortalityTables]{MortalityTables-package} package
#' 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 with wildcards (default: "*" to show all sets)
#' @param package The package that contains the desired dataset in its \code{extdata/}
#' directory. Defaults to the "MortalityTables" package.
#' Multiple packages can be given as a vector.
#' Multiple packages can be given as a vector, even using regular expressions.
#' @param prefix The file prefix, defaults to MortalityTables. Can be overridden to list other types of files, like "PensionTables"
#'
#' @examples
......@@ -14,24 +14,26 @@
#' mortalityTables.list(package = c("MyCustomPackage"))
#'
#' @export
mortalityTables.list = function(pattern = "*", package = c("MortalityTables", "MortalityTablesPrivate"), prefix = "MortalityTables") {
mortalityTables.list = function(pattern = "*", package = c("^MortalityTables", "^PensionTables"), prefix = "MortalityTables") {
ret = c()
for (p in c(package)) {
filepath = system.file("extdata", package = p);
files = Sys.glob(file.path(filepath, paste(prefix, "_", pattern, ".R", sep = "")))
ret = c(ret, gsub(paste('^', prefix, '_(.*).R$', sep = ""), '\\1', basename(files)))
pkgs = installed.packages()
for (p in pkgs[,1]) {
if (any(sapply(package, grepl, p))) { # package matches the pattern given as argument
filepath = system.file("extdata", package = p);
files = Sys.glob(file.path(filepath, paste(prefix, "_", pattern, ".R", sep = "")))
ret = c(ret, gsub(paste('^', prefix, '_(.*).R$', sep = ""), '\\1', basename(files)))
}
}
ret
}
#' List all available sets of pension tables provided by the \link[MortalityTables]{MortalityTables-package} package
#' An existing pension table can then be loaded with \link{pensionTables.load}.
#'
#' @param pattern Restrict the results only to pension table sets that match the pattern (default: "*" to show all sets)
#' @param pattern Restrict the results only to pension table sets that match the pattern with wildcards (default: "*" to show all sets)
#' @param package The package that contains the desired dataset in its \code{extdata/}
#' directory. Defaults to the "MortalityTables" package.
#' Multiple packages can be given as a vector.
#' Multiple packages can be given as a vector, even using regular expressions.
#'
#' @examples
#' pensionTables.list()
......@@ -39,6 +41,6 @@ mortalityTables.list = function(pattern = "*", package = c("MortalityTables", "M
#' pensionTables.list(package = c("MyCustomPackage"))
#'
#' @export
pensionTables.list = function(pattern = "*", package = c("MortalityTables", "MortalityTablesPrivate")) {
pensionTables.list = function(pattern = "*", package = c("^MortalityTables", "^PensionTables")) {
mortalityTables.list(pattern = pattern, package = package, prefix = "PensionTables")
}
......@@ -4,8 +4,9 @@
#' data sets is provided by the function \code{\link{mortalityTables.list}}.
#' Wildcards (*) are allowed to match and load multiple datasets.
#' @param package The package that contains the dataset in its \code{extdata/}
#' directory. Defaults to the "MortalityTables" package.
#' Multiple packages can be given as a vector.
#' directory. Defaults to all packages starting with names that
#' start with "MortalityTables" or "PensionTables".
#' Multiple packages can be given as a vector, even using regular expressions.
#' @param prefix The prefix for the data sets (default is "MortalityTables").
#'
#' @examples
......@@ -16,20 +17,24 @@
#' mortalityTables.load("MyCustomTable", package = c("MyCustomPackage"))
#'
#' @export
mortalityTables.load = function(dataset, package = c("MortalityTables", "MortalityTablesPrivate"), prefix = "MortalityTables") {
mortalityTables.load = function(dataset, package = c("^MortalityTables", "^PensionTables"), prefix = "MortalityTables") {
sets = mortalityTables.list(dataset, package = package, prefix = prefix);
if (length(sets) == 0) {
warning(sprintf("Unable to locate dataset '%s' provided by the %s package!", dataset, paste(c(package), collapse = " or ")));
}
pkgs = installed.packages()
for (set in sets) {
sname = gsub("[^-A-Za-z0-9_.]", "", set);
message("Loading table dataset '", sname, "'");
loaded = FALSE;
for (p in c(package)) {
filename = system.file("extdata", paste(prefix, "_", sname, ".R", sep = ""), package = p);
if (filename != "") {
sys.source(filename, envir = globalenv())
loaded = TRUE
for (p in pkgs[,1]) {
if (any(sapply(package, grepl, p))) { # package matches the pattern given as argument
filename = system.file("extdata", paste(prefix, "_", sname, ".R", sep = ""), package = p);
if (filename != "") {
require(p, character.only = TRUE) # Make sure the providing package is loaded, in case it provides helper functions
sys.source(filename, envir = globalenv())
loaded = TRUE
}
}
}
if (!loaded) {
......@@ -45,16 +50,16 @@ mortalityTables.load = function(dataset, package = c("MortalityTables", "Mortali
#' data sets is provided by the function \code{\link{pensionTables.list}}.
#' Wildcards (*) are allowed to match and load multiple datasets.
#' @param package The package that contains the dataset in its \code{extdata/}
#' directory. Defaults to the "MortalityTables" package.
#' Multiple packages can be given as a vector.
#' directory. Defaults to all packages starting with names that
#' start with "MortalityTables" or "PensionTables".
#' Multiple packages can be given as a vector, even using regular expressions.
#'
#' pensionTables.list()
#' pensionTables.load("*")
#' pensionTables.load("USA_PensionPlan_RP2014")
#'
#' @export
pensionTables.load = function(dataset, package = c("MortalityTables", "MortalityTablesPrivate")) {
pensionTables.load = function(dataset, package = c("^MortalityTables", "^PensionTables")) {
mortalityTables.load(dataset = dataset, package = package, prefix = "PensionTables")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment