Skip to content

Commit

Permalink
Improve verify_data function
Browse files Browse the repository at this point in the history
Automatically parse allowed data types rather than manual specification. Additional checks if data are of type `matrix` are left in.
  • Loading branch information
pepijnvink committed Dec 18, 2023
1 parent 6a69f13 commit 56f55f6
Showing 1 changed file with 11 additions and 43 deletions.
54 changes: 11 additions & 43 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,18 @@ verify_data <- function(data,
df = FALSE,
imp = FALSE,
pred = FALSE) {
if (df && !imp) {
if (!(is.data.frame(data) || is.matrix(data))) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'data.frame' or 'matrix'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
}
if (df && imp) {
if (!(is.data.frame(data) ||
is.matrix(data) || mice::is.mids(data))) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'data.frame', 'matrix', or 'mids'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
df <- data.frame(val = unlist(as.list(environment())[-1]),
type = c("data.frame", "mids", "matrix"))
types <- df[df$val==T,]$type

Check warning on line 44 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=44,col=21,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 44 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=44,col=24,[T_and_F_symbol_linter] Use TRUE instead of the symbol T.

Check warning on line 44 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=44,col=25,[commas_linter] Commas should always have a space after.
types_format <- purrr::map(types, function(x) paste0("`", x, "`")) %>% unlist()

Check warning on line 45 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=45,col=3,[object_usage_linter] local variable 'types_format' assigned but may not be used
if(!inherits(data, types)){

Check warning on line 46 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=46,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.
cli::cli_abort(c(
"!" = "The {.arg data} argument requires an object of class {stringr::str_flatten_comma({types_format}, \", or \")}.",
"i" = "Input object is of class {class(data)}"
),
call. = FALSE)
}
if (imp && !df) {
if (!mice::is.mids(data)) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'mids'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
}
if (pred) {
if (!is.matrix(data)) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'matrix'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
if (is.matrix(data)) {
if (dim(data)[1] != dim(data)[2]) {
cli::cli_abort(
c(
Expand Down

0 comments on commit 56f55f6

Please sign in to comment.