tidyverts / fabletools

General fable features useful for extension packages
http://fabletools.tidyverts.org/
89 stars 31 forks source link

How to register package / tags to feature_set #89

Closed njtierney closed 5 years ago

njtierney commented 5 years ago

I really love the idea of listing out the features with feature_set:

library(feasts)
#> Loading required package: fablelite
#> 
#> Attaching package: 'feasts'
#> The following object is masked from 'package:grDevices':
#> 
#>     X11
feature_set(tags = "count")
#> [[1]]
#> function (x) 
#> {
#>     midline <- median(x, na.rm = TRUE)
#>     ab <- x <= midline
#>     lenx <- length(x)
#>     p1 <- ab[1:(lenx - 1)]
#>     p2 <- ab[2:lenx]
#>     cross <- (p1 & !p2) | (p2 & !p1)
#>     c(n_crossing_points = sum(cross, na.rm = TRUE))
#> }
#> <bytecode: 0x7fccaf4edde0>
#> <environment: namespace:feasts>
#> 
#> [[2]]
#> function (x) 
#> {
#>     cutx <- cut(x, breaks = 10, include.lowest = TRUE, labels = FALSE)
#>     rlex <- rle(cutx)
#>     return(c(n_flat_spots = max(rlex$lengths)))
#> }
#> <bytecode: 0x7fccb19a2978>
#> <environment: namespace:feasts>

Created on 2019-07-08 by the reprex package (v0.2.1)

Say I create my own features, how do I tell fablelite/feasts about them?

earowang commented 5 years ago

?register_feature()

earowang commented 5 years ago

maybe a section on "Register features" in the ?features() docs cc @mitchelloharawild?

njtierney commented 5 years ago

Ah nice - I did not see register_feature() 😓

Maybe it could be linked to in the documentation for features_set()?

Also, with the new list of functions feature, this doesn't seem to quite work:

library(feasts)
#> Loading required package: fablelite
#> 
#> Attaching package: 'feasts'
#> The following object is masked from 'package:grDevices':
#> 
#>     X11
feat_three <- list(min = min,
                   med = median,
                   max = max)

feat_three
#> $min
#> function (..., na.rm = FALSE)  .Primitive("min")
#> 
#> $med
#> function (x, na.rm = FALSE, ...) 
#> UseMethod("median")
#> <bytecode: 0x7f9b90f5c7b0>
#> <environment: namespace:stats>
#> 
#> $max
#> function (..., na.rm = FALSE)  .Primitive("max")

library(tsibbledata)

aus_retail %>%
  features(Turnover, feat_three)
#> # A tibble: 152 x 5
#>    State             Industry                               min   med   max
#>    <chr>             <chr>                                <dbl> <dbl> <dbl>
#>  1 Australian Capit… Cafes, restaurants and catering ser…   3.4 15.7   49.1
#>  2 Australian Capit… Cafes, restaurants and takeaway foo…   6.7 26     71.2
#>  3 Australian Capit… Clothing retailing                     2.7 10.4   34.7
#>  4 Australian Capit… Clothing, footwear and personal acc…   4.6 18.3   61.3
#>  5 Australian Capit… Department stores                      7.4 24.5   58.3
#>  6 Australian Capit… Electrical and electronic goods ret…   3.6 18.3   58.9
#>  7 Australian Capit… Food retailing                        14.6 82.6  232. 
#>  8 Australian Capit… Footwear and other personal accesso…   1.9  7.35  28.3
#>  9 Australian Capit… Furniture, floor coverings, housewa…   2.5 15.6   34.1
#> 10 Australian Capit… Hardware, building and garden suppl…   2.3 10.3   36  
#> # … with 142 more rows

# Register your new feature using `register_feature()`:

register_feature(feat_three, 
                 tags = c("summary", "simple"))
#> Error in as.list(table[[pkg]]): attempt to use zero-length variable name

Created on 2019-07-08 by the reprex package (v0.2.1)

mitchelloharawild commented 5 years ago

Hmm, this one is difficult. Essentially a "feature" should be a function that returns a value(s). A "feature set" is a list of features which may be named.

The user interface involves using one or more features to create a feature set, from which the features are computed.

For a package (like feasts) to create a new feature, you would use:

#' Three features
#' @export
feat_three <- function(x, ...){
  c(min = min(x, ...), med = median(x, ...), max = max(x, ...)
}

And then in the .onLoad() you would register the feature with:

register_feature(feat_three, 
                 tags = c("summary", "simple"))