mortalityTables.load.R 2.67 KB
Newer Older
1
#' Load a named set of mortality tables provided by the \link{MortalityTables} package
2
#'
3
#' @param dataset The set(s) of life tables to be loaded. A list of all available
4
#'                data sets is provided by the function \code{\link{mortalityTables.list}}.
5
#'                Wildcards (*) are allowed to match and load multiple datasets.
6 7
#' @param package The package that contains the dataset in its \code{extdata/}
#'                directory. Defaults to the "MortalityTables" package.
8 9
#'                Multiple packages can be given as a vector.
#' @param prefix The prefix for the data sets (default is "MortalityTables").
10
#'
11 12 13 14 15 16 17
#' @examples
#' mortalityTables.list()
#' mortalityTables.load("Austria_Annuities_*")
#' mortalityTables.load("Austria_Annuities_AVOe2005R")
#' mortalityTables.load("*Annuities")
#' mortalityTables.load("MyCustomTable", package = c("MyCustomPackage"))
#'
18
#' @export
19 20 21 22
mortalityTables.load = function(dataset, package = c("MortalityTables", "MortalityTablesPrivate"), 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 ")));
23 24 25
    }
    for (set in sets) {
        sname = gsub("[^-A-Za-z0-9_.]", "", set);
26
        message("Loading table dataset '", sname, "'");
27 28 29 30 31 32 33 34 35
        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
            }
        }
        if (!loaded) {
36
            warning(sprintf("Unable to locate dataset '%s' provided by the %s package!", sname, package));
37
        }
38 39
    }
}
40 41 42 43 44 45


#' Load a named set of pension tables provided by the \link{MortalityTables} package
#'
#' @param dataset The set of lifpensione tables to be loaded. A list of all available
#'                data sets is provided by the function \code{\link{pensionTables.list}}.
46
#'                Wildcards (*) are allowed to match and load multiple datasets.
47 48
#' @param package The package that contains the dataset in its \code{extdata/}
#'                directory. Defaults to the "MortalityTables" package.
49
#'                Multiple packages can be given as a vector.
50
#'
51 52 53 54
#' pensionTables.list()
#' pensionTables.load("*")
#' pensionTables.load("USA_PensionPlan_RP2014")
#'
55
#' @export
56 57
pensionTables.load = function(dataset, package = c("MortalityTables", "MortalityTablesPrivate")) {
    mortalityTables.load(dataset = dataset, package = package, prefix = "PensionTables")
58 59 60
}