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

Train custom classes #481

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

teunbrand
Copy link
Contributor

This PR aims to fix #480.

For discrete training, is.factor(x) is swapped out by !is.null(levels(x)).
That way, any custom class that implements levels.custom() should be able to train a range.

For continuous training, we require the result of range(x) to be numeric (rather than x itself).
Error message is slightly terser, but that isn't too bad.

devtools::load_all("~/packages/scales")
#> ℹ Loading scales
#> Warning: package 'testthat' was built under R version 4.4.2
library(vctrs)

newdisc <- new_list_of(x = list("A", c("B", "C")), ptype = character(), class = "foo")
levels.foo <- function(x) unique(unlist(x))
train_discrete(newdisc)
#> [1] "A" "B" "C"

newcont <- new_list_of(x = list(1, c(5, 3)), ptype = double(), class = "bar")
range.bar <- function(x, ...) range(unlist(x), ...)
train_continuous(newcont)
#> [1] 1 5

Created on 2024-11-19 with reprex v2.1.1

@@ -41,6 +41,11 @@ train_continuous <- function(new, existing = NULL, call = caller_env()) {
return(existing)
}

new <- try_fetch(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The as.numeric() requirement is in L55. Because as as.numeric(LETTERS) returns a vector of NA_real_ without throwing an error, it seemed more sensible to require the output of range() to be numeric,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make sure range training works with custom classes
1 participant