Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jakubsob/r6methods
Add Methods to R6 Class Definition
https://github.com/jakubsob/r6methods
Last synced: about 1 month ago
JSON representation
Add Methods to R6 Class Definition
- Host: GitHub
- URL: https://github.com/jakubsob/r6methods
- Owner: jakubsob
- License: other
- Created: 2021-02-27T22:01:44.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-16T19:36:42.000Z (almost 3 years ago)
- Last Synced: 2024-09-24T13:21:28.172Z (3 months ago)
- Language: R
- Size: 2.88 MB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - jakubsob/r6methods - Add Methods to R6 Class Definition (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# r6methods
[![R-CMD-check](https://github.com/jakubsob/r6methods/workflows/R-CMD-check/badge.svg)](https://github.com/jakubsob/r6methods/actions)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/r6methods)](https://cran.r-project.org/package=r6methods)
[![CRAN_time_from_release](https://www.r-pkg.org/badges/ago/r6methods)](https://cran.r-project.org/package=r6methods)
[![metacran downloads](https://cranlogs.r-pkg.org/badges/grand-total/r6methods)](https://cran.r-project.org/package=r6methods)
[![Codecov test coverage](https://codecov.io/gh/jakubsob/r6methods/branch/master/graph/badge.svg)](https://codecov.io/gh/jakubsob/r6methods?branch=master)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 the package from CRAN:
```r
install.packages("r6methods")
```or install development version from Github using:
```r
remotes::install_github("jakubsob/r6methods")
```## Basic usage
Core functionality comes with `make_methods` function.
```{r}
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:
```{r}
make_methods(Person, "private", "both")
```Or only getters:
```{r}
make_methods(Person, "private", "get", add_roxygen = FALSE)
```You can also create methods for fields of your liking, not only all of private/public:
```{r}
make_methods(Person, c("age", "secret1"), "get", add_roxygen = FALSE)
```## 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.**Insert R6 methods**
![insert methods](man/figures/README-insert_methods.gif)
**Insert R6 methods gadget**
![insert methods gadget](man/figures/README-insert_methods_gadget.gif)