Skip to content
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

add some base/S3 classes #434

Merged
merged 19 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions R/S3.R
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use >= : we do want to allow 0x0 but also nx0 etc. NB same for array validation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is referring to the checks on dim(). Yes, good catch. The error message even says "non-negative." Of course, it is impossible to set the "dim" attribute on any R object to a negative value outside of C. I guess this would catch alternative matrix implementations that override dim() and end up in an invalid state, which is really their problem, not ours.

Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,20 @@ validate_factor <- function(self) {
c(
if (typeof(self) != "integer")
"Underlying data must be an <integer>",
if (!is.character(attr(self, "levels")))
"attr(, 'levels') must be a <character>"
if (!is.character(attr(self, "levels", TRUE)))
"attr(, 'levels') must be a <character>",
if (max(unclass(self), 0L) > length(attr(self, "levels", TRUE)))
t-kalinowski marked this conversation as resolved.
Show resolved Hide resolved
"Not enough 'levels' for underlying data"
)
}

validate_date <- function(self) {
c(
if (mode(self) != "numeric")
"Underlying data must be numeric",
if (!inherits(self, "Date"))
"Underlying data must have class 'Date'"
)
}

validate_POSIXct <- function(self) {
Expand Down Expand Up @@ -349,7 +356,7 @@ class_array <- new_S3_class("array",
#' @order 3
class_formula <- new_S3_class("formula",
constructor = function(.data = NULL, env = parent.frame()) {
stats::formula(.data, env)
stats::formula(.data, env = env)
},
validator = validate_formula
)
7 changes: 4 additions & 3 deletions R/class.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,17 @@ new_object <- function(.parent, ...) {
stop(msg)
}

# force .parent before ...
# TODO: Some type checking on `.parent`?
object <- .parent

args <- list(...)
if ("" %in% names2(args)) {
stop("All arguments to `...` must be named")
}

has_setter <- vlapply(class@properties[names(args)], prop_has_setter)

# TODO: Some type checking on `.parent`?
object <- .parent

attrs <- c(
list(class = class_dispatch(class), S7_class = class),
args[!has_setter],
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/S3.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
Code
validate_date("x")
Output
[1] "Underlying data must be numeric"
[1] "Underlying data must be numeric"
[2] "Underlying data must have class 'Date'"

# catches invalid POSIXct

Expand Down
Loading