Q: Make feature return different classes I'm trying to create my own feature, which I want to return both POSIXct and numeric values. I discovered the only workaround is to use the unnest() after the features function. Can we simplify this workflow? I want to include features in my package, and I don't want to make users applying unnest every time after features.
Sample code
library(feasts, quietly = T, warn.conflicts = F)
library(dplyr, quietly = T, warn.conflicts = F)
library(tidyr, warn.conflicts = F, quietly = T)

  df <- data.frame(datetime = seq(c(ISOdate(2000,3,20)),
                                  by = "hour",
                                  length.out = 30),
                   group = c(rep("A", 15), rep("B", 15)),
                   x = rnorm(30))
  
  feat_event <- function(x){
    
    start <- dplyr::first(x)
    end <- dplyr::last(x)
    length <- difftime(end, start, units = "h")
    
    output <- list(
      start = unname(start),
      end = unname(end),
      length = unname(length)
    )
    
    output
  }
  
  feat_event(df$datetime)
#> $start
#> [1] "2000-03-20 12:00:00 GMT"
#> 
#> $end
#> [1] "2000-03-21 17:00:00 GMT"
#> 
#> $length
#> Time difference of 29 hours
  
  df %>% 
    as_tsibble(key = group, index = datetime) %>% 
    features(datetime, feat_event) %>% 
    unnest()
#> Warning: `cols` is now required when using unnest().
#> Please use `cols = c(start, end, length)`
#> # A tibble: 2 x 4
#>   group start               end                 length  
#>   <chr> <dttm>              <dttm>              <drtn>  
#> 1 A     2000-03-20 12:00:00 2000-03-21 02:00:00 14 hours
#> 2 B     2000-03-21 03:00:00 2000-03-21 17:00:00 14 hours

Created on 2021-03-17 by the reprex package (v1.0.0)

A: Return a tibble instead of list.
feat_event <- function(x){
  
  start <- dplyr::first(x)
  end <- dplyr::last(x)
  length <- difftime(end, start, units = "h")
  
  output <- tibble(
    start = unname(start),
    end = unname(end),
    length = unname(length)
  )
  
  output
}

feat_event(df$datetime)
# A tibble: 1 x 3
#  start               end                 length  
#  <dttm>              <dttm>              <drtn>  
#1 2000-03-20 12:00:00 2000-03-21 17:00:00 29 hours

df %>% 
   as_tsibble(key = group, index = datetime) %>% 
   features(datetime, feat_event)

# A tibble: 2 x 4
#  group start               end                 length  
#  <chr> <dttm>              <dttm>              <drtn>  
#1 A     2000-03-20 12:00:00 2000-03-21 02:00:00 14 hours
#2 B     2000-03-21 03:00:00 2000-03-21 17:00:00 14 hours

