Skip to content

Commit

Permalink
saves empty dataframe for quicker access (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbarbone committed Jan 18, 2025
1 parent ab0d413 commit 54f5a66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions R/data-frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ NULL
#' @param x A list or `NULL` (see return)
quick_df <- function(x = NULL) {
if (is.null(x)) {
return(empty_df())
return(.empty_df)
}

if (!is.list(x)) {
Expand All @@ -38,12 +38,11 @@ quick_df <- function(x = NULL) {

switch(
length(n) + 1L,
empty_df(),
.empty_df,
struct(
x = x,
class = "data.frame",
# # nolint next: seq_linter
names = names(x) %||% make.names(1:length(x)),
names = names(x) %||% seq_along(x),
row.names = c(NA_integer_, -n)
),
stop(cond_quick_df_list())
Expand All @@ -56,6 +55,13 @@ empty_df <- function() {
struct(list(), "data.frame", row.names = integer(), names = character())
}

.empty_df <- structure(
list(),
class = "data.frame",
names = character(),
row.names = integer()
)

#' @export
#' @rdname quick_df
#' @param ... Columns as `tag = value` (passed to `list()`)
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-data-frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ test_that("quick_df()", {
expect_identical(quick_df(NULL), empty_df())
expect_identical(quick_df(list()), empty_df())
})

test_that("empty_df", {
expect_identical(empty_df(), .empty_df)
})

0 comments on commit 54f5a66

Please sign in to comment.