Skip to content

Commit

Permalink
a start on measure class
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Mar 4, 2024
1 parent 1129f60 commit 2ffdf11
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
89 changes: 89 additions & 0 deletions R/class-measure.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# ------------------------------------------------------------------------------
# Creation

new_measure <- function(x, labels, ..., subclass = NULL) {
new_vctr(
.data = x,
...,
class = c(subclass, "measure")
)
}


measure <- function() {
which <- vec_cast(which, integer())

# no duplicates allowed
which <- unique(which)

# which cannot go outside the range of the number of values in x
if (length(which) > 0L && max(which) > length(x)) {
msg <- paste0("The largest value of `which` can be ", length(x), ".")
abort(msg)
}

labs <- levels(x)

# Check for `equivocal` in labels. Not allowed.
if (equivocal %in% labs) {
msg <- paste0(
"`\"", equivocal, "\"`",
"is reserved for equivocal values",
"and must not already be a level."
)
abort(msg)
}

# rip out the underlying integer structure
# as.integer() also removes attributes
x_int <- as.integer(unclass(x))

# declare equivocal
x_int[which] <- 0L

new_class_pred(
x = x_int,
labels = labs,
ordered = is.ordered(x),
equivocal = equivocal
)
}

# ------------------------------------------------------------------------------
# Printing

# Always return a character vector
# Rely on as.character.factor() for NA handling
# Used by data.frame() columns and general printing
#' @export
format.measure <- function(x, ...) {

}

# -------------------------------------------------------------------------
# Check that measures are the `measure_obj` class

is_measure <- function(x) {
inherits(x, "measure_obj")
}

# ------------------------------------------------------------------------------
# Coercion

#' Coerce to a `measure` object
#'
#' `as_measure()` provides coercion to `measure` from other
#' existing objects.
#'
#' @examples
#'
#'
#' @export
as_measure <- function(x) {
UseMethod("as_measure")
}

#' @export
as_measure.default <- function(x) {
abort_default(x, "as_class_pred")
}
10 changes: 3 additions & 7 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ The goal of measure is to be a recipes-like interface to tidymodels for analytic
You can install the development version of measure like so:

``` r
remotes::install_github("jameshwade/measure")
require(pak)
pak::pak("jameshwade/measure")
```

## Example

This is a basic example which shows you how to solve a common problem:

```r
library(measure)
## basic example code
```
More to come.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](h
[![R-CMD-check](https://github.com/JamesHWade/measure/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/JamesHWade/measure/actions/workflows/R-CMD-check.yaml)
[![Codecov test
coverage](https://codecov.io/gh/JamesHWade/measure/branch/main/graph/badge.svg)](https://app.codecov.io/gh/JamesHWade/measure?branch=main)
[![James’s GitHub
stats](https://github-readme-stats.vercel.app/api?username=jameshwade)](https://github.com/jameshwade/github-readme-stats)
<!-- badges: end -->

The goal of measure is to be a recipes-like interface to tidymodels for
Expand Down

0 comments on commit 2ffdf11

Please sign in to comment.