From 0d9e01b3174ebd3200096afaf2537c1aad6c5dd5 Mon Sep 17 00:00:00 2001 From: Nick Christofides <118103879+NicChr@users.noreply.github.com> Date: Mon, 9 Sep 2024 08:25:54 +0100 Subject: [PATCH] Small improvements. --- R/dots.R | 14 +++++++++----- R/new_df.R | 23 ++++++++++++++++------- man/new_df.Rd | 4 ++-- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/R/dots.R b/R/dots.R index 3dbc377..e8bd67a 100644 --- a/R/dots.R +++ b/R/dots.R @@ -16,15 +16,19 @@ named_dots <- function(...){ dot_nms <- names(dots) if (is.null(dot_nms)){ - names(dots) <- dot_expr_names(...) + names(dots) <- expr_names(...) } else if (!all(nzchar(dot_nms))){ empty <- which_(!nzchar(dot_nms)) - expr_names <- dot_expr_names(...) - dot_nms[empty] <- expr_names[empty] + expr_nms <- expr_names(...) + dot_nms[empty] <- expr_nms[empty] names(dots) <- dot_nms } dots } -dot_expr_names <- function(...){ - vapply(substitute(alist(...))[-1L], deparse2, "", USE.NAMES = FALSE) +expr_names <- function(...){ + as.character(substitute(c(...))[-1L]) + # vapply(substitute(alist(...))[-1L], deparse2, "", USE.NAMES = FALSE) +} +list_named <- function(...){ + cpp_list_rm_null(named_dots(...)) } diff --git a/R/new_df.R b/R/new_df.R index b07062b..6edc4b9 100644 --- a/R/new_df.R +++ b/R/new_df.R @@ -1,7 +1,7 @@ #' Fast data frame constructor #' #' @param ... Key-value pairs. -#' @param ..N `integer(1)` (Optional) number of rows. \cr +#' @param .nrows `integer(1)` (Optional) number of rows. \cr #' Commonly used to initialise a 0-column data frame with rows. #' @param .recycle `logical(1)` Should arguments be recycled? #' Default is `FALSE`. @@ -11,12 +11,16 @@ #' @returns A `data.frame` #' #' @export -new_df <- function(..., ..N = NULL, .recycle = FALSE, .name_repair = FALSE){ - out <- cpp_list_rm_null(named_dots(...)) +new_df <- function(..., .nrows = NULL,.recycle = FALSE, .name_repair = FALSE){ + + out <- list_named(...) + + # Recycle if (.recycle){ - out <- do.call(cheapr::recycle, out) + out <- do.call(function(...) recycle(..., length = .nrows), out) } - if (is.null(..N)){ + + if (is.null(.nrows)){ if (length(out) == 0L){ row_names <- integer() } else { @@ -24,10 +28,15 @@ new_df <- function(..., ..N = NULL, .recycle = FALSE, .name_repair = FALSE){ row_names <- c(NA_integer_, -N) } } else { - row_names <- .set_row_names(..N) + row_names <- .set_row_names(.nrows) } + out_names <- as.character(attr(out, "names", TRUE)) - if (.name_repair) out_names <- unique_name_repair(out_names) + + if (.name_repair){ + out_names <- unique_name_repair(out_names) + } + attr(out, "names") <- out_names attr(out, "row.names") <- row_names class(out) <- "data.frame" diff --git a/man/new_df.Rd b/man/new_df.Rd index 8485331..390354d 100644 --- a/man/new_df.Rd +++ b/man/new_df.Rd @@ -4,12 +4,12 @@ \alias{new_df} \title{Fast data frame constructor} \usage{ -new_df(..., ..N = NULL, .recycle = FALSE, .name_repair = FALSE) +new_df(..., .nrows = NULL, .recycle = FALSE, .name_repair = FALSE) } \arguments{ \item{...}{Key-value pairs.} -\item{..N}{\code{integer(1)} (Optional) number of rows. \cr +\item{.nrows}{\code{integer(1)} (Optional) number of rows. \cr Commonly used to initialise a 0-column data frame with rows.} \item{.recycle}{\code{logical(1)} Should arguments be recycled?