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

Bug in s3_dispatch #28

Open
Tazinho opened this issue Dec 31, 2020 · 2 comments
Open

Bug in s3_dispatch #28

Tazinho opened this issue Dec 31, 2020 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@Tazinho
Copy link

Tazinho commented Dec 31, 2020

(Probably due to a change in R 4.0; I discovered this when solving the regarding exercise in Adv R)

# Output in R 4.0.3
x <- structure(1:10, class = "test")

sloop::s3_dispatch(t(x))
#> => t.test
#>  * t.default
t(x)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]    1    2    3    4    5    6    7    8    9    10
#> attr(,"class")
#> [1] "test"
t.test(x)
#> 
#>  One Sample t-test
#> 
#> data:  x
#> t = 5.7446, df = 9, p-value = 0.0002782
#> alternative hypothesis: true mean is not equal to 0
#> 95 percent confidence interval:
#>  3.334149 7.665851
#> sample estimates:
#> mean of x 
#>       5.5

Created on 2020-12-31 by the reprex package (v0.3.0)

@hadley hadley added the bug an unexpected problem or unintended behavior label Oct 30, 2023
@tslumley
Copy link

tslumley commented Apr 9, 2024

I came here to report this

@moodymudskipper
Copy link

moodymudskipper commented Feb 20, 2025

s3_dispatch() names and looks up the methods naively without looking at the global tables, I think this would solve most issues, right now as soon as standard conventions are not followed it seams to break.

here's another use case, rare but for one of the most used functions:

s3 <- getNamespaceInfo("utils", "S3methods")
s3[paste0(s3[,1], ".", s3[,2]) != s3[,3],]
#>      [,1]   [,2]         [,3]         [,4]
#> [1,] "head" "data.frame" "head.array" NA  
#> [2,] "tail" "data.frame" "tail.array" NA  
#> [3,] "tail" "table"      "tail.array" NA

sloop::s3_dispatch(head(iris))
#> => head.data.frame
#>  * head.default

And another example, not related to global tables here, but from the fact foo's name is used directly rather than finding the method in the body.

foo <- function() {
  UseMethod("head")
}
sloop::s3_dispatch(foo(letters))
#>    foo.character
#> => foo.default

This can be used, rather than converting the function symbol to character

names(utils::isS3stdGeneric(foo))
#> [1] "head"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants