Skip to content

Move the check on misuse of data.frame() to tests #3758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions R/performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ data_frame <- function(...) {
new_data_frame(list(...))
}

data.frame <- function(...) {
abort(glue("
Please use `data_frame()` or `new_data_frame()` instead of `data.frame()` for better performance.
See the vignette 'ggplot2 internal programming guidelines' for details.
"))
}

split_matrix <- function(x, col_names = colnames(x)) {
force(col_names)
x <- lapply(seq_len(ncol(x)), function(i) x[, i])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ get_n_warning <- function(f) {
sum(d$token == "SYMBOL_FUNCTION_CALL" & d$text == "warning")
}

get_n_data.frame <- function(f) {
d <- getParseData(parse(f, keep.source = TRUE))
sum(d$token == "SYMBOL_FUNCTION_CALL" & d$text == "data.frame")
}

test_that("`get_n_*() detects number of calls properly", {
withr::local_file("tmp.R")
writeLines(
c(
'stop("foo!")',
'warning("bar!")',
"data.frame(x = 1)"
),
"tmp.R"
)

expect_equal(get_n_stop("tmp.R"), 1)
expect_equal(get_n_warning("tmp.R"), 1)
expect_equal(get_n_data.frame("tmp.R"), 1)
})

# Pattern is needed filter out files such as ggplot2.rdb, which is created when running covr::package_coverage()
R_files <- list.files("../../R", pattern = ".*\\.(R|r)$", full.names = TRUE)

Expand All @@ -22,3 +43,8 @@ test_that("do not use warning()", {
warnings <- vapply(R_files, get_n_warning, integer(1))
expect_equal(sum(warnings), 0)
})

test_that("do not use data.frame(), use `data_frame()` or `new_data_frame()`", {
data.frames <- vapply(R_files, get_n_data.frame, integer(1))
expect_equal(sum(data.frames), 0)
})