Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in call2 when using local fit function and omitting pkg #1229

Closed
chillerb opened this issue Jan 16, 2025 · 2 comments · Fixed by #1230
Closed

Error in call2 when using local fit function and omitting pkg #1229

chillerb opened this issue Jan 16, 2025 · 2 comments · Fixed by #1230
Labels
bug an unexpected problem or unintended behavior

Comments

@chillerb
Copy link

The problem

According to the parsnip tutorial, pkg is not required to use a local function for fitting:

func is the package and name of the function that will be called. If you are using a locally defined function, only fun is required.

However, if I try to register a model with a local fit function, I receive an error in call2, that namespace ns should be a string.
I am quite new to tidymodels, am I missing something?

Reproducible example

library(tidymodels)

set_new_model("my_model")

set_model_mode(model = "my_model", mode = "regression")

set_model_engine(
  "my_model", 
  mode = "regression", 
  eng = "my_engine"
)

my_model <-
  function(mode = "regression") {
    # Check for correct mode
    if (mode  != "regression") {
      rlang::abort("`mode` should be 'regression'.")
    }
    
    # Save some empty slots for future parts of the specification
    new_model_spec(
      "my_model",
      args = list(),
      eng_args = NULL,
      mode = mode,
      method = NULL,
      engine = NULL
    )
  }

my_model_fun <- function(...) {
  print(list(...))
}

set_fit(
  model = "my_model",
  eng = "my_engine",
  mode = "regression",
  value = list(
    interface = "matrix",
    # should not be changeable by the user
    protect = c("formula", "data"),
    func = c(fun = "my_model_fun"),
    defaults = list()
  )
)

my_model() %>%
  translate("my_engine")
#> my model Model Specification (regression)
#> 
#> Computational engine: my_engine 
#> 
#> Model fit template:
#> Error in `call2()` at parsnip/R/misc.R:245:3:
#> ! `ns` must be a string

Created on 2025-01-16 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14)
#>  os       Ubuntu 22.04.5 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Etc/UTC
#>  date     2025-01-16
#>  pandoc   3.4 @ /usr/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package      * version    date (UTC) lib source
#>    backports      1.5.0      2024-05-23 [1] CRAN (R 4.4.1)
#>  P broom        * 1.0.7      2024-09-26 [?] CRAN (R 4.4.1)
#>  P class          7.3-22     2023-05-03 [3] CRAN (R 4.4.1)
#>  P cli            3.6.3      2024-06-21 [?] CRAN (R 4.4.1)
#>  P codetools      0.2-20     2024-03-31 [3] CRAN (R 4.4.1)
#>    colorspace     2.1-1      2024-07-26 [1] CRAN (R 4.4.1)
#>    data.table     1.16.0     2024-08-27 [1] CRAN (R 4.4.1)
#>  P dials        * 1.3.0      2024-07-30 [?] CRAN (R 4.4.1)
#>  P DiceDesign     1.10       2023-12-07 [?] CRAN (R 4.4.1)
#>  P digest         0.6.37     2024-08-19 [?] CRAN (R 4.4.1)
#>  P dplyr        * 1.1.4      2023-11-17 [?] CRAN (R 4.4.1)
#>  P evaluate       1.0.0      2024-09-17 [?] CRAN (R 4.4.1)
#>  P fansi          1.0.6      2023-12-08 [?] CRAN (R 4.4.1)
#>  P fastmap        1.2.0      2024-05-15 [?] CRAN (R 4.4.1)
#>    foreach        1.5.2      2022-02-02 [1] CRAN (R 4.4.1)
#>  P fs             1.6.4      2024-04-25 [?] CRAN (R 4.4.1)
#>  P furrr          0.3.1      2022-08-15 [?] CRAN (R 4.4.1)
#>  P future         1.34.0     2024-07-29 [?] CRAN (R 4.4.1)
#>  P future.apply   1.11.3     2024-10-27 [?] CRAN (R 4.4.1)
#>  P generics       0.1.3      2022-07-05 [?] CRAN (R 4.4.1)
#>    ggplot2      * 3.5.1      2024-04-23 [1] CRAN (R 4.4.1)
#>  P globals        0.16.3     2024-03-08 [?] CRAN (R 4.4.1)
#>  P glue           1.7.0      2024-01-09 [?] CRAN (R 4.4.1)
#>  P gower          1.0.1      2022-12-22 [?] CRAN (R 4.4.1)
#>  P GPfit          1.0-8      2019-02-08 [?] CRAN (R 4.4.1)
#>    gtable         0.3.5      2024-04-22 [1] CRAN (R 4.4.1)
#>  P hardhat        1.4.0      2024-06-02 [?] CRAN (R 4.4.1)
#>  P htmltools      0.5.8.1    2024-04-04 [?] CRAN (R 4.4.1)
#>  P infer        * 1.0.7      2024-03-25 [?] CRAN (R 4.4.1)
#>  P ipred          0.9-15     2024-07-18 [?] CRAN (R 4.4.1)
#>    iterators      1.0.14     2022-02-05 [1] CRAN (R 4.4.1)
#>  P knitr          1.49       2024-11-08 [?] CRAN (R 4.4.1)
#>  P lattice        0.22-6     2024-03-20 [3] CRAN (R 4.4.1)
#>  P lava           1.8.0      2024-03-05 [?] CRAN (R 4.4.1)
#>  P lhs            1.2.0      2024-06-30 [?] CRAN (R 4.4.1)
#>  P lifecycle      1.0.4      2023-11-07 [?] CRAN (R 4.4.1)
#>  P listenv        0.9.1      2024-01-29 [?] CRAN (R 4.4.1)
#>  P lubridate      1.9.4      2024-12-08 [?] CRAN (R 4.4.1)
#>  P magrittr       2.0.3      2022-03-30 [?] CRAN (R 4.4.1)
#>  P MASS           7.3-60.2   2024-04-26 [3] CRAN (R 4.4.1)
#>  P Matrix         1.7-0      2024-04-26 [3] CRAN (R 4.4.1)
#>  P modeldata    * 1.4.0      2024-06-19 [?] CRAN (R 4.4.1)
#>    munsell        0.5.1      2024-04-01 [1] CRAN (R 4.4.1)
#>  P nnet           7.3-19     2023-05-03 [3] CRAN (R 4.4.1)
#>  P parallelly     1.40.0     2024-12-03 [?] CRAN (R 4.4.1)
#>  P parsnip      * 1.2.1      2024-03-22 [?] CRAN (R 4.4.1)
#>  P pillar         1.9.0      2023-03-22 [?] CRAN (R 4.4.1)
#>  P pkgconfig      2.0.3      2019-09-22 [?] CRAN (R 4.4.1)
#>  P prodlim        2024.06.25 2024-06-24 [?] CRAN (R 4.4.1)
#>  P purrr        * 1.0.2      2023-08-10 [?] CRAN (R 4.4.1)
#>  P R6             2.5.1      2021-08-19 [?] CRAN (R 4.4.1)
#>  P Rcpp           1.0.13     2024-07-17 [?] CRAN (R 4.4.1)
#>  P recipes      * 1.1.0      2024-07-04 [?] CRAN (R 4.4.1)
#>  P reprex         2.1.1      2024-07-06 [?] CRAN (R 4.4.1)
#>  P rlang          1.1.4      2024-06-04 [?] CRAN (R 4.4.1)
#>  P rmarkdown      2.29       2024-11-04 [?] CRAN (R 4.4.1)
#>  P rpart          4.1.23     2023-12-05 [3] CRAN (R 4.4.1)
#>  P rsample      * 1.2.1      2024-03-25 [?] CRAN (R 4.4.1)
#>  P rstudioapi     0.16.0     2024-03-24 [?] CRAN (R 4.4.1)
#>  P scales       * 1.3.0      2023-11-28 [?] CRAN (R 4.4.1)
#>  P sessioninfo    1.2.2      2021-12-06 [?] CRAN (R 4.4.1)
#>  P survival       3.6-4      2024-04-24 [3] CRAN (R 4.4.1)
#>  P tibble       * 3.2.1      2023-03-20 [?] CRAN (R 4.4.1)
#>  P tidymodels   * 1.2.0      2024-03-25 [?] CRAN (R 4.4.1)
#>  P tidyr        * 1.3.1      2024-01-24 [?] CRAN (R 4.4.1)
#>  P tidyselect     1.2.1      2024-03-11 [?] CRAN (R 4.4.1)
#>  P timechange     0.3.0      2024-01-18 [?] CRAN (R 4.4.1)
#>  P timeDate       4041.110   2024-09-22 [?] CRAN (R 4.4.1)
#>  P tune         * 1.2.1      2024-04-18 [?] CRAN (R 4.4.1)
#>  P utf8           1.2.4      2023-10-22 [?] CRAN (R 4.4.1)
#>  P vctrs          0.6.5      2023-12-01 [?] CRAN (R 4.4.1)
#>  P withr          3.0.1      2024-07-31 [?] CRAN (R 4.4.1)
#>  P workflows    * 1.1.4      2024-02-19 [?] CRAN (R 4.4.1)
#>  P workflowsets * 1.1.0      2024-03-21 [?] CRAN (R 4.4.1)
#>  P xfun           0.49       2024-10-31 [?] CRAN (R 4.4.1)
#>  P yaml           2.3.10     2024-07-26 [?] CRAN (R 4.4.1)
#>  P yardstick    * 1.3.1      2024-03-21 [?] CRAN (R 4.4.1)
#> 
#>  [1] /root/.cache/R/renv/library/rabaki-e6dba559/linux-ubuntu-jammy/R-4.4/x86_64-pc-linux-gnu
#>  [2] /root/.cache/R/renv/sandbox/linux-ubuntu-jammy/R-4.4/x86_64-pc-linux-gnu/25ebdc09
#>  [3] /usr/local/lib/R/library
#> 
#>  P ── Loaded and on-disk path mismatch.
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@chillerb
Copy link
Author

The error seems to originate from:

#' Print the model call
#'
#' @param object A "model_spec" object.
#' @return A character string.
#' @keywords internal
#' @export
show_call <- function(object) {
  object$method$fit$args <-
    map(object$method$fit$args, convert_arg)

  call2(object$method$fit$func["fun"],
    !!!object$method$fit$args,
    .ns = object$method$fit$func["pkg"]
  )
}

I noticed that the pkg name is NA, if pkg is not passed or even explicitly set to pkg=NULL:

`> object$method$fit$func["pkg"]
<NA> 
  NA 

Is this intended? Call2 works for .ns = NULL, but fails for .ns = NA.

@EmilHvitfeldt EmilHvitfeldt added the bug an unexpected problem or unintended behavior label Jan 16, 2025
topepo added a commit that referenced this issue Jan 19, 2025
topepo added a commit that referenced this issue Jan 29, 2025
* push back a month

* redoc

* changes for #1229

* small cleanup

* update news

* snapshot

* Apply suggestions from code review

Co-authored-by: Hannah Frick <[email protected]>

---------

Co-authored-by: Hannah Frick <[email protected]>
Copy link

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants