On 09/09/2021 18:23, Hugh Parsonage wrote:
I would like to register an S3 method for `is.unsorted` for my
package's class "factor256" but I'm having trouble honouring the
`strictly` argument. I've defined
is.unsorted.factor256 <- function(x, na.rm = FALSE, strictly = FALSE) {
strictly
}
i.e. the class is sorted iff strictly = TRUE
However, the strictly argument appears to be ignored
x <- integer(2)
class(x) <- "factor256"
is.unsorted(x) # FALSE [expected]
is.unsorted(x, strictly = TRUE) # FALSE [unexpected]
The method is definitely being dispatched as when I change the function to
is.unsorted.factor256 <- function(x, na.rm = FALSE, strictly = FALSE) {
cat("dispatching\n")
strictly
}
I see the dispatch:
is.unsorted(ff, strictly = T)
#> dispatching
#> [1] FALSE
Moreover, if I define
is.unsorted.factor256 <- function(x, na.rm = FALSE, strictly = FALSE) {
cat("dispatching\n")
print(match.call())
strictly
}
I get:
is.unsorted(ff, strictly = T)
# dispatching
# is.unsorted.factor256(x = x, na.rm = strictly)
# [1] FALSE
While defining not a method but a classic function works as expected:
f=function(x, na.rm = FALSE, strictly = FALSE) {
cat("f\n")
print(match.call())
strictly
}
f(ff, strictly=T)
# f
# f(x = ff, strictly = T)
# [1] TRUE
Just my 0.02 €
Serguei.
and
methods("is.unsorted")
[1] is.unsorted.factor256
see '?methods' for accessing help and source code
I note that if I omit the na.rm = argument I will get the intended
result (I was drawn to the solution by noting the .Internal call has
omitted it) but I'm wondering whether this is correct.
Best,
Hugh Parsonage.
______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel
______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel