Skip to content
Back to skills

R Dev Oop

ASecurity

R OOP with R6, S3, S4. Use for object-oriented programming patterns.

  • 5 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 4, 2026
data

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned June 4, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill r-dev-oop --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of R Dev Oop?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for R Dev Oop
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-r-dev-oop/badge)](https://www.skillsdirectory.com/skills/leolin990405-r-dev-oop)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: r-dev-oop
description: R OOP with R6, S3, S4. Use for object-oriented programming patterns.
---

# R Object-Oriented Programming

OOP systems in R.

## R6 Classes

```r
library(R6)

# Define class
Person <- R6Class("Person",
  public = list(
    name = NULL,
    age = NULL,
    
    initialize = function(name, age = NA) {
      self$name <- name
      self$age <- age
    },
    
    greet = function() {
      cat("Hello, I'm", self$name, "\n")
    },
    
    set_age = function(age) {
      private$validate_age(age)
      self$age <- age
    }
  ),
  
  private = list(
    validate_age = function(age) {
      if (!is.numeric(age) || age < 0) {
        stop("Invalid age")
      }
    }
  ),
  
  active = list(
    birth_year = function() {
      as.integer(format(Sys.Date(), "%Y")) - self$age
    }
  )
)

# Use
p <- Person$new("Alice", 30)
p$greet()
p$birth_year

# Inheritance
Employee <- R6Class("Employee",
  inherit = Person,
  public = list(
    title = NULL,
    initialize = function(name, age, title) {
      super$initialize(name, age)
      self$title <- title
    }
  )
)
```

## S3 Classes

```r
# Constructor
new_person <- function(name, age) {
  structure(
    list(name = name, age = age),
    class = "person"
  )
}

# Validator
validate_person <- function(x) {
  if (!is.character(x$name)) stop("name must be character")
  if (!is.numeric(x$age)) stop("age must be numeric")
  x
}

# Helper
person <- function(name, age) {
  validate_person(new_person(name, age))
}

# Methods
print.person <- function(x, ...) {
  cat("Person:", x$name, "(", x$age, ")\n")
}

summary.person <- function(object, ...) {
  cat("Name:", object$name, "\n")
  cat("Age:", object$age, "\n")
}

# Generic
greet <- function(x) UseMethod("greet")
greet.person <- function(x) cat("Hello, I'm", x$name, "\n")
greet.default <- function(x) cat("Hello\n")
```

## S4 Classes

```r
library(methods)

# Define class
setClass("Person",
  slots = c(
    name = "character",
    age = "numeric"
  ),
  prototype = list(
    name = NA_character_,
    age = NA_real_
  )
)

# Constructor
Person <- function(name, age) {
  new("Person", name = name, age = age)
}

# Validity
setValidity("Person", function(object) {
  if (length(object@name) != 1) return("name must be length 1")
  if (object@age < 0) return("age must be positive")
  TRUE
})

# Methods
setGeneric("greet", function(x) standardGeneric("greet"))
setMethod("greet", "Person", function(x) {
  cat("Hello, I'm", x@name, "\n")
})

# Inheritance
setClass("Employee",
  contains = "Person",
  slots = c(title = "character")
)
```

Files in this skill

  • R6/SKILL.md1.8 KB
  • SKILL.md2.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.