Skip to content

Commit

Permalink
Add utils for coercing to dataframe (#272)
Browse files Browse the repository at this point in the history
* add utils for coercing to dataframe

* fix failing test
  • Loading branch information
etiennebacher authored Sep 28, 2022
1 parent 973607e commit 1552145
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
18 changes: 10 additions & 8 deletions R/data_arrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ data_arrange <- function(data, select = NULL, safe = TRUE) {
return(data)
}

# coerce to data frame?
if (!is.data.frame(data)) {
data <- tryCatch(as.data.frame(data, stringsAsFactors = FALSE),
error = function(e) {
stop("Could not coerce `data` into a data frame.", call. = FALSE)
}
)
}
# Input validation check
data <- .coerce_to_dataframe(data)

# find which vars should be decreasing
desc <- select[grepl("^-", select)]
Expand All @@ -44,6 +38,7 @@ data_arrange <- function(data, select = NULL, safe = TRUE) {

# check for variables that are not in data
dont_exist <- select[which(!select %in% names(data))]

if (length(dont_exist) > 0) {
if (!safe) {
insight::format_error(
Expand All @@ -52,6 +47,13 @@ data_arrange <- function(data, select = NULL, safe = TRUE) {
text_concatenate(dont_exist), "."
)
)
} else {
insight::format_warning(
paste0(
"The following column(s) don't exist in the dataset: ",
text_concatenate(dont_exist), "."
)
)
}
select <- select[-which(select %in% dont_exist)]
}
Expand Down
9 changes: 1 addition & 8 deletions R/data_partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ data_partition <- function(data,
training_proportion = proportion,
...) {
# Sanity checks
if (!is.data.frame(data)) {
data <- tryCatch(as.data.frame(data), error = function(e) NULL)
if (is.null(data)) {
insight::format_error(
"`data` needs to be a data frame, or an object that can be coerced to a data frame."
)
}
}
data <- .coerce_to_dataframe(data)

if (sum(proportion) > 1) {
stop("`proportion` cannot be higher than 1.", call. = FALSE)
Expand Down
7 changes: 2 additions & 5 deletions R/select_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,8 @@
insight::format_error("The `data` argument must be provided.")
}
# check data frame input
if (!is.null(data) && !is.data.frame(data)) {
data <- try(as.data.frame(data), silent = TRUE)
if (inherits(data, c("try-error", "simpleError"))) {
insight::format_error("The `data` argument must be a data frame, or an object that can be coerced to a data frame.")
}
if (!is.null(data)) {
data <- .coerce_to_dataframe(data)
}
}

Expand Down
19 changes: 19 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@
`%||%` <- function(x, y) {
if (is.null(x)) y else x
}


#' Try to convert object to a dataframe
#'
#' @keywords internal
#' @noRd
.coerce_to_dataframe <- function(data) {
if (!is.data.frame(data)) {
data <- tryCatch(
as.data.frame(data, stringsAsFactors = FALSE),
error = function(e) {
insight::format_error(
"`data` must be a data frame, or an object that can be coerced to a data frame."
)
}
)
}
data
}
2 changes: 1 addition & 1 deletion tests/testthat/test-data_partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("data_partition works as expected", {

expect_error(
data_partition(new.env()),
"`data` needs to be a data frame"
"`data` must be a data frame"
)

# to be coerced to data frames
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
test_that(".coerce_to_dataframe works for matrices", {
mat <- matrix(c(1, 2, 3, 11, 12, 13), nrow = 2, ncol = 3, byrow = TRUE)

expect_equal(
.coerce_to_dataframe(mat),
data.frame(
V1 = c(1, 11),
V2 = c(2, 12),
V3 = c(3, 13)
)
)
})

test_that(".coerce_to_dataframe works for vectors and list", {
expect_equal(
.coerce_to_dataframe(1:3),
data.frame(data = 1:3)
)

expect_equal(
.coerce_to_dataframe(c("a", "b", "c")),
data.frame(data = c("a", "b", "c"), stringsAsFactors = FALSE)
)

expect_equal(
.coerce_to_dataframe(list(var1 = 1:3, var2 = 4:6)),
data.frame(var1 = 1:3, var2 = 4:6)
)
})

test_that(".coerce_to_dataframe errors correctly if can't coerce", {
expect_error(
.coerce_to_dataframe(list(var1 = 1:3, var2 = 4:5)),
regexp = "object that can be coerced"
)
})

0 comments on commit 1552145

Please sign in to comment.