To create a new class of projectable_col vectors, one simply needs to define a col_*() helper function. This can be done with the new_col() constructor function. The first field passed into the new_col() function via the ... argument will be taken as the face value. This can be overriden by explicitly defining a face_value() method. The subclass of the projectable_col created in this way will be projectable_col_* where * is replaced by whatever is passed in as the class argument.

new_col(..., shadow = character(), class)

Arguments

...

Fields to store in the projectable_col

shadow

A shadow to initialise the projectable_col with

class

A character vector giving the projectable_col subclass.

Value

A projectable_col vector with subclass projectable_col_* where * is given by whatever was passed in as the class argument.

Examples


# Define a `col_student` helper function:
col_student <- function(name = character(), age = integer(), grade = double()) {
  new_col(name = name, age = age, grade = grade, class = "student")
}
col_student(c("Kinto", "Pinto"), c(25, 100), c(99, 100))
#> <col_student[2]>
#> [1] Kinto Pinto

# Define a `col_fivenum` helper function:
col_fivenum <- function(x, na.rm = TRUE) {
  five_num <- fivenum(x, na.rm)
  new_col(
    median = five_num[3],
    min = five_num[1],
    hinge_lower = five_num[2],
    hinge_upper = five_num[4],
    max = five_num[5],
    class = "fivenum"
  )
}

library(dplyr)
iris %>%
  group_by(Species) %>%
  summarise(across(where(is.numeric), col_fivenum))
#> # A tibble: 3 × 5
#>   Species    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>   <fct>        <col_fvnm>  <col_fvnm>   <col_fvnm>  <col_fvnm>
#> 1 setosa                5         3.4          1.5         0.2
#> 2 versicolor          5.9         2.8          4.4         1.3
#> 3 virginica           6.5           3          5.6           2