Skip to content

Bug in s3_dispatch #28

Description

@Tazinho

(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)

Activity

  1. added
    bugan unexpected problem or unintended behavior
    on Oct 30, 2023
  2. tslumley commented on Apr 9, 2024

    @tslumley

    I came here to report this

  3. moodymudskipper commented on Feb 20, 2025

    @moodymudskipper

    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"
    
  4. yudalang3 commented on Apr 22, 2025

    @yudalang3

    I also very confused, What is the meanings of the => and *.
    For I do not find any document to see this #37 @hadley

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions