sloop v1.0.0

0

Monthly downloads

0th

Percentile

Helpers for 'OOP' in R

A collection of helper functions designed to help you to better understand object oriented programming in R, particularly using 'S3'.

Readme

sloop

CRAN
status Travis build
status Codecov test
coverage

The goal of sloop is to provide tools to help you interactively explore and understand object oriented programming in R, particularly with S3.

Please note that unlike other r-lib packages, sloop only works with R 3.3 and later.

Installation

You can install sloop from github with:

# install.packages("devtools")
devtools::install_github("r-lib/sloop")

Usage

library(sloop)

sloop provides a variety of tools for understanding how S3 works. The most useful is probably s3_dispatch(). Given a function call, it shows the set of methods that are considered, found, and actually called:

s3_dispatch(print(Sys.time()))
#> => print.POSIXct
#>    print.POSIXt
#>  * print.default

To the best of my ability it covers all the details of S3 method dispatch including group generics, internal generics, implicit classes, and use of NextMethod() (indiciated by <-):

# Implicit class
x <- matrix(1:6, nrow = 2)
s3_dispatch(print(x))
#>    print.matrix
#>    print.integer
#>    print.numeric
#> => print.default

# Internal generic 
length.numeric <- function(x) 10
s3_dispatch(length(x))
#>    length.matrix
#>    length.integer
#>  * length.numeric
#>    length.default
#> => length (internal)

s3_dispatch(length(structure(x, class = "numeric")))
#> => length.numeric
#>    length.default
#>  * length (internal)

# NextMethod
s3_dispatch(Sys.Date()[1])
#> => [.Date
#>    [.default
#> -> [ (internal)

# group generic + NextMethod()
s3_dispatch(sum(Sys.Date()))
#>    sum.Date
#>    sum.default
#> => Summary.Date
#>    Summary.default
#> -> sum (internal)

It also provides tools for determing what type of function or object you’re dealing with:

ftype(t)
#> [1] "S3"      "generic"
ftype(t.test)
#> [1] "S3"      "generic"
ftype(t.data.frame)
#> [1] "S3"     "method"

otype(1:10)
#> [1] "base"
otype(mtcars)
#> [1] "S3"
otype(R6::R6Class()$new())
#> [1] "R6"

And for retrieving the methods associated with a generic or class:

s3_methods_class("factor")
#> # A tibble: 27 x 4
#>    generic       class  visible source
#>    <chr>         <chr>  <lgl>   <chr> 
#>  1 [             factor TRUE    base  
#>  2 [[            factor TRUE    base  
#>  3 [[<-          factor TRUE    base  
#>  4 [<-           factor TRUE    base  
#>  5 all.equal     factor TRUE    base  
#>  6 as.character  factor TRUE    base  
#>  7 as.data.frame factor TRUE    base  
#>  8 as.Date       factor TRUE    base  
#>  9 as.list       factor TRUE    base  
#> 10 as.logical    factor TRUE    base  
#> # ... with 17 more rows

s3_methods_generic("summary")
#> # A tibble: 35 x 4
#>    generic class                 visible source             
#>    <chr>   <chr>                 <lgl>   <chr>              
#>  1 summary aov                   TRUE    stats              
#>  2 summary aovlist               FALSE   registered S3method
#>  3 summary aspell                FALSE   registered S3method
#>  4 summary check_packages_in_dir FALSE   registered S3method
#>  5 summary connection            TRUE    base               
#>  6 summary data.frame            TRUE    base               
#>  7 summary Date                  TRUE    base               
#>  8 summary default               TRUE    base               
#>  9 summary ecdf                  FALSE   registered S3method
#> 10 summary factor                TRUE    base               
#> # ... with 25 more rows

Functions in sloop

Name Description
s3_get_method Find S3 method from its name
s3_dispatch Illustrate S3 dispatch
otype Determine the type of an object
s3_class Compute the S3 class of an object
ftype Determine function type.
s3_methods_class List methods for a S3 or S4 generic (or class)
is_s3_generic Determine if a function is an S3 generic or S3 method.
No Results!

Last month downloads

Details

License GPL-3
URL https://github.com/r-lib/sloop
BugReports https://github.com/r-lib/sloop/issues
Encoding UTF-8
LazyData true
RoxygenNote 6.1.0
NeedsCompilation no
Packaged 2018-10-23 15:07:46 UTC; hadley
Repository CRAN
Date/Publication 2018-10-30 16:20:07 UTC

Include our badge in your README

[![Rdoc](http://www.rdocumentation.org/badges/version/sloop)](http://www.rdocumentation.org/packages/sloop)