-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
108 lines (79 loc) · 2.86 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# r6methods
<!-- badges: start -->
[![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)
<!-- badges: end -->
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)