• Stars
    star
    397
  • Rank 108,561 (Top 3 %)
  • Language
    R
  • License
    Other
  • Created over 4 years ago
  • Updated 28 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

S7: a new OO system for R

S7

Lifecycle: experimental R-CMD-check Codecov test coverage

The S7 package is a new OOP system designed to be a successor to S3 and S4. It has been designed and implemented collaboratively by the R Consortium Object-Oriented Programming Working Group, which includes representatives from R-Core, Bioconductor, the tidyverse/Posit, and the wider R community.

S7 is somewhat experimental; we are confident in the design but it has relatively little usage in the wild currently. We hope to avoid any major breaking changes, but reserve the right if we discover major problems.

Installation

The long-term goal of this project is to merge S7 in to base R. For now, you can experiment by installing it from CRAN:

install.packages("S7")

Usage

This section gives a very brief overview of the entirety of S7. Learn more of the basics in vignette("S7"), generics and methods in vignette("generics-methods"), classes and objects in vignette("classes-objects"), and compatibility with S3 and S4 in vignette("compatibility").

library(S7)

Classes and objects

S7 classes have a formal definition, which includes a list of properties and an optional validator. Use new_class() to define a class:

range <- new_class("range",
  properties = list(
    start = class_double,
    end = class_double
  ),
  validator = function(self) {
    if (length(self@start) != 1) {
      "@start must be length 1"
    } else if (length(self@end) != 1) {
      "@end must be length 1"
    } else if (self@end < self@start) {
      "@end must be greater than or equal to @start"
    }
  }
)

new_class() returns the class object, which is also the constructor you use to create instances of the class:

x <- range(start = 1, end = 10)
x
#> <range>
#>  @ start: num 1
#>  @ end  : num 10

Properties

The data possessed by an object is called its properties. Use @ to get and set properties:

x@start
#> [1] 1
x@end <- 20
x
#> <range>
#>  @ start: num 1
#>  @ end  : num 20

Properties are automatically validated against the type declared in new_class() (double in this case), and with the class validator:

x@end <- "x"
#> Error: <range>@end must be <double>, not <character>
x@end <- -1
#> Error: <range> object is invalid:
#> - @end must be greater than or equal to @start

Generics and methods

Like S3 and S4, S7 uses functional OOP where methods belong to generic functions, and method calls look like all other function calls: generic(object, arg2, arg3). This style is called functional because from the outside it looks like a regular function call, and internally the components are also functions.

Use new_generic() to create a new generic: the first argument is the generic name (used in error messages) and the second gives the arguments used for dispatch. The third, and optional argument, supplies the body of the generic. This is only needed if your generic has additional arguments that aren’t used for method dispatch.

inside <- new_generic("inside", "x")

Once you have a generic, you can define a method for a specific class with method<-:

# Add a method for our class
method(inside, range) <- function(x, y) {
  y >= x@start & y <= x@end
}

inside(x, c(0, 5, 10, 15))
#> [1] FALSE  TRUE  TRUE  TRUE

You can use method<- to register methods for base types on S7 generics, and S7 classes on S3 or S4 generics. See vignette("compatibility") for more details.

More Repositories

1

censusguide

84
star
2

submissions-pilot1-to-fda

eCTD package for pilot1 submission to FDA
XSLT
79
star
3

rtrs-wg

R Tables for Regulatory Submissions Working Group
HTML
62
star
4

IDEA-WG

R Community Diversity and Inclusion Working Group (RCDI-WG)
49
star
5

submissions-wg

R Submissions Working Group
HTML
48
star
6

r-repositories-wg

RC Working Group on Repositories
HTML
37
star
7

wishlist

A wishlist of idea from the ISC and community
29
star
8

submissions-pilot1

development repo for pilot1 submission to FDA
XSLT
28
star
9

isc-proposal

Boilerplate proposal for R Consortium ISC Proposals
Shell
26
star
10

R-Certification-WG

R Certification Working Group
HTML
25
star
11

submissions-pilot2-to-fda

eCTD package for pilot 2 submission
XSLT
23
star
12

r-collaboration

Open Collaboration, Data Registry, and Use Cases Developed by the R Community
23
star
13

asa-biop-swe-wg

TeX
21
star
14

submissions-pilot2

development repo for pilot2 submission to FDA
JavaScript
19
star
15

r-medicine-wg

R/Medicine Working Group
TeX
18
star
16

marshalling-wg

ISC Working Group 'Marshaling and Serialization in R' (anno May 2024)
HTML
15
star
17

RBusiness

Rbusiness
R
14
star
18

submissions-pilot4-webR

Development repository for Pilot 4 WebAssembly Shiny App
R
14
star
19

r-advisory-database

Advisory database for R packages published on cran.r-project.org or bioconductor.org
12
star
20

artwork

🎨 R Consortium Artwork, Logos, and License Guidelines
9
star
21

RMedicine_2024

JavaScript
9
star
22

submissions-pilot3-utilities

A simple R package used to test submission process to the FDA
R
7
star
23

submissions-pilot4

Submissions WG Pilot 4
Dockerfile
7
star
24

HTA-wg

HTML
6
star
25

pilot1-to-pmda-submission

PMDA submission pilot
HTML
5
star
26

project-analysis

Data-Driven Discovery and Tracking of R Consortium Activities
JavaScript
5
star
27

isc

R Consortium ISC meeting notes and collaboration
5
star
28

multilingual-documentation-wg

5
star
29

submissions-pilot4-webR-to-fda

XSLT
4
star
30

r-submission-site

(will switch to rconsortium template later)
CSS
4
star
31

submissions-pilot3

development repo for pilot 3
2
star
32

rugs-materials

This repository is for any presentations and material you might want to share from your R User Groups
2
star
33

RMedicine_website

JavaScript
2
star
34

rconsortium_website

JavaScript
1
star
35

covid19-wg

1
star
36

Code-Coverage-WG

1
star
37

pharma-wg-oversight-committee

R Pharma Oversight Committee
HTML
1
star
38

r-medicine

R in Medicine
CSS
1
star
39

r-jobs-board

Informal listing of R-related jobs for job seekers and job providers to be able to easily communicate
1
star