Skip to content

Cannot retrieve original data object with "rpart" engine #1130

Closed
@grantmcdermott

Description

@grantmcdermott

The problem

Hi folks. I'm having trouble retrieving the original data object that was passed to the "rpart" engine. The old eval(tree$call$data) approach doesn't work here b/c the data object is hidden... and so it reverts back to the global utils::data() function.

I tried to see how you implement this as part of the update method for rpart backends. But unless I'm missing something, an _rpart.update method is not supported either.

Reproducible example

library(parsnip)
library(rpart)

tree = decision_tree() |>
  set_engine("rpart") |>
  set_mode("classification") |>
  fit(Species ~ Petal.Length + Petal.Width, data = iris)


tree$fit$call
#> rpart::rpart(formula = Species ~ Petal.Length + Petal.Width, 
#>     data = data)
# data object here is evaluated as the utils::data() function, not the actual passed data object
head(eval(tree$fit$call$data))
#>                                                                             
#> 1 function (..., list = character(), package = NULL, lib.loc = NULL,        
#> 2     verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE) 
#> 3 {                                                                         
#> 4     fileExt <- function(x) {                                              
#> 5         db <- grepl("\\\\.[^.]+\\\\.(gz|bz2|xz)$", x)                     
#> 6         ans <- sub(".*\\\\.", "", x)
# update doesn't work either
update(tree)
#> Error in update.default(tree): need an object with call component

Created on 2024-07-15 with reprex v2.1.0

Any suggestions for a workaround would be much appreciated. Thanks in advance.

P.S. Possibly related to #257 but I need an automated way to retrieve the original data object. In this issue, the solution was to manually write it back to the call. Similarly, repair_call also needs the user to manually specify the data object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions