Skip to content

Commit

Permalink
Small improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Sep 9, 2024
1 parent ae1d1d9 commit 0d9e01b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
14 changes: 9 additions & 5 deletions R/dots.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(...))
}
23 changes: 16 additions & 7 deletions R/new_df.R
Original file line number Diff line number Diff line change
@@ -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`.
Expand All @@ -11,23 +11,32 @@
#' @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 {
N <- NROW(.subset2(out, 1L))
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"
Expand Down
4 changes: 2 additions & 2 deletions man/new_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d9e01b

Please sign in to comment.