knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-" )
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.
You can install sloop from github with:
# install.packages("pak") pak::pak("r-lib/sloop")
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()))
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()
(indicated by ->
):
# Implicit class x <- matrix(1:6, nrow = 2) s3_dispatch(print(x)) # Internal generic length.numeric <- function(x) 10 s3_dispatch(length(x)) s3_dispatch(length(structure(x, class = "numeric"))) # NextMethod s3_dispatch(Sys.Date()[1]) # group generic + NextMethod() s3_dispatch(sum(Sys.Date()))
It also provides tools for determing what type of function or object you're dealing with:
ftype(t) ftype(t.test) ftype(t.data.frame) otype(1:10) otype(mtcars) otype(R6::R6Class()$new())
And for retrieving the methods associated with a generic or class:
s3_methods_class("factor") s3_methods_generic("summary")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.