From 31a8a73ffa1de86e7069927ce8488a040d14fdd0 Mon Sep 17 00:00:00 2001 From: Tim Riffe Date: Sun, 4 Apr 2021 09:35:42 +0200 Subject: [PATCH] adding importFrom stuff for stats,fertestr --- NAMESPACE | 2 ++ R/utils-pipe.R | 3 +++ R/utils_downloads.R | 18 ++++++++++-------- man/pipe.Rd | 8 ++++++++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 4df356b1a..cbc1bafd5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -183,6 +183,8 @@ importFrom(stats,optim) importFrom(stats,optimize) importFrom(stats,predict) importFrom(stats,quantile) +importFrom(stats,reshape) +importFrom(stats,setNames) importFrom(stats,splinefun) importFrom(stats,uniroot) importFrom(tibble,as.tibble) diff --git a/R/utils-pipe.R b/R/utils-pipe.R index e79f3d808..fd0b1d13d 100644 --- a/R/utils-pipe.R +++ b/R/utils-pipe.R @@ -8,4 +8,7 @@ #' @export #' @importFrom magrittr %>% #' @usage lhs \%>\% rhs +#' @param lhs A value or the magrittr placeholder. +#' @param rhs A function call using the magrittr semantics. +#' @return The result of calling `rhs(lhs)`. NULL diff --git a/R/utils_downloads.R b/R/utils_downloads.R index 8f671ad71..02a7f56c7 100644 --- a/R/utils_downloads.R +++ b/R/utils_downloads.R @@ -13,6 +13,8 @@ #' #' @return numeric matrix of `nLx` with `length(nLxDatesIn)` and abrdiged ages in rows. #' @export +#' @importFrom stats setNames +#' @importFrom stats reshape #' @examples #' # life expectancy calculated from Lx downloaded from WPP19. Using names or codes. #' Lxs_name <- downloadnLx(nLx=NULL, location = "Argentina", @@ -35,8 +37,6 @@ #' } downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") { - requireNamespace("fertestr", quietly = TRUE) - requireNamespace("magrittr", quietly = TRUE) verbose <- getOption("basepop_verbose", TRUE) if (!is.null(nLx)) { @@ -81,7 +81,7 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") { # filter and matrix shape lt_ctry <- lt_wpp19[lt_wpp19$LocID %in% location_code & lt_wpp19$Sex %in% sex_code,] %>% as.data.frame() %>% - reshape(data = ., + stats::reshape(data = ., direction = "wide", idvar = c("LocID","AgeStart","Sex"), timevar = "Year", v.names = "mx", drop = c("AgeSpan","lx")) @@ -92,13 +92,13 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") { seq(1953,2023,5), as.numeric(nLxDatesIn), extrap = TRUE, method = method) %>% as.data.frame() %>% - setNames(as.character(nLxDatesIn)) + stats::setNames(as.character(nLxDatesIn)) ) %>% split(., list(lt_ctry$LocID, lt_ctry$Sex)) %>% lapply(function(X){ Age <- X[["AgeStart"]] apply(X[,-c(1:3)] %>% - as.data.frame()%>% setNames(as.character(nLxDatesIn)), 2, + as.data.frame()%>% stats::setNames(as.character(nLxDatesIn)), 2, function(S){ MortalityLaws::LifeTable(x = Age, mx = S, @@ -125,6 +125,8 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") { #' #' @return numeric matrix interpolated asfr #' @export +#' @importFrom fertestr get_location_code +#' @importFrom stats setNames #' @examples #' # Total fertility ratio calculated from ASFRx downloaded from WPP19. #' # See `downloadnLx` for analogous examples on multiple countries or using codes instead of names. @@ -133,7 +135,7 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") { #' plot(1950:2025, as.numeric(colSums(ASFR_Arg))*5, xlab = "Year", ylab="TFR", ylim=c(1.5,4), t="l") #' } downloadAsfr <- function(Asfrmat, location = NULL, AsfrDatesIn, method="linear") { - requireNamespace("fertestr", quietly = TRUE) + #requireNamespace("fertestr", quietly = TRUE) verbose <- getOption("basepop_verbose", TRUE) if (!is.null(Asfrmat)) { @@ -163,7 +165,7 @@ downloadAsfr <- function(Asfrmat, location = NULL, AsfrDatesIn, method="linear") # spread format asfr_ctry <- asfr_wpp19[asfr_wpp19$LocID %in% location_code,] %>% as.data.frame() %>% - reshape(direction = "wide", idvar = c("LocID","AgeStart"), + stats::reshape(direction = "wide", idvar = c("LocID","AgeStart"), timevar = "Year", v.names = "ASFR") # interp/extrap @@ -171,7 +173,7 @@ downloadAsfr <- function(Asfrmat, location = NULL, AsfrDatesIn, method="linear") as.numeric(AsfrDatesIn), extrap = TRUE, method = method) %>% as.data.frame() %>% - setNames(as.character(AsfrDatesIn)) %>% + stats::setNames(as.character(AsfrDatesIn)) %>% as.matrix() # combination as rowname diff --git a/man/pipe.Rd b/man/pipe.Rd index 0eec75261..a648c2969 100644 --- a/man/pipe.Rd +++ b/man/pipe.Rd @@ -6,6 +6,14 @@ \usage{ lhs \%>\% rhs } +\arguments{ +\item{lhs}{A value or the magrittr placeholder.} + +\item{rhs}{A function call using the magrittr semantics.} +} +\value{ +The result of calling \code{rhs(lhs)}. +} \description{ See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. }