From cc4369f9ebe8ac31884dcb72f37c029b24f94075 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Wed, 1 Apr 2020 15:08:01 -0500 Subject: [PATCH] fix #15 use tibble::enframe() to convert named vectors to empty tibble in list_NA() --- NAMESPACE | 2 ++ R/tbr_gmean.R | 6 ++++-- R/utils.R | 23 +++++++++++++++-------- man/list_NA.Rd | 18 ++++++++++++++++++ tbrf.Rproj | 1 + 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 man/list_NA.Rd diff --git a/NAMESPACE b/NAMESPACE index 7f68769..2f39a95 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,4 +29,6 @@ importFrom(stats,sd) importFrom(stats,var) importFrom(tibble,as.tibble) importFrom(tibble,as_tibble) +importFrom(tibble,enframe) +importFrom(tidyr,pivot_wider) importFrom(tidyr,unnest) diff --git a/R/tbr_gmean.R b/R/tbr_gmean.R index 0f992a5..8acb715 100644 --- a/R/tbr_gmean.R +++ b/R/tbr_gmean.R @@ -94,11 +94,13 @@ tbr_gmean_window <- function(x, tcolumn, unit = "years", n, i, ...) { dots <- list(...) if (is.na(dots$conf)) { - resultsColumns <- c("mean") + resultsColumns <- c("mean" = NA) } else { - resultsColumns <- c("mean", "lwr_ci", "upr_ci") + resultsColumns <- c("mean" = NA, + "lwr_ci" = NA, + "upr_ci" = NA) } # do not calculate the first row, always returns NA diff --git a/R/utils.R b/R/utils.R index 3f8548c..23fe652 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,12 +1,19 @@ -# function to return tibble with NAs as specified -# list_NA(c("mean", "lwr.ci", "upr.ci")) -list_NA <- function(...) { - names <- c(...) - results <- as.list(c(rep(NA, length(names)))) - names(results) <- names - results <- tibble::as_tibble(results) - results[1:length(results)] <- as.numeric(results[1:length(results)]) +#' List NA +#' +#' function to return tibble with NAs as specified +#' @param x named vector +#' +#' @importFrom tibble enframe +#' @importFrom tidyr pivot_wider +#' @return empty tibble +#' @keywords internal +list_NA <- function(x) { + names <- x + + results <- tibble::enframe(names) %>% + mutate(value = as.numeric(.data$value)) %>% + tidyr::pivot_wider() return(results) } diff --git a/man/list_NA.Rd b/man/list_NA.Rd new file mode 100644 index 0000000..be14344 --- /dev/null +++ b/man/list_NA.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{list_NA} +\alias{list_NA} +\title{List NA} +\usage{ +list_NA(x) +} +\arguments{ +\item{x}{named vector} +} +\value{ +empty tibble +} +\description{ +function to return tibble with NAs as specified +} +\keyword{internal} diff --git a/tbrf.Rproj b/tbrf.Rproj index 104a73d..946fb13 100644 --- a/tbrf.Rproj +++ b/tbrf.Rproj @@ -15,4 +15,5 @@ LaTeX: pdfLaTeX BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageCheckArgs: --as-cran PackageRoxygenize: rd,collate,namespace,vignette