tidymodelsdrake-r-package

Error when using initial_time_split with drake


Learning how to use drake with tidymodels.

Something about using rsample's initial_time_split(), rather than just initial_split(), is giving me an error, when I run make(plan). I get the following:

#> > target data
#> > target split_data
#> Error in UseMethod("complement"): no applicable method for 'complement' applied to an object of class "rsplit"

Have really been racking my brain on this one. The function works fine, independently (ie. the following works): enter image description here

I feel like I am missing something pretty basic.

Here is the full drake process in a single file (so that it is easier to post up on stack overflow).

Thanks in advance for the hints, as to what I'm doing wrong.

library(drake)
library(tidyverse)
library(tidymodels)

###################################################################

generate_data <- function() {
  tibble(x = rnorm(1e5), y = rnorm(1e5))
}


split_the_data <- function(data) {
  
  data %>% 
    initial_time_split()
  
}


fit_model <- function(data) {
  
  summary(lm(y ~ x, data = data))
}



###################################################################


plan <- drake_plan(
  
  data = generate_data(),
  
  split_data = split_the_data(data),
  
  model = fit_model(training(split_data))
  
)


###################################################################


make(plan)

Solution

  • This should now be fixed in the current development version (as of 93d60ef41119defc0432cc95d2dd6787e4a00b14). You can install it with

    install.packages("remotes")
    remotes::install_github("ropensci/drake")
    

    The error happened because drake calls NROW() on every target (for dynamic branching purposes) and apparently NROW() errors on rsplit objects.

    library(tidyverse)
    library(tidymodels)
    #> ── Attaching packages ─────────────────────────────────────── tidymodels 0.1.1 ──
    #> ✓ broom     0.7.0      ✓ recipes   0.1.13
    #> ✓ dials     0.0.8      ✓ rsample   0.0.7 
    #> ✓ infer     0.5.3      ✓ tune      0.1.1 
    #> ✓ modeldata 0.0.2      ✓ workflows 0.1.2 
    #> ✓ parsnip   0.1.2      ✓ yardstick 0.0.7
    #> ── Conflicts ────────────────────────────────────────── tidymodels_conflicts() ──
    #> x scales::discard() masks purrr::discard()
    #> x dplyr::filter()   masks stats::filter()
    #> x recipes::fixed()  masks stringr::fixed()
    #> x dplyr::lag()      masks stats::lag()
    #> x yardstick::spec() masks readr::spec()
    #> x recipes::step()   masks stats::step()
    
    generate_data <- function() {
        tibble(x = rnorm(1e5), y = rnorm(1e5))
    }
    
    split_the_data <- function(data) {
        data %>% 
            initial_time_split()
    }
    
    NROW(split_the_data(generate_data()))
    #> Error in UseMethod("complement"): no applicable method for 'complement' applied to an object of class "rsplit"
    

    Created on 2020-07-23 by the reprex package (v0.3.0)

    Issue tracked in https://github.com/ropensci/drake/issues/1300.