0

I have the following function built, which fails with devtools::check() with the following message:

> fast_regression_parsnip_spec_tbl(.parsnip_fns = "linear_reg")
  Error in `dplyr::mutate()`:
  ! Problem while computing `.model_spec = purrr::pmap(dplyr::cur_data(),
    ~match.fun(..3)(mode = ..2, engine = ..1))`.
  Caused by error in `get()`:
  ! object 'linear_reg' of mode 'function' was not found
  Backtrace:
       ▆
    1. ├─tidyaml::fast_regression_parsnip_spec_tbl(.parsnip_fns = "linear_reg")
    2. │ └─mod_filtered_tbl %>% ...
    3. ├─dplyr::mutate(...)
    4. ├─dplyr:::mutate.data.frame(...)
    5. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
    6. │   ├─base::withCallingHandlers(...)
    7. │   └─mask$eval_all_mutate(quo)
    8. ├─purrr::pmap(dplyr::cur_data(), ~match.fun(..3)(mode = ..2, engine = ..1))
    9. │ └─tidyaml (local) .f(...)
   10. │   └─base::match.fun(..3)
   11. │     └─base::get(as.character(FUN), mode = "function", envir = envir)
   12. └─base::.handleSimpleError(...)
   13.   └─dplyr (local) h(simpleError(msg, call))
   14.     └─rlang::abort(...)
  Execution halted

Here is the function:

fast_regression_parsnip_spec_tbl <- function(.parsnip_fns = "all",
                                             .parsnip_eng = "all") {

  # Thank you https://stackoverflow.com/questions/74691333/build-a-tibble-of-parsnip-model-calls-with-match-fun/74691529#74691529
  # Tidyeval ----
  pf <- list(.parsnip_fns) %>%
    purrr::flatten_chr()
  pe <- list(.parsnip_eng) %>%
    purrr::flatten_chr()

  # Make tibble
  mod_tbl <- dplyr::tibble(
    .parsnip_engine = c(
      "lm",
      "brulee",
      "gee",
      "glm",
      "glmer",
      "glmnet",
      "gls",
      "h2o",
      "keras",
      "lme",
      "lmer",
      "spark",
      "stan",
      "stan_glmer",
      "Cubist",
      "glm",
      "gee",
      "glmer",
      "glmnet",
      "h2o",
      "hurdle",
      "stan",
      "stan_glmer",
      "zeroinfl",
      "survival",
      "flexsurv",
      "flexsurvspline"
    ),
    .parsnip_mode = c(
      rep("regression", 24),
      rep("censored regression", 3)
    ),
    .parsnip_fns = c(
      rep("linear_reg", 14),
      "cubist_rules",
      rep("poisson_reg",9),
      rep("survival_reg", 3)
    )
  )

  # Filter ----
  if (!"all" %in% pe){
    mod_tbl <- mod_tbl %>%
      dplyr::filter(.parsnip_engine %in% pe)
  }

  if (!"all" %in% pf){
    mod_tbl <- mod_tbl %>%
      dplyr::filter(.parsnip_fns %in% pf)
  }

  mod_filtered_tbl <- mod_tbl

  mod_spec_tbl <- mod_filtered_tbl %>%
    dplyr::mutate(
      .model_spec = purrr::pmap(
        dplyr::cur_data(),
        ~ match.fun(..3)(mode = ..2, engine = ..1)
      )
    )

  # Return ----
  class(mod_spec_tbl) <- c("fst_reg_spec_tbl", class(mod_spec_tbl))
  attr(mod_spec_tbl, ".parsnip_engines") <- .parsnip_eng
  attr(mod_spec_tbl, ".parsnip_functions") <- .parsnip_fns

  return(mod_spec_tbl)

}

Not sure how to proceed with this. I did try to include loadNameSpace(package = "parsnip") to the function but that did not do what I thought it would.

UPDATE I changed ~ match.fun... to ~ get... not entirely sure why this works so I'm leaving open, as I also here that using get is sort of hacky and potentially dangerous.

UPDATE

Here is my session info:

> library(tidyaml)
> sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tidyaml_0.0.0.9000

loaded via a namespace (and not attached):
 [1] parsnip_1.0.3    rstudioapi_0.14  magrittr_2.0.3   tidyselect_1.2.0 munsell_0.5.0   
 [6] colorspace_2.0-3 R6_2.5.1         rlang_1.0.6      fansi_1.0.3      dplyr_1.0.10    
[11] tools_4.2.1      hardhat_1.2.0    grid_4.2.1       gtable_0.3.1     utf8_1.2.2      
[16] cli_3.4.1        DBI_1.1.3        assertthat_0.2.1 tibble_3.1.8     lifecycle_1.0.3 
[21] tidyr_1.2.1      purrr_0.3.5      ggplot2_3.4.0    vctrs_0.5.1      glue_1.6.2      
[26] compiler_4.2.1   pillar_1.8.1     generics_0.1.3   scales_1.2.1     pkgconfig_2.0.3

Here are the full results of devtools::check() where match.fun is implemented.

==> devtools::check()

══ Documenting ═══════════════════════════════════════════════════════════════════════════════════════════
ℹ Updating tidyaml documentation
ℹ Loading tidyaml

══ Building ══════════════════════════════════════════════════════════════════════════════════════════════
Setting env vars:
• CFLAGS    : -Wall -pedantic -fdiagnostics-color=always
• CXXFLAGS  : -Wall -pedantic -fdiagnostics-color=always
• CXX11FLAGS: -Wall -pedantic -fdiagnostics-color=always
• CXX14FLAGS: -Wall -pedantic -fdiagnostics-color=always
• CXX17FLAGS: -Wall -pedantic -fdiagnostics-color=always
• CXX20FLAGS: -Wall -pedantic -fdiagnostics-color=always
── R CMD build ───────────────────────────────────────────────────────────────────────────────────────────
✔  checking for file 'C:\Users\steve\Documents\GitHub\tidyaml/DESCRIPTION' ...
─  preparing 'tidyaml': (655ms)
✔  checking DESCRIPTION meta-information ...
─  installing the package to build vignettes
✔  creating vignettes (5.6s)
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
─  building 'tidyaml_0.0.0.9000.tar.gz'
   
══ Checking ══════════════════════════════════════════════════════════════════════════════════════════════
Setting env vars:
• _R_CHECK_CRAN_INCOMING_REMOTE_               : FALSE
• _R_CHECK_CRAN_INCOMING_                      : FALSE
• _R_CHECK_FORCE_SUGGESTS_                     : FALSE
• _R_CHECK_PACKAGES_USED_IGNORE_UNUSED_IMPORTS_: FALSE
• NOT_CRAN                                     : true
── R CMD check ───────────────────────────────────────────────────────────────────────────────────────────
─  using log directory 'C:/Users/steve/Documents/GitHub/tidyaml.Rcheck'
─  using R version 4.2.1 (2022-06-23 ucrt)
─  using platform: x86_64-w64-mingw32 (64-bit)
─  using session charset: UTF-8
─  using options '--no-manual --as-cran'
✔  checking for file 'tidyaml/DESCRIPTION'
─  this is package 'tidyaml' version '0.0.0.9000'
─  package encoding: UTF-8
✔  checking package namespace information
✔  checking package dependencies (5.2s)
✔  checking if this is a source package ...
✔  checking if there is a namespace
✔  checking for executable files (349ms)
✔  checking for hidden files and directories ...
✔  checking for portable file names
✔  checking serialization versions
✔  checking whether package 'tidyaml' can be installed (3.5s)
✔  checking installed package size ... 
✔  checking package directory
✔  checking for future file timestamps ... 
✔  checking 'build' directory ...
✔  checking DESCRIPTION meta-information ... 
✔  checking top-level files
✔  checking for left-over files ...
✔  checking index information ... 
✔  checking package subdirectories ... 
✔  checking R files for non-ASCII characters ... 
✔  checking R files for syntax errors ... 
✔  checking whether the package can be loaded (890ms)
✔  checking whether the package can be loaded with stated dependencies (770ms)
✔  checking whether the package can be unloaded cleanly (773ms)
✔  checking whether the namespace can be loaded with stated dependencies (790ms)
✔  checking whether the namespace can be unloaded cleanly (991ms)
✔  checking loading without being on the library search path (977ms)
✔  checking dependencies in R code (776ms)
✔  checking S3 generic/method consistency (1.2s)
✔  checking replacement functions (768ms)
✔  checking foreign function calls (775ms)
✔  checking R code for possible problems (5.2s)
✔  checking Rd files ... 
✔  checking Rd metadata ... 
✔  checking Rd line widths ... 
✔  checking Rd cross-references ... 
✔  checking for missing documentation entries (774ms)
✔  checking for code/documentation mismatches (2.3s)
✔  checking Rd \usage sections (1.4s)
✔  checking Rd contents ... 
✔  checking for unstated dependencies in examples ... 
✔  checking installed files from 'inst/doc'
✔  checking files in 'vignettes'
E  checking examples (2.2s)
   Running examples in 'tidyaml-Ex.R' failed
   The error most likely occurred in:
   
   > base::assign(".ptime", proc.time(), pos = "CheckExEnv")
   > ### Name: fast_regression_parsnip_spec_tbl
   > ### Title: Utility Regression call to 'parsnip'
   > ### Aliases: fast_regression_parsnip_spec_tbl
   > 
   > ### ** Examples
   > 
   > fast_regression_parsnip_spec_tbl(.parsnip_fns = "linear_reg")
   Error in `dplyr::mutate()`:
   ! Problem while computing `.model_spec = purrr::pmap(dplyr::cur_data(),
     ~match.fun(..3)(mode = ..2, engine = ..1))`.
   Caused by error in `get()`:
   ! object 'linear_reg' of mode 'function' was not found
   Backtrace:
        ▆
     1. ├─tidyaml::fast_regression_parsnip_spec_tbl(.parsnip_fns = "linear_reg")
     2. │ └─mod_filtered_tbl %>% ...
     3. ├─dplyr::mutate(...)
     4. ├─dplyr:::mutate.data.frame(...)
     5. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
     6. │   ├─base::withCallingHandlers(...)
     7. │   └─mask$eval_all_mutate(quo)
     8. ├─purrr::pmap(dplyr::cur_data(), ~match.fun(..3)(mode = ..2, engine = ..1))
     9. │ └─tidyaml (local) .f(...)
    10. │   └─base::match.fun(..3)
    11. │     └─base::get(as.character(FUN), mode = "function", envir = envir)
    12. └─base::.handleSimpleError(...)
    13.   └─dplyr (local) h(simpleError(msg, call))
    14.     └─rlang::abort(...)
   Execution halted
✔  checking for unstated dependencies in vignettes ... 
✔  checking package vignettes in 'inst/doc' ...
✔  checking re-building of vignette outputs (1.8s)
✔  checking for non-standard things in the check directory
✔  checking for detritus in the temp directory ...
   
   See
     'C:/Users/steve/Documents/GitHub/tidyaml.Rcheck/00check.log'
   for details.
   
   
── R CMD check results ─────────────────────────────────────────────────────────── tidyaml 0.0.0.9000 ────
Duration: 35.9s

❯ checking examples ... ERROR
  Running examples in 'tidyaml-Ex.R' failed
  The error most likely occurred in:
  
  > base::assign(".ptime", proc.time(), pos = "CheckExEnv")
  > ### Name: fast_regression_parsnip_spec_tbl
  > ### Title: Utility Regression call to 'parsnip'
  > ### Aliases: fast_regression_parsnip_spec_tbl
  > 
  > ### ** Examples
  > 
  > fast_regression_parsnip_spec_tbl(.parsnip_fns = "linear_reg")
  Error in `dplyr::mutate()`:
  ! Problem while computing `.model_spec = purrr::pmap(dplyr::cur_data(),
    ~match.fun(..3)(mode = ..2, engine = ..1))`.
  Caused by error in `get()`:
  ! object 'linear_reg' of mode 'function' was not found
  Backtrace:
       ▆
    1. ├─tidyaml::fast_regression_parsnip_spec_tbl(.parsnip_fns = "linear_reg")
    2. │ └─mod_filtered_tbl %>% ...
    3. ├─dplyr::mutate(...)
    4. ├─dplyr:::mutate.data.frame(...)
    5. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
    6. │   ├─base::withCallingHandlers(...)
    7. │   └─mask$eval_all_mutate(quo)
    8. ├─purrr::pmap(dplyr::cur_data(), ~match.fun(..3)(mode = ..2, engine = ..1))
    9. │ └─tidyaml (local) .f(...)
   10. │   └─base::match.fun(..3)
   11. │     └─base::get(as.character(FUN), mode = "function", envir = envir)
   12. └─base::.handleSimpleError(...)
   13.   └─dplyr (local) h(simpleError(msg, call))
   14.     └─rlang::abort(...)
  Execution halted

1 error ✖ | 0 warnings ✔ | 0 notes ✔
Error: R CMD check found ERRORs
Execution halted

Exited with status 1.

parsn

        #~ get(..3)(mode = ..2, engine = ..1)



MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82
  • Your code works for me (with `match.fun`). Can you include the output of `library(tidymodels)` so that we can see the versions of the packages you have? – bretauv Dec 06 '22 at 08:54
  • yes I will do that now – MCP_infiltrator Dec 06 '22 at 13:03
  • possible because it is loaded via namespace but not attached? – MCP_infiltrator Dec 06 '22 at 13:10
  • I have the same error if I put this function in a package (imports in description are dplyr, purrr, parsnip and magrittr) – bretauv Dec 06 '22 at 13:27
  • If I put `library(parsnip)` at the beginning of the function then it works so indeed this is probably related to the fact that the package is not attached – bretauv Dec 06 '22 at 13:29
  • [This answer](https://stackoverflow.com/a/11991403/11598948) describes very well the problem and shows two solutions: 1) use `get()` as you did, 2) reexport the functions you need from `parsnip`. I think there's nothing more to add to the answer I refer to so I vote to close – bretauv Dec 06 '22 at 13:37
  • I am going to place parsnip in the Depends section of the DESCRIPTION file and see if that does it with use match.fun if not then I'll keep get and yes this answers the question and I agree to close. **UPDATE** placing parsnip in Depends: fixed the issue – MCP_infiltrator Dec 06 '22 at 13:47

0 Answers0