MyNixOS website logo
Description

Make Methods for R6 Classes.

Generate boilerplate code for R6 classes. Given R6 class create getters and/or setters for selected class fields or use RStudio addins to insert methods straight into class definition.

r6methods

R-CMD-check License:MIT

Generate boilerplate code for R6 classes. Given R6 class create getters and/or setters for selected class fields or use RStudio addins to insert methods straight into class definition.

Installation

You can install development version from Github using:

remotes::install_github("jakubsob/r6methods")

Basic usage

Core functionality comes with make_methods function.

library(r6methods)
library(R6)

Person <- R6Class(
  "Person", 
  public = list(
    name = NULL,
    age = NA,
    initialize = function(name, age = NA) {
      self$name <- name
      self$age <- age
    },
    print = function(...) {
      cat("Person: \n")
      cat("  Name: ", self$name, "\n", sep = "")
      cat("  Age:  ", self$age, "\n", sep = "")
      invisible(self)
    }
  ),
  private = list(
    secret1 = NULL,
    secret2 = NULL
  )
)

Create getters and setters for private fields:

make_methods(Person, "private", "both")
#> #' @description Setter for secret1
#> set_secret1 = function(secret1) {
#>   private$secret1 <- secret1
#> },
#> #' @description Getter for secret1
#> get_secret1 = function() {
#>   private$secret1
#> },
#> #' @description Setter for secret2
#> set_secret2 = function(secret2) {
#>   private$secret2 <- secret2
#> },
#> #' @description Getter for secret2
#> get_secret2 = function() {
#>   private$secret2
#> }

Or only getters:

make_methods(Person, "private", "get", add_roxygen = FALSE)
#> get_secret1 = function() {
#>   private$secret1
#> },
#> get_secret2 = function() {
#>   private$secret2
#> }

You can also create methods for fields of your liking, not only all of private/public:

make_methods(Person, c("age", "secret1"), "get", add_roxygen = FALSE)
#> get_age = function() {
#>   self$age
#> },
#> get_secret1 = function() {
#>   private$secret1
#> }

RStudio addins

Four addins are supplied with the package. They are grouped into 2 families:

  • Generate: makes method strings and prints them to console to be copied to class definition.
  • Insert: makes method strings and inserts them into class definition.

Addins with gadget suffix open gadget in RStudio which allows user to have more control over the generated methods.

Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows