An open API service indexing awesome lists of open source software.

https://github.com/mkearney/callmethod

🤙 Call Method for Developing Packages
https://github.com/mkearney/callmethod

devtools methods package-development r-methods r-package r-packages r-programming rstats

Last synced: about 1 month ago
JSON representation

🤙 Call Method for Developing Packages

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(callmethod)
```
# callmethod

> A methods package with some simpler syntax at the cost of being a less transferable

## Installation

You can install the released version of callmethod from Github with:

``` r
## install from Github
remotes::install_github("mkearney/callmethod")
```

## Examples

Define a method that's a wrapper around `base::rnorm()`:

```{r rnorm}
## define method
r_norm <- function(n, m = 0, sd = 1) call_method("r_norm")

## set default
r_norm.default <- function(...) rnorm(...)

## call method
r_norm(10, 3)
```

Define a method that generates random ID strings:

```{r random_string}
## define method
rstring <- function(n, collapse = "") call_method("random_string")

## define method default
random_string.default <- function(...) {
list(...)[[1]]
}

random_string.character <- function(...) {
dots <- list(...)
n <- nchar(dots[[1]])
collapse <- dots[[2]]
random_string.numeric(n, collapse)
}

## define method for numeric
random_string.numeric <- function(...) {
dots <- list(...)
n <- dots[[1]]
collapse <- dots[[2]]
fl <- sample(letters, 2, replace = TRUE)
paste(c(fl[1],
sample(c(rep(0:9, 3), letters, toupper(letters)), n - 2, replace = TRUE),
fl[2]), collapse = collapse)
}

## call random_string method
rstring(20)

rstring("thislong")
```