{targets} is a package to manage the workflow. (https://wlandau.github.io/targets-manual/)
I tried to apply the {targets} for the tidymodels framework.
When I re-runtarget to get the coefficients of the final model, It returns null
I was able to use your code and did not run into any issues which produced a NULL value for the target coeff. Can you try running the code below and see if you are able to reproduce the same results?
library(targets)
tar_script({
options(tidyverse.quiet = TRUE)
tar_option_set(packages = c("tidyverse", "tidymodels"))
tar_pipeline(
tar_target(
glmn_rec,
recipe(mpg ~ ., data = mtcars) %>%
step_normalize(all_predictors())
),
tar_target(
mod,
linear_reg(penalty = 0.1, mixture = 1) %>%
set_engine("glmnet")
),
tar_target(
glmn_wflow,
workflow() %>%
add_model(mod) %>%
add_recipe(glmn_rec)
),
tar_target(
glmn_fit,
glmn_wflow %>%
fit(data = mtcars)
),
tar_target(
coeff,
glmn_fit %>%
pull_workflow_fit() %>%
pluck("fit") %>%
coef(s = 0.1)
)
)
})
tar_make()
#> e[34m●e[39m run target glmn_rec
#> e[34m●e[39m run target mod
#> e[34m●e[39m run target glmn_wflow
#> e[34m●e[39m run target glmn_fit
#> e[34m●e[39m run target coeff
tar_read(coeff)
#> Loading required package: Matrix
#> 11 x 1 sparse Matrix of class "dgCMatrix"
#> 1
#> (Intercept) 20.09062500
#> cyl -0.39267064
#> disp .
#> hp -0.89172557
#> drat 0.41257152
#> wt -2.58105019
#> qsec 0.82333186
#> vs 0.05920734
#> am 1.05461719
#> gear 0.22456562
#> carb -0.75029547
tar_make()
#> e[32m✓e[39m skip target glmn_rec
#> e[32m✓e[39m skip target mod
#> e[32m✓e[39m skip target glmn_wflow
#> e[32m✓e[39m skip target glmn_fit
#> e[32m✓e[39m skip target coeff
#> e[32m✓e[39m Already up to date.
tar_read(coeff)
#> 11 x 1 sparse Matrix of class "dgCMatrix"
#> 1
#> (Intercept) 20.09062500
#> cyl -0.39267064
#> disp .
#> hp -0.89172557
#> drat 0.41257152
#> wt -2.58105019
#> qsec 0.82333186
#> vs 0.05920734
#> am 1.05461719
#> gear 0.22456562
#> carb -0.75029547
Thanks for updating the example. I was able to reproduce your issue. I don't know why it's happening but after some playing around I was able to avoid the issue by making a namespaced call to glmnet:::coef.glmnet().
Can you try the code below and see if it works on your end when you update s = 0.1 before calling tar_make() a second time?
No problem. While that did seem to solve the issue at hand, it's generally not recommended to namspace internal functions (i.e use the triple-colon operator :::), especially internal S3 methods. A more robust long-term solution is needed, but I'm glad it was helpful in the short-term.