├── .Rbuildignore ├── .gitignore ├── tests ├── testthat.R └── testthat │ ├── test-basic.R │ ├── test-accessing.R │ └── test-methods.R ├── docs ├── pkgdown.yml ├── link.svg ├── docsearch.js ├── pkgdown.js ├── articles │ ├── index.html │ └── Introduction.html ├── authors.html ├── pkgdown.css ├── reference │ ├── is.enum_value.html │ ├── get_enum_values.html │ ├── add_enum_methods.html │ ├── get_enum_method.html │ ├── methodize.html │ ├── apply_enum_method.html │ ├── print.enum_value.html │ ├── cash-.enum_type.html │ ├── sub-.enum_type.html │ ├── subset-.enum_type.html │ ├── cash-set-.enum_type.html │ ├── print.enum_type.html │ ├── as.enum_type.html │ ├── get_enum_index.html │ ├── enum_type.html │ ├── is.enum_type.html │ ├── sample_enum.html │ └── index.html ├── index.html └── docsearch.css ├── R ├── names.enum_type.R ├── length.enum_type.R ├── get_enum_values.R ├── print.enum_value.R ├── enum_list.R ├── apply_enum_method.R ├── summary.enum_type.R ├── print.enum_type.R ├── enum_value.R ├── get_enum_index.R ├── is.enum_value.R ├── get_enum_method.R ├── sample_enum.R ├── add_enum_methods.R ├── match_enum_value.R ├── accessing_enum_values.R ├── as.enum_type.R ├── is.enum_type.R └── enum_type.R ├── man ├── is.enum_value.Rd ├── get_enum_values.Rd ├── methodize.Rd ├── add_enum_methods.Rd ├── get_enum_method.Rd ├── print.enum_value.Rd ├── apply_enum_method.Rd ├── match_enum_value.Rd ├── cash-.enum_type.Rd ├── sub-.enum_type.Rd ├── print.enum_type.Rd ├── subset-.enum_type.Rd ├── cash-set-.enum_type.Rd ├── as.enum_type.Rd ├── get_enum_index.Rd ├── enum_type.Rd ├── is.enum_type.Rd └── sample_enum.Rd ├── .travis.yml ├── NAMESPACE ├── DESCRIPTION ├── README.md └── vignettes └── Introduction.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user/ 2 | *.Rproj 3 | .Rbuildignore 4 | .Rhistory 5 | .Rproj.user 6 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(enumeR) 3 | 4 | test_check("enumeR") 5 | -------------------------------------------------------------------------------- /docs/pkgdown.yml: -------------------------------------------------------------------------------- 1 | pandoc: 2.7.2 2 | pkgdown: 1.3.0 3 | pkgdown_sha: ~ 4 | articles: 5 | Introduction: Introduction.html 6 | 7 | -------------------------------------------------------------------------------- /R/names.enum_type.R: -------------------------------------------------------------------------------- 1 | names.enum_type <- function(enum_type) { 2 | stopifnot(is.enum_type(enum_type)) 3 | enum_type[["type_name"]] 4 | } 5 | -------------------------------------------------------------------------------- /R/length.enum_type.R: -------------------------------------------------------------------------------- 1 | length.enum_type <- function(enum_type) { 2 | stopifnot(is.enum_type(enum_type)) 3 | length(get_enum_values(enum_type)) 4 | } 5 | -------------------------------------------------------------------------------- /R/get_enum_values.R: -------------------------------------------------------------------------------- 1 | #' TEMPORARRY DOCUMENTATION 2 | #' 3 | #' @description TEMPORARY DOCUMENTATION 4 | #' 5 | #' @export 6 | 7 | get_enum_values <- function(enum_type) { 8 | stopifnot(is.enum_type(enum_type)) 9 | enum_type[["values"]] 10 | } 11 | -------------------------------------------------------------------------------- /R/print.enum_value.R: -------------------------------------------------------------------------------- 1 | #' TEMPORARRY DOCUMENTATION 2 | #' 3 | #' @description TEMPORARY DOCUMENTATION 4 | #' 5 | #' @export 6 | 7 | print.enum_value <- function(enum_value) { 8 | cat(get_enum_index(enum_value), " : ", enum_value$value_name, "\n") 9 | } 10 | -------------------------------------------------------------------------------- /R/enum_list.R: -------------------------------------------------------------------------------- 1 | enum_list <- function(list) { 2 | stopifnot(all(sapply(list, is.enum_value))) 3 | class(list) <- c(class(list), "enum_list") 4 | list 5 | } 6 | 7 | print.enum_list <- function(enum_list) { 8 | cat("List of enum values:\n") 9 | invisible(sapply(enum_list, print)) 10 | } 11 | -------------------------------------------------------------------------------- /man/is.enum_value.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/is.enum_value.R 3 | \name{is.enum_value} 4 | \alias{is.enum_value} 5 | \title{TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | is.enum_value(object) 8 | } 9 | \description{ 10 | TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /R/apply_enum_method.R: -------------------------------------------------------------------------------- 1 | #' TEMPORARRY DOCUMENTATION 2 | #' 3 | #' @description TEMPORARY DOCUMENTATION 4 | #' 5 | #' @export 6 | apply_enum_method <- function(enum_value, method, ...) { 7 | eval(enum_value$methods_env$methods_universal[[as.character(substitute(method))]])(...) 8 | } 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /man/get_enum_values.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/get_enum_values.R 3 | \name{get_enum_values} 4 | \alias{get_enum_values} 5 | \title{TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | get_enum_values(enum_type) 8 | } 9 | \description{ 10 | TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /man/methodize.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/add_enum_methods.R 3 | \name{methodize} 4 | \alias{methodize} 5 | \title{@title TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | methodize(enum_type, method) 8 | } 9 | \description{ 10 | @description TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /man/add_enum_methods.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/add_enum_methods.R 3 | \name{add_enum_methods} 4 | \alias{add_enum_methods} 5 | \title{TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | add_enum_methods(enum_type, ...) 8 | } 9 | \description{ 10 | TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /man/get_enum_method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/get_enum_method.R 3 | \name{get_enum_method} 4 | \alias{get_enum_method} 5 | \title{TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | get_enum_method(enum_value, method) 8 | } 9 | \description{ 10 | TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: r 2 | cache: packages 3 | 4 | r: 5 | - oldrel 6 | - release 7 | - devel 8 | 9 | r_packages: 10 | - covr 11 | 12 | warnings_are_errors: false 13 | 14 | notifications: 15 | email: 16 | on_success: change 17 | on_failure: change 18 | 19 | after_success: 20 | - Rscript -e 'covr::codecov()' 21 | -------------------------------------------------------------------------------- /man/print.enum_value.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/print.enum_value.R 3 | \name{print.enum_value} 4 | \alias{print.enum_value} 5 | \title{TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | \method{print}{enum_value}(enum_value) 8 | } 9 | \description{ 10 | TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /man/apply_enum_method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/apply_enum_method.R 3 | \name{apply_enum_method} 4 | \alias{apply_enum_method} 5 | \title{TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | apply_enum_method(enum_value, method, ...) 8 | } 9 | \description{ 10 | TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /man/match_enum_value.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/match_enum_value.R 3 | \name{match_enum_value} 4 | \alias{match_enum_value} 5 | \title{TEMPORARRY DOCUMENTATION} 6 | \usage{ 7 | match_enum_value(x, table, nomatch = NA_integer_, incomparables = NULL) 8 | } 9 | \description{ 10 | TEMPORARY DOCUMENTATION 11 | } 12 | -------------------------------------------------------------------------------- /R/summary.enum_type.R: -------------------------------------------------------------------------------- 1 | summary.enum_type <- function(object, ...) { 2 | if (!is.enum_type(object)) { 3 | return(summary.default(object, ...)) 4 | } 5 | value <- array("", c(2L, 1L), list(c("Values", "Methods"), "Length")) 6 | value[1L, 1L] <- length(get_enum_values(object)) 7 | value[2L, 1L] <- length(object[["methods"]]) 8 | class(value) <- c("table") 9 | value 10 | } 11 | -------------------------------------------------------------------------------- /R/print.enum_type.R: -------------------------------------------------------------------------------- 1 | #' Print enum_type 2 | #' 3 | #' @description Pretty prints enum_type object. Overloads default print. 4 | #' 5 | #' @param enum_type Object of class enum_type 6 | #' 7 | #' @export 8 | 9 | print.enum_type <- function(enum_type) { 10 | cat("Enum type: ", enum_type[["type_name"]], 11 | "\n\nValues:\n") 12 | invisible(sapply(enum_type[["values"]], print)) 13 | } 14 | -------------------------------------------------------------------------------- /R/enum_value.R: -------------------------------------------------------------------------------- 1 | ##not exported 2 | 3 | enum_value <- function(enum_type, methods_env, value_name, index, fields = list()) { 4 | value <- c(list(type_name = enum_type, 5 | methods_env = methods_env, 6 | value_name = value_name, 7 | index = index), 8 | fields) 9 | class(value) <- "enum_value" 10 | value 11 | } 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /man/cash-.enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessing_enum_values.R 3 | \name{$.enum_type} 4 | \alias{$.enum_type} 5 | \title{Get subset of values of enum_type} 6 | \usage{ 7 | \method{$}{enum_type}(enum_type, value) 8 | } 9 | \arguments{ 10 | \item{enum_type}{enum_type} 11 | 12 | \item{index}{index} 13 | } 14 | \description{ 15 | placeholder 16 | } 17 | -------------------------------------------------------------------------------- /man/sub-.enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessing_enum_values.R 3 | \name{[.enum_type} 4 | \alias{[.enum_type} 5 | \title{Get subset of values of enum_type} 6 | \usage{ 7 | \method{[}{enum_type}(enum_type, index) 8 | } 9 | \arguments{ 10 | \item{enum_type}{enum_type} 11 | 12 | \item{index}{index} 13 | } 14 | \description{ 15 | placeholder 16 | } 17 | -------------------------------------------------------------------------------- /man/print.enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/print.enum_type.R 3 | \name{print.enum_type} 4 | \alias{print.enum_type} 5 | \title{Print enum_type} 6 | \usage{ 7 | \method{print}{enum_type}(enum_type) 8 | } 9 | \arguments{ 10 | \item{enum_type}{Object of class enum_type} 11 | } 12 | \description{ 13 | Pretty prints enum_type object. Overloads default print. 14 | } 15 | -------------------------------------------------------------------------------- /man/subset-.enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessing_enum_values.R 3 | \name{[<-.enum_type} 4 | \alias{[<-.enum_type} 5 | \title{Get subset of values of enum_type} 6 | \usage{ 7 | \method{[}{enum_type}(enum_type, index) <- value 8 | } 9 | \arguments{ 10 | \item{enum_type}{enum_type} 11 | 12 | \item{index}{index} 13 | } 14 | \description{ 15 | placeholder 16 | } 17 | -------------------------------------------------------------------------------- /man/cash-set-.enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessing_enum_values.R 3 | \name{$<-.enum_type} 4 | \alias{$<-.enum_type} 5 | \title{Get subset of values of enum_type} 6 | \usage{ 7 | \method{$}{enum_type}(enum_type, index) <- value 8 | } 9 | \arguments{ 10 | \item{enum_type}{enum_type} 11 | 12 | \item{index}{index} 13 | } 14 | \description{ 15 | placeholder 16 | } 17 | -------------------------------------------------------------------------------- /man/as.enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/as.enum_type.R 3 | \name{as.enum_type} 4 | \alias{as.enum_type} 5 | \title{Coerce to an enum type} 6 | \usage{ 7 | as.enum_type(x) 8 | } 9 | \arguments{ 10 | \item{x}{any R object} 11 | } 12 | \value{ 13 | Object of class \code{\link{enum_type}} 14 | } 15 | \description{ 16 | Functions to check if an object is an enum type, or coerce it if possible. 17 | } 18 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method("$",enum_type) 4 | S3method("$<-",enum_type) 5 | S3method("[",enum_type) 6 | S3method("[<-",enum_type) 7 | S3method(print,enum_type) 8 | S3method(print,enum_value) 9 | export(add_enum_methods) 10 | export(apply_enum_method) 11 | export(as.enum_type) 12 | export(enum_type) 13 | export(get_enum_index) 14 | export(get_enum_method) 15 | export(get_enum_values) 16 | export(is.enum_type) 17 | export(is.enum_value) 18 | export(match_enum_value) 19 | export(sample_enum) 20 | -------------------------------------------------------------------------------- /man/get_enum_index.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/get_enum_index.R 3 | \name{get_enum_index} 4 | \alias{get_enum_index} 5 | \title{Get Enum Index} 6 | \usage{ 7 | get_enum_index(enum_value) 8 | } 9 | \arguments{ 10 | \item{enum_value}{object of class `enum_value` or list of such objects with the same `enum_type`} 11 | } 12 | \value{ 13 | integer index of `enum_value` or named vector of integers coresponding to values in list 14 | } 15 | \description{ 16 | Extracts `index` attribute value of passed `enum_value`. 17 | } 18 | -------------------------------------------------------------------------------- /R/get_enum_index.R: -------------------------------------------------------------------------------- 1 | #' Get Enum Index 2 | #' 3 | #' @description Extracts `index` attribute value of passed `enum_value`. 4 | #' 5 | #' @param enum_value object of class `enum_value` or list of such objects with the same `enum_type` 6 | #' 7 | #' @return integer index of `enum_value` or named vector of integers coresponding to values in list 8 | #' 9 | #' @export 10 | 11 | get_enum_index <- function(enum_value) { 12 | stopifnot(is.enum_value(enum_value)) 13 | if(is.enum_value(enum_value)) { 14 | enum_value$index 15 | } else { 16 | sapply(enum_value, function(value) value$index) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /man/enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/enum_type.R 3 | \name{enum_type} 4 | \alias{enum_type} 5 | \title{Enum type} 6 | \usage{ 7 | enum_type(type_name, values_names, ...) 8 | } 9 | \arguments{ 10 | \item{type_name}{Character vector of length 1. Name of the type. Type name will be referenced by this value} 11 | 12 | \item{...}{Optional other fields that are used during constructing enum_values} 13 | 14 | \item{value_names}{Character vector of positive length. Possible names of enum_values (instances)} 15 | } 16 | \description{ 17 | Creates new enum type. 18 | } 19 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: enumeR 2 | Title: Enum types support for R 3 | Version: 0.0.0.9000 4 | Authors@R: c( 5 | person("Dominik", 6 | "Rafacz", 7 | role = c("aut", "cre"), 8 | email = "d.rafacz@student.mini.pw.edu.pl"), 9 | person("Mateusz", 10 | "Bąkała", 11 | role = "aut", 12 | email = "m.bakala2@student.mini.pw.edu.pl") 13 | ) 14 | Description: This package provides tools for creating and using enumeration types (enums). 15 | License: GPL 16 | Encoding: UTF-8 17 | LazyData: true 18 | RoxygenNote: 6.1.1 19 | Suggests: 20 | knitr, 21 | rmarkdown, 22 | testthat (>= 2.1.0) 23 | VignetteBuilder: knitr 24 | -------------------------------------------------------------------------------- /R/is.enum_value.R: -------------------------------------------------------------------------------- 1 | #' TEMPORARRY DOCUMENTATION 2 | #' 3 | #' @description TEMPORARY DOCUMENTATION 4 | #' 5 | #' @export 6 | 7 | is.enum_value <- function(object) { 8 | if(class(object) == "enum_value" && 9 | !any(is.null(object$type_name), 10 | is.null(object$value_name), 11 | is.null(object$index), 12 | is.null(object$methods_env)) && 13 | all(is.character(object$type_name), 14 | is.character(object$value_name), 15 | is.integer(object$index), 16 | is.environment(object$methods_env)) && 17 | all(length(object$type_name) == 1, 18 | length(object$value_name) == 1, 19 | length(object$index == 1))){ 20 | return(TRUE) 21 | } 22 | return(FALSE) 23 | } 24 | -------------------------------------------------------------------------------- /man/is.enum_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/is.enum_type.R 3 | \name{is.enum_type} 4 | \alias{is.enum_type} 5 | \title{Test if the object is an enum type} 6 | \usage{ 7 | is.enum_type(object) 8 | } 9 | \value{ 10 | \code{TRUE} if the object passed all tests described above, \code{FALSE} otherwise. 11 | } 12 | \description{ 13 | This function checks class of passed object. If vector of classes contains 14 | \code{"enum_type"}, then checks if all necessary fields are not \code{null}, namely 15 | \code{type_name} and \code{values}, and whether \code{type_name} is a singular 16 | character vector. Next, checks if \code{fields_names} is a character vector or \code{null}. 17 | Lastly, all values need to be unique and proper \code{enum_value} objects. 18 | } 19 | -------------------------------------------------------------------------------- /R/get_enum_method.R: -------------------------------------------------------------------------------- 1 | #' TEMPORARRY DOCUMENTATION 2 | #' 3 | #' @description TEMPORARY DOCUMENTATION 4 | #' 5 | #' @export 6 | 7 | get_enum_method <- function(enum_value, method) { 8 | method_name <- as.character(substitute(method)) 9 | methods_names <- names(enum_value$methods_env$methods_universal) 10 | if(!method_name %in% methods_names) { 11 | stop(method_name, " is not a proper name of any of ", enum_value$type_name, " methods!") 12 | } 13 | method <- enum_value$methods_env$methods_universal[[method_name]] 14 | variables <- names(enum_value)[-c(1,2)] 15 | variables_w_d <- paste(".", variables, sep = "") 16 | inds <- match(variables_w_d, names(formals(method))) 17 | formals(method)[inds] <- enum_value[-c(1,2)] 18 | ret <- function(...) { 19 | method(...) 20 | } 21 | #formals(ret) <- formals(method)[-inds] 22 | ret 23 | } 24 | -------------------------------------------------------------------------------- /docs/link.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | -------------------------------------------------------------------------------- /man/sample_enum.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/sample_enum.R 3 | \name{sample_enum} 4 | \alias{sample_enum} 5 | \title{Sample n values from an enum type} 6 | \usage{ 7 | sample_enum(enum_type, size, replace = FALSE, weight = NULL) 8 | } 9 | \arguments{ 10 | \item{enum_type}{Object that passes [is.enum_type()] check.} 11 | 12 | \item{size}{A non-negative integer giving number of items to choose.} 13 | 14 | \item{replace}{Should sampling be with replacement?} 15 | 16 | \item{weight}{Sampling weights. This must evaluate to a vector of non-negative numbers the same 17 | length as the input. Weights are automatically standardised to sum to 1.} 18 | } 19 | \value{ 20 | A list of enum values of length \code{size} drawn from \code{enum_type}'s values. 21 | } 22 | \description{ 23 | This is a wrapper around [sample()] to make selecting random enum values easier. 24 | } 25 | -------------------------------------------------------------------------------- /R/sample_enum.R: -------------------------------------------------------------------------------- 1 | #' Sample n values from an enum type 2 | #' 3 | #' @description This is a wrapper around [sample()] to make selecting random enum values easier. 4 | #' 5 | #' @param enum_type Object that passes [is.enum_type()] check. 6 | #' @param size A non-negative integer giving number of items to choose. 7 | #' @param replace Should sampling be with replacement? 8 | #' @param weight Sampling weights. This must evaluate to a vector of non-negative numbers the same 9 | #' length as the input. Weights are automatically standardised to sum to 1. 10 | #' 11 | #' @return A list of enum values of length \code{size} drawn from \code{enum_type}'s values. 12 | #' 13 | #' @export 14 | 15 | sample_enum <- function(enum_type, size, replace = FALSE, weight = NULL) { 16 | stopifnot(is.enum_type(enum_type)) 17 | if (!is.null(weight)) { 18 | weight <- weight/sum(weight) 19 | } 20 | enum_list(sample(enum_type[["values"]], size, replace, weight)) 21 | } 22 | -------------------------------------------------------------------------------- /R/add_enum_methods.R: -------------------------------------------------------------------------------- 1 | #' TEMPORARRY DOCUMENTATION 2 | #' 3 | #' @description TEMPORARY DOCUMENTATION 4 | #' 5 | #' @export 6 | add_enum_methods <- function(enum_type, ...) { 7 | added_methods <- match.call(expand.dots = FALSE)$`...` 8 | n <- length(added_methods) 9 | invisible( 10 | lapply(1:n, function(index) { 11 | enum_type[["methods_env"]]$methods_universal[[names(added_methods)[index]]] <- methodize(enum_type, eval(added_methods[[index]])) 12 | }) 13 | ) 14 | } 15 | 16 | 17 | #' @title TEMPORARRY DOCUMENTATION 18 | #' 19 | #' @description TEMPORARY DOCUMENTATION 20 | methodize <- function(enum_type, method) { 21 | variables <- paste(".", c("index", enum_type[["fields_names"]]), sep = "") 22 | if(is.null(formals(method))) { 23 | formals(method) <- alist(.value_name = ) 24 | } else { 25 | formals(method)[[".value_name"]] <- alist(x= )$x 26 | } 27 | for(var in variables) { 28 | formals(method)[[var]] <- alist(x = )$x 29 | } 30 | method 31 | } 32 | -------------------------------------------------------------------------------- /R/match_enum_value.R: -------------------------------------------------------------------------------- 1 | #' TEMPORARRY DOCUMENTATION 2 | #' 3 | #' @description TEMPORARY DOCUMENTATION 4 | #' 5 | #' @export 6 | match_enum_value <- function(x, table, nomatch = NA_integer_, incomparables = NULL) { 7 | stopifnot(is.enum_value(x) || all(sapply(x, is.enum_value))) 8 | if(is.enum_type(table)) { 9 | table <- get_enum_values(table) 10 | } else if (!all(sapply(table, is.enum_value))) { 11 | stop("table should be list of enum_values or enum_type") 12 | } 13 | if(is.enum_value(x)) { 14 | matched <- FALSE 15 | i <- 1 16 | while(!matched && i <= length(table)) { 17 | if (identical(x, table[[i]])) { 18 | matched <- TRUE 19 | } 20 | i <- i +1 21 | } 22 | ifelse(matched, i-1, NA_integer_) 23 | } else { 24 | sapply(x, function(val) { 25 | matched <- FALSE 26 | i <- 1 27 | while(!matched && i <= length(table)) { 28 | if (identical(val, table[[i]])) { 29 | matched <- TRUE 30 | } 31 | i <- i +1 32 | } 33 | ifelse(matched, i-1, NA_integer_) 34 | }) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # enumeR 2 | 3 | [](https://travis-ci.org/DominikRafacz/enumeR) 4 | 5 | [](https://codecov.io/gh/DominikRafacz/enumeR) 6 | 7 | [](https://github.com/emersion/stability-badges#experimental) 8 | 9 | 10 | 11 | This package provides *enum_type* and *enum* classes, which allows to create and instantinize enumerable types in **R** 12 | 13 | It is in it's early development and authors don't grat any assurance that function names, parameters or functionalities won't change in future. 14 | 15 | # Installation 16 | 17 | To install this package you need to use `devtools` library, which can be installed with: 18 | ``` 19 | install.packages("devtools") 20 | ``` 21 | 22 | If you have already installed `devtools`, you need to run following code to install this package: 23 | 24 | ``` 25 | devtools::install_github("DominikRafacz/enumeR") 26 | ``` 27 | -------------------------------------------------------------------------------- /R/accessing_enum_values.R: -------------------------------------------------------------------------------- 1 | #' Get subset of values of enum_type 2 | #' 3 | #' @description placeholder 4 | #' 5 | #' @param enum_type enum_type 6 | #' @param index index 7 | #' 8 | #' @export 9 | "[.enum_type" <- function(enum_type, index) { 10 | enum_type[["values"]][index] 11 | } 12 | 13 | #' Get subset of values of enum_type 14 | #' 15 | #' @description placeholder 16 | #' 17 | #' @param enum_type enum_type 18 | #' @param index index 19 | #' 20 | #' @export 21 | "[<-.enum_type" <- function(enum_type, index, value) { 22 | stop("You cannot modify enum_type values!") 23 | } 24 | 25 | #' Get subset of values of enum_type 26 | #' 27 | #' @description placeholder 28 | #' 29 | #' @param enum_type enum_type 30 | #' @param index index 31 | #' 32 | #' @export 33 | "$.enum_type" <- function(enum_type, value) { 34 | enum_type[["values"]][[value]] 35 | } 36 | 37 | #' Get subset of values of enum_type 38 | #' 39 | #' @description placeholder 40 | #' 41 | #' @param enum_type enum_type 42 | #' @param index index 43 | #' 44 | #' @export 45 | "$<-.enum_type" <- function(enum_type, index, value) { 46 | stop("You cannot modify enum_type values!") 47 | } 48 | -------------------------------------------------------------------------------- /R/as.enum_type.R: -------------------------------------------------------------------------------- 1 | #' Coerce to an enum type 2 | #' 3 | #' @description Functions to check if an object is an enum type, or coerce it if possible. 4 | #' 5 | #' @param x any R object 6 | #' 7 | #' @return Object of class \code{\link{enum_type}} 8 | #' 9 | #' @export 10 | 11 | as.enum_type <- function(x) { 12 | stopifnot(!is.null(x), length(x) > 0) 13 | UseMethod("as.enum_type") 14 | } 15 | 16 | as.enum_type.default <- function(x) { 17 | stop(gettextf("cannot coerce class %s to an enum_type", sQuote(deparse(class(x))[1L])), 18 | domain = NA) 19 | } 20 | 21 | as.enum_type.list <- function(x) { 22 | stopifnot(length(x) > 0) 23 | 24 | type_name <- x$type_name 25 | type_name_ <- "type_name" 26 | if (is.null(type_name)) { 27 | type_name <- x$name 28 | type_name_ <- "name" 29 | if (is.null(type_name)) { 30 | type_name <- deparse(substitute(x)) 31 | type_name_ <- "" 32 | } 33 | } 34 | 35 | values <- x$values 36 | if (is.null(values)) { 37 | values <- x[[names(x)[which(names(x)[1] != type_name_)]]] 38 | } 39 | 40 | enum_type(type_name, values) 41 | } 42 | 43 | as.enum_type.character <- function(x) { 44 | stopifnot(length(x) > 0) 45 | if (length(unique(x)) != length(x)) { 46 | message("Warning: Vector has non-unique values. Repeated ones will be dropped") 47 | } 48 | enum_type(deparse(substitute(x)), unique(x)) 49 | } 50 | -------------------------------------------------------------------------------- /R/is.enum_type.R: -------------------------------------------------------------------------------- 1 | #' Test if the object is an enum type 2 | #' 3 | #' @description This function checks class of passed object. If vector of classes contains 4 | #' \code{"enum_type"}, then checks if all necessary fields are not \code{null}, namely 5 | #' \code{type_name} and \code{values}, and whether \code{type_name} is a singular 6 | #' character vector. Next, checks if \code{fields_names} is a character vector or \code{null}. 7 | #' Lastly, all values need to be unique and proper \code{enum_value} objects. 8 | #' 9 | #' @return \code{TRUE} if the object passed all tests described above, \code{FALSE} otherwise. 10 | #' 11 | #' @export 12 | 13 | is.enum_type <- function(object) { 14 | if("enum_type" %in% class(object) && 15 | !any(is.null(object[["type_name"]]), 16 | is.null(object[["values"]]), 17 | is.null(object[["methods_env"]])) && 18 | is.character(object[["type_name"]]) && 19 | is.environment(object[["methods_env"]]) && 20 | length(object[["type_name"]]) == 1 && 21 | (is.null(object[["fields_names"]]) || 22 | is.character(object[["fields_names"]]))) { 23 | values <- object[["values"]] 24 | n <- length(values) 25 | if (length(unique(values)) == n && 26 | all(unlist(lapply(values, is.enum_value)))) { 27 | #TO DO: add more tests, such as checking if all values have all fields 28 | return(TRUE) 29 | } 30 | } 31 | return(FALSE) 32 | } 33 | -------------------------------------------------------------------------------- /tests/testthat/test-basic.R: -------------------------------------------------------------------------------- 1 | test_that("enum_values are created properly", { 2 | temp <- new.env() 3 | object <- enum_value("animal", temp, "COW", 5L, list(voice = "mooo!", color = c("red", "white"))) 4 | reference <- list(type_name = "animal", 5 | methods_env = temp, 6 | value_name = "COW", 7 | index = 5, 8 | voice = "mooo!", 9 | color = c("red", "white")) 10 | class(reference) <- "enum_value" 11 | expect_equal(object, reference) 12 | }) 13 | 14 | test_that("enum_types with no fields are created properly", { 15 | object <- enum_type("animal", 16 | c("DOG", "CAT", "COW")) 17 | temp <- object[["methods_env"]] 18 | reference <- list(type_name = "animal", 19 | values_names = c("DOG", "CAT", "COW"), 20 | fields_names = NULL, 21 | values = list(DOG = enum_value("animal", temp, "DOG", 1L), 22 | CAT = enum_value("animal", temp, "CAT", 2L), 23 | COW = enum_value("animal", temp, "COW", 3L)), 24 | methods_env = temp) 25 | class(reference$values) <- c("list", "enum_list") 26 | class(reference) <- "enum_type" 27 | #length.enum_type <- base::length 28 | expect_equal(object, reference) 29 | }) 30 | 31 | test_that("enum_values are recoginized as enum_values", { 32 | object <- enum_value("animal", new.env(), "COW", 5L, list(voice = "mooo!", color = c("red", "white"))) 33 | expect_true(is.enum_value(object)) 34 | }) 35 | 36 | test_that("enum_types are recoginized as enum_types", { 37 | object <- enum_type("animal", 38 | c("DOG", "CAT", "COW")) 39 | expect_true(is.enum_type(object)) 40 | }) 41 | -------------------------------------------------------------------------------- /R/enum_type.R: -------------------------------------------------------------------------------- 1 | #' Enum type 2 | #' 3 | #' @description Creates new enum type. 4 | #' 5 | #' @param type_name Character vector of length 1. Name of the type. Type name will be referenced by this value 6 | #' @param value_names Character vector of positive length. Possible names of enum_values (instances) 7 | #' @param ... Optional other fields that are used during constructing enum_values 8 | #' 9 | #' @export 10 | 11 | enum_type <- function(type_name, values_names, ...) { 12 | fields <- as.list(match.call(expand.dots = FALSE)$`...`) 13 | fields <- lapply(fields, eval) 14 | n <- length(values_names) 15 | 16 | stopifnot(is.character(type_name)) 17 | stopifnot(length(type_name) == 1) 18 | stopifnot(all(unlist(lapply(fields, function(field) length(field) == n)))) 19 | stopifnot(is.character(values_names)) 20 | stopifnot(length(unique(values_names)) == n) 21 | 22 | if(length(fields) == 0) { 23 | fields_names <- NULL 24 | } else { 25 | fields_names <- names(fields) 26 | } 27 | 28 | methods_env <- new.env() 29 | methods_env$methods_universal <- list() 30 | methods_env$methods_switch <- list() 31 | 32 | new_type <- list(type_name = type_name, 33 | values_names = values_names, 34 | fields_names = fields_names, 35 | values = lapply(1L:n, 36 | function(index) enum_value(enum_type = type_name, 37 | methods_env = methods_env, 38 | value_name = values_names[index], 39 | index = index, 40 | lapply(fields, function(field) field[[index]]))), 41 | methods_env = methods_env) 42 | names(new_type$values) <- values_names 43 | new_type$values <- enum_list(new_type$values) 44 | class(new_type) <- "enum_type" 45 | new_type 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /vignettes/Introduction.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Introduction} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | This vignette will introduce you to basic functionalities of `enumeR` package. 11 | 12 | 13 | ```{r, include = FALSE} 14 | knitr::opts_chunk$set( 15 | collapse = TRUE, 16 | comment = "#>" 17 | ) 18 | ``` 19 | 20 | ```{r setup} 21 | library(enumeR) 22 | ``` 23 | 24 | First we create some `enum_type` object. It represents a class with finite number of possible values - similliar to enums from other languages like `C++` or `Java`. During construction of an object besides names of values you can specify values of own fields that will be kept inside values. For example: 25 | 26 | ```{r example} 27 | season_of_year <- enum_type("season_of_year", 28 | c("SPRING", "SUMMER", "AUTUMN", "WINTER"), 29 | min_temperature = c(-13, 7, -6, -20), 30 | max_temperature = c(20, 32, 18, 12)) 31 | 32 | print(season_of_year) 33 | ``` 34 | 35 | Now you can access values of this type: 36 | 37 | ```{r access} 38 | my_favourite_seasons <- season_of_year[c(1,4)] 39 | my_hated_seasons <- season_of_year[c("SUMMER","AUTUMN")] 40 | holiday_season <- season_of_year$SUMMER 41 | ``` 42 | 43 | ... but cannot modify it: 44 | ```{r modif, error=TRUE} 45 | season_of_year$SUMMER <- "something" 46 | ``` 47 | 48 | You can also choose a random subset: 49 | ```{r sample} 50 | sample_seasons <- sample_enum(season_of_year) 51 | ``` 52 | 53 | Objects have thier own classes, which can be checked: 54 | ```{r class} 55 | class(season_of_year) 56 | class(holiday_season) 57 | is.enum_type(season_of_year) 58 | is.enum_value(holiday_season) 59 | ``` 60 | 61 | You can add methods to `enum_type` which will be accessing its fields. You can access these fields using their names with dot before it. Thus it's not recommended to add fields which names begin with dot. 62 | ```{r methods} 63 | add_enum_methods(season_of_year, 64 | temperature_diff = function() { 65 | .max_temperature - .min_temperature 66 | }) 67 | 68 | get_enum_method(holiday_season, temperature_diff)() 69 | 70 | ``` 71 | -------------------------------------------------------------------------------- /docs/docsearch.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | // register a handler to move the focus to the search bar 4 | // upon pressing shift + "/" (i.e. "?") 5 | $(document).on('keydown', function(e) { 6 | if (e.shiftKey && e.keyCode == 191) { 7 | e.preventDefault(); 8 | $("#search-input").focus(); 9 | } 10 | }); 11 | 12 | $(document).ready(function() { 13 | // do keyword highlighting 14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */ 15 | var mark = function() { 16 | 17 | var referrer = document.URL ; 18 | var paramKey = "q" ; 19 | 20 | if (referrer.indexOf("?") !== -1) { 21 | var qs = referrer.substr(referrer.indexOf('?') + 1); 22 | var qs_noanchor = qs.split('#')[0]; 23 | var qsa = qs_noanchor.split('&'); 24 | var keyword = ""; 25 | 26 | for (var i = 0; i < qsa.length; i++) { 27 | var currentParam = qsa[i].split('='); 28 | 29 | if (currentParam.length !== 2) { 30 | continue; 31 | } 32 | 33 | if (currentParam[0] == paramKey) { 34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20")); 35 | } 36 | } 37 | 38 | if (keyword !== "") { 39 | $(".contents").unmark({ 40 | done: function() { 41 | $(".contents").mark(keyword); 42 | } 43 | }); 44 | } 45 | } 46 | }; 47 | 48 | mark(); 49 | }); 50 | }); 51 | 52 | /* Search term highlighting ------------------------------*/ 53 | 54 | function matchedWords(hit) { 55 | var words = []; 56 | 57 | var hierarchy = hit._highlightResult.hierarchy; 58 | // loop to fetch from lvl0, lvl1, etc. 59 | for (var idx in hierarchy) { 60 | words = words.concat(hierarchy[idx].matchedWords); 61 | } 62 | 63 | var content = hit._highlightResult.content; 64 | if (content) { 65 | words = words.concat(content.matchedWords); 66 | } 67 | 68 | // return unique words 69 | var words_uniq = [...new Set(words)]; 70 | return words_uniq; 71 | } 72 | 73 | function updateHitURL(hit) { 74 | 75 | var words = matchedWords(hit); 76 | var url = ""; 77 | 78 | if (hit.anchor) { 79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor; 80 | } else { 81 | url = hit.url + '?q=' + escape(words.join(" ")); 82 | } 83 | 84 | return url; 85 | } 86 | -------------------------------------------------------------------------------- /tests/testthat/test-accessing.R: -------------------------------------------------------------------------------- 1 | context("Accessing enum_values") 2 | 3 | simple_enum <- enum_type("color", 4 | c("RED", "GREEN", "BLUE"), 5 | hex_value = c("#FF0000", "#00FF00", "#0000FF")) 6 | 7 | red <- list(type_name = "color", 8 | methods_env = simple_enum[["methods_env"]], 9 | value_name = "RED", 10 | index = 1, 11 | hex_value = "#FF0000") 12 | class(red) <- "enum_value" 13 | 14 | green <- list(type_name = "color", 15 | methods_env = simple_enum[["methods_env"]], 16 | value_name = "GREEN", 17 | index = 2, 18 | hex_value = "#00FF00") 19 | class(green) <- "enum_value" 20 | 21 | test_that("enum_values are accessed properly with '$'", { 22 | expect_true(is.enum_value(simple_enum$RED)) 23 | expect_true(is.enum_value(simple_enum$GREEN)) 24 | expect_true(is.enum_value(simple_enum$BLUE)) 25 | expect_equal(simple_enum[["type_name"]], simple_enum$RED[["type_name"]]) 26 | expect_equal(simple_enum[["type_name"]], simple_enum$GREEN[["type_name"]]) 27 | expect_equal(simple_enum[["type_name"]], simple_enum$BLUE[["type_name"]]) 28 | expect_equal(simple_enum$RED, red) 29 | expect_equal(simple_enum$GREEN, green) 30 | }) 31 | 32 | test_that("it's not possible to modify enum_type with '$<-'", { 33 | expect_error(simple_enum$RED <- "anything", "You cannot modify enum_type values!" ) 34 | expect_error(simple_enum$GREEN <- "anything", "You cannot modify enum_type values!" ) 35 | expect_error(simple_enum$BLUE <- "anything", "You cannot modify enum_type values!" ) 36 | }) 37 | 38 | test_that("enum_values are subsetted properly with '['", { 39 | object <- simple_enum[1] 40 | expect_equal(object, list(RED = red)) 41 | object <- simple_enum[c(1,2,1)] 42 | reference <- list(RED = red, GREEN = green, RED = red) 43 | expect_equal(object, reference) 44 | object <- simple_enum[c("RED", "GREEN", "RED")] 45 | expect_equal(object, reference) 46 | }) 47 | 48 | test_that("it's not possible to modify enum_type with '[<-'", { 49 | expect_error(simple_enum[1] <- "anything", "You cannot modify enum_type values!" ) 50 | expect_error(simple_enum["GREEN"] <- "anything", "You cannot modify enum_type values!" ) 51 | expect_error(simple_enum[c(1,2,3,1)] <- "anything", "You cannot modify enum_type values!" ) 52 | expect_error(simple_enum[c("RED", "BLUE", "RED")] <- "anything", "You cannot modify enum_type values!" ) 53 | }) 54 | -------------------------------------------------------------------------------- /tests/testthat/test-methods.R: -------------------------------------------------------------------------------- 1 | context("Adding and accessing enum methods") 2 | 3 | color_enum <- enum_type("color_enum", 4 | c("QUEEN_BLUE", "DARK_PURPLE", "PINK_RASPBERRY"), 5 | red = c(67, 47, 156), 6 | green = c(124, 36, 13), 7 | blue = c(144, 58, 56)) 8 | 9 | qu <- color_enum$QUEEN_BLUE 10 | 11 | test_that("adding universal method with no parameters works", { 12 | add_enum_methods(color_enum, 13 | get_hex_color_w_index_name = function() { 14 | paste0(.value_name, 15 | " ", 16 | .index, 17 | " #", 18 | as.hexmode(.red), 19 | as.hexmode(.green), 20 | as.hexmode(.blue)) 21 | }) 22 | object <- color_enum[["methods_env"]]$methods_universal$get_hex_color_w_index_name(qu$value_name, 23 | qu$index, 24 | qu$red, 25 | qu$green, 26 | qu$blue) 27 | expect_equal(object, "QUEEN_BLUE 1 #437c90") 28 | }) 29 | 30 | test_that("adding universal method which does nothing with no parameters works", { 31 | add_enum_methods(color_enum, 32 | do_nothing = function() {NULL}) 33 | object <- color_enum[["methods_env"]]$methods_universal$do_nothing(qu$value_name, 34 | qu$index, 35 | qu$red, 36 | qu$green, 37 | qu$blue) 38 | expect_equal(object, NULL) 39 | }) 40 | 41 | test_that("adding universal method with some parameters works", { 42 | add_enum_methods(color_enum, 43 | return_multiplicity_of_index = function(multiplicity) { 44 | multiplicity * .index 45 | }) 46 | object <- color_enum[["methods_env"]]$methods_universal$return_multiplicity_of_index(12, 47 | qu$value_name, 48 | qu$index, 49 | qu$red, 50 | qu$green, 51 | qu$blue) 52 | expect_equal(object, 12) 53 | }) 54 | 55 | test_that("calling universal method with no parameters works", { 56 | object <- get_enum_method(qu, get_hex_color_w_index_name)() 57 | expect_equal(object, "QUEEN_BLUE 1 #437c90") 58 | }) 59 | 60 | test_that("calling universal method which does nothing with no parameters works", { 61 | object <- get_enum_method(qu, do_nothing)() 62 | expect_equal(object, NULL) 63 | }) 64 | 65 | test_that("calling universal method with some parameters works", { 66 | object <- get_enum_method(qu, return_multiplicity_of_index)(12) 67 | expect_equal(object, 12) 68 | }) 69 | -------------------------------------------------------------------------------- /docs/pkgdown.js: -------------------------------------------------------------------------------- 1 | /* http://gregfranko.com/blog/jquery-best-practices/ */ 2 | (function($) { 3 | $(function() { 4 | 5 | $("#sidebar") 6 | .stick_in_parent({offset_top: 40}) 7 | .on('sticky_kit:bottom', function(e) { 8 | $(this).parent().css('position', 'static'); 9 | }) 10 | .on('sticky_kit:unbottom', function(e) { 11 | $(this).parent().css('position', 'relative'); 12 | }); 13 | 14 | $('body').scrollspy({ 15 | target: '#sidebar', 16 | offset: 60 17 | }); 18 | 19 | $('[data-toggle="tooltip"]').tooltip(); 20 | 21 | var cur_path = paths(location.pathname); 22 | var links = $("#navbar ul li a"); 23 | var max_length = -1; 24 | var pos = -1; 25 | for (var i = 0; i < links.length; i++) { 26 | if (links[i].getAttribute("href") === "#") 27 | continue; 28 | // Ignore external links 29 | if (links[i].host !== location.host) 30 | continue; 31 | 32 | var nav_path = paths(links[i].pathname); 33 | 34 | var length = prefix_length(nav_path, cur_path); 35 | if (length > max_length) { 36 | max_length = length; 37 | pos = i; 38 | } 39 | } 40 | 41 | // Add class to parent
is.enum_value.RdTEMPORARY DOCUMENTATION
117 | 118 |is.enum_value(object)121 | 122 | 123 |
get_enum_values.RdTEMPORARY DOCUMENTATION
117 | 118 |get_enum_values(enum_type)121 | 122 | 123 |
add_enum_methods.RdTEMPORARY DOCUMENTATION
117 | 118 |add_enum_methods(enum_type, ...)121 | 122 | 123 |
get_enum_method.RdTEMPORARY DOCUMENTATION
117 | 118 |get_enum_method(enum_value, method)121 | 122 | 123 |
methodize.Rd@description TEMPORARY DOCUMENTATION
117 | 118 |methodize(enum_type, method)121 | 122 | 123 |
apply_enum_method.RdTEMPORARY DOCUMENTATION
117 | 118 |apply_enum_method(enum_value, method, ...)121 | 122 | 123 |
print.enum_value.RdTEMPORARY DOCUMENTATION
117 | 118 |# S3 method for enum_value 121 | print(enum_value)122 | 123 | 124 |
cash-.enum_type.Rdplaceholder
117 | 118 |# S3 method for enum_type 121 | $(enum_type, value)122 | 123 |
| enum_type | 128 |enum_type |
129 |
|---|---|
| index | 132 |index |
133 |
sub-.enum_type.Rdplaceholder
117 | 118 |# S3 method for enum_type 121 | [(enum_type, index)122 | 123 |
| enum_type | 128 |enum_type |
129 |
|---|---|
| index | 132 |index |
133 |
subset-.enum_type.Rdplaceholder
117 | 118 |# S3 method for enum_type 121 | [(enum_type, index) <- value122 | 123 |
| enum_type | 128 |enum_type |
129 |
|---|---|
| index | 132 |index |
133 |
cash-set-.enum_type.Rdplaceholder
117 | 118 |# S3 method for enum_type 121 | $(enum_type, index) <- value122 | 123 |
| enum_type | 128 |enum_type |
129 |
|---|---|
| index | 132 |index |
133 |
print.enum_type.RdPretty prints enum_type object. Overloads default print.
117 | 118 |# S3 method for enum_type 121 | print(enum_type)122 | 123 |
| enum_type | 128 |Object of class enum_type |
129 |
|---|
as.enum_type.RdFunctions to check if an object is an enum type, or coerce it if possible.
117 | 118 |as.enum_type(x)121 | 122 |
| x | 127 |any R object |
128 |
|---|
Object of class enum_type
This package provides enum_type and enum classes, which allows to create and instantinize enumerable types in R
77 |It is in it’s early development and authors don’t grat any assurance that function names, parameters or functionalities won’t change in future.
78 |To install this package you need to use devtools library, which can be installed with:
install.packages("devtools")
84 | If you have already installed devtools, you need to run following code to install this package:
devtools::install_github("DominikRafacz/enumeR")
86 | get_enum_index.RdExtracts `index` attribute value of passed `enum_value`.
117 | 118 |get_enum_index(enum_value)121 | 122 |
| enum_value | 127 |object of class `enum_value` or list of such objects with the same `enum_type` |
128 |
|---|
integer index of `enum_value` or named vector of integers coresponding to values in list
134 | 135 | 136 |enum_type.RdCreates new enum type.
117 | 118 |enum_type(type_name, values_names, ...)121 | 122 |
| type_name | 127 |Character vector of length 1. Name of the type. Type name will be referenced by this value |
128 |
|---|---|
| ... | 131 |Optional other fields that are used during constructing enum_values |
132 |
| value_names | 135 |Character vector of positive length. Possible names of enum_values (instances) |
136 |
is.enum_type.RdThis function checks class of passed object. If vector of classes contains
121 | "enum_type", then checks if all necessary fields are not null, namely
122 | type_name and values, and whether type_name is a singular
123 | character vector. Next, checks if fields_names is a character vector or null.
124 | Lastly, all values need to be unique and proper enum_value objects.
is.enum_type(object)129 | 130 |
TRUE if the object passed all tests described above, FALSE otherwise.
sample_enum.RdThis is a wrapper around [sample()] to make selecting random enum values easier.
117 | 118 |sample_enum(enum_type, size, replace = FALSE, weight = NULL)121 | 122 |
| enum_type | 127 |Object that passes [is.enum_type()] check. |
128 |
|---|---|
| size | 131 |A non-negative integer giving number of items to choose. |
132 |
| replace | 135 |Should sampling be with replacement? |
136 |
| weight | 139 |Sampling weights. This must evaluate to a vector of non-negative numbers the same 140 | length as the input. Weights are automatically standardised to sum to 1. |
141 |
A list of enum values of length size drawn from enum_type's values.
Introduction.RmdThis vignette will introduce you to basic functionalities of enumeR package.
First we create some enum_type object. It represents a class with finite number of possible values - similliar to enums from other languages like C++ or Java. During construction of an object besides names of values you can specify values of own fields that will be kept inside values. For example:
season_of_year <- enum_type("season_of_year",
87 | c("SPRING", "SUMMER", "AUTUMN", "WINTER"),
88 | min_temperature = c(-13, 7, -6, -20),
89 | max_temperature = c(20, 32, 18, 12))
90 |
91 | print(season_of_year)
92 | #> Enum type: season_of_year
93 | #>
94 | #> Values:
95 | #> 1 : SPRING
96 | #> 2 : SUMMER
97 | #> 3 : AUTUMN
98 | #> 4 : WINTER
99 | #> $SPRING
100 | #> NULL
101 | #>
102 | #> $SUMMER
103 | #> NULL
104 | #>
105 | #> $AUTUMN
106 | #> NULL
107 | #>
108 | #> $WINTER
109 | #> NULLNow you can access values of this type:
111 |my_favourite_seasons <- season_of_year[c(1,4)]
112 | my_hated_seasons <- season_of_year[c("SUMMER","AUTUMN")]
113 | holiday_season <- season_of_year$SUMMER… but cannot modify it:
115 |season_of_year$SUMMER <- "something"
116 | #> Error in `$<-.enum_type`(`*tmp*`, SUMMER, value = "something"): You cannot modify enum_type values!You can also choose a random subset:
118 | 119 |Objects have thier own classes, which can be checked:
120 |class(season_of_year)
121 | #> [1] "enum_type"
122 | class(holiday_season)
123 | #> [1] "enum_value"
124 | is.enum_type(season_of_year)
125 | #> [1] TRUE
126 | is.enum_value(holiday_season)
127 | #> [1] TRUEYou can add methods to enum_type which will be accessing its fields. You can access these fields using their names with dot before it. Thus it’s not recommended to add fields which names begin with dot.