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: fn must be an R function, not a primitive function" after library call #48

Closed
moodymudskipper opened this issue Nov 21, 2019 · 16 comments

Comments

@moodymudskipper
Copy link

@moodymudskipper moodymudskipper commented Nov 21, 2019

This happens when attaching one of my packages :

library(conflicted)
library(inops)
#> Error: `fn` must be an R function, not a primitive function

The package can be attached without problem if conflicted is not attached.

library(inops)
#> 
#> Attaching package: 'inops'
#> The following object is masked from 'package:base':
#> 
#>     <<-

It appears to come from the fact that the package overrides a primitive (namely <<-).

library(conflicted) triggers conflicted:::.onAttach(), which calls conflicted:::conflicts_register(), which calls conflicted:::is_superset() and rlang::fn_fmls() is called, and chokes on primitives because they have no formals.

If I call library() twice then I have no error and can use the package, including <<- :

library(conflicted)
library(inops)
#> Error: `fn` must be an R function, not a primitive function
library(inops)
2 %in[]% c(1,3)
#> [1] TRUE
x <- 1:4
x < 3 <- 0
x
#> [1] 0 0 3 4

library(conflicted) will be the one failing if it's called second :

library(inops)
#> 
#> Attaching package: 'inops'
#> The following object is masked from 'package:base':
#> 
#>     <<-
library(conflicted)
#> Error: package or namespace load failed for 'conflicted':
#>  .onAttach failed in attachNamespace() for 'conflicted', details:
#>   call: NULL
#>   error: `fn` must be an R function, not a primitive function
@jtelleriar
Copy link

@jtelleriar jtelleriar commented Nov 27, 2019

This happens also with h2o R Library :(

@peterlittlejohn
Copy link

@peterlittlejohn peterlittlejohn commented Jan 7, 2020

This also happens with the bit64 library...

@nir4most
Copy link

@nir4most nir4most commented Jul 6, 2020

when bit64::: is in conflict with base::: , fn_fmls fails
There's a reliance on fn_fmls() which requires a function (and doesn't like 'primitive functions')

Also just a minor edit to say that this seems intended by rlang development:
r-lib/rlang#809 (comment)

library(bit64)
debug(conflicted:::is_superset)
library(conflicted)

#when bit64::`:` is in conflict with base::`:` , fn_fmls fails 
# 
# debug: args_base <- names(fn_fmls(base_obj))
# Browse[2]> n
# Error: package or namespace load failed for ‘conflicted’:
#   .onAttach failed in attachNamespace() for 'conflicted', details:
#   call: NULL
# error: `fn` must be an R function, not a primitive function
# In addition: Warning message:
#   package ‘conflicted’ was built under R version 3.6.3 

@emilBeBri
Copy link

@emilBeBri emilBeBri commented Oct 19, 2020

I also experience this issue with the bit64 package. How do you fix it?

@fdrennan
Copy link

@fdrennan fdrennan commented Apr 18, 2021

This error makes the package unusable for me almost at the start.

@njtierney
Copy link

@njtierney njtierney commented Aug 13, 2021

I've also experienced this - does anyone have a workaround?

@jtelleriar
Copy link

@jtelleriar jtelleriar commented Aug 13, 2021

@moodymudskipper
Copy link
Author

@moodymudskipper moodymudskipper commented Aug 13, 2021

If you put the first call below in your RProfile it seems to work ok :

setHook(
  packageEvent("conflicted", "attach"),
  function(...) {
    invisible(suppressMessages(trace(conflicted:::is_superset, quote(
        if(is.primitive(get(fun))) {
          # hacking the existing exception for "lag"
          pkg <- "dplyr"
          fun <- "lag"
        }
    ), print = FALSE)))
  }
)

library(conflicted)
#> Warning: package 'conflicted' was built under R version 4.0.5
library(inops)

x <- 1:5
x < 3 <- 0
#> Error: [conflicted] `<<-` found in 2 packages.
#> Declare a preference with `conflict_prefer()`:
#> * conflict_prefer("<<-", "inops")
#> * conflict_prefer("<<-", "base")

conflict_prefer("<<-", "base")
#> [conflicted] Will prefer base::`<<-` over any other package
x < 3 <- 0
#> Error in x < 3 <- 0: incorrect number of arguments to "<<-"

conflict_prefer("<<-", "inops")
#> [conflicted] Removing existing preference
#> [conflicted] Will prefer inops::`<<-` over any other package
x < 3 <- 0
x
#> [1] 0 0 3 4 5


# works with other packages mentionned in thread
suppressPackageStartupMessages({
  library(h2o)
  library(bit64)
})
#> Warning: package 'h2o' was built under R version 4.0.5
#> Warning: package 'bit64' was built under R version 4.0.3
#> Warning: package 'bit' was built under R version 4.0.3

`%*%`
#> Error: [conflicted] `%*%` found in 2 packages.
#> Declare a preference with `conflict_prefer()`:
#> * conflict_prefer("%*%", "h2o")
#> * conflict_prefer("%*%", "base")

`:`
#> Error: [conflicted] `:` found in 2 packages.
#> Declare a preference with `conflict_prefer()`:
#> * conflict_prefer(":", "bit64")
#> * conflict_prefer(":", "base")

Created on 2021-08-13 by the reprex package (v0.3.0)

@njtierney
Copy link

@njtierney njtierney commented Aug 13, 2021

Thanks, @moodymudskipper - I'll give that a go!

@njtierney
Copy link

@njtierney njtierney commented Aug 18, 2021

For reference, this happens in this situation:


author: njtierney
date: 2021-08-18
output:
reprex::reprex_document:
session_info: true
title: dopey-nyala_reprex.R

library(greta)
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
#>     %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#>     eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#>     tapply
library(conflicted)
#> Error: package or namespace load failed for 'conflicted':
#>  .onAttach failed in attachNamespace() for 'conflicted', details:
#>   call: NULL
#>   error: `fn` must be an R function, not a primitive function

Created on 2021-08-18 by the reprex package (v2.0.0.9000)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_AU.UTF-8                 
#>  ctype    en_AU.UTF-8                 
#>  tz       Australia/Perth             
#>  date     2021-08-18                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                           
#>  backports     1.2.1      2020-12-09 [1] CRAN (R 4.1.0)                   
#>  base64enc     0.1-3      2015-07-28 [1] CRAN (R 4.1.0)                   
#>  callr         3.7.0      2021-04-20 [1] CRAN (R 4.1.0)                   
#>  cli           3.0.0.9000 2021-07-05 [1] Github (r-lib/cli@82178a4)       
#>  coda          0.19-4     2020-09-30 [1] CRAN (R 4.1.0)                   
#>  codetools     0.2-18     2020-11-04 [1] CRAN (R 4.1.0)                   
#>  conflicted    1.0.4      2019-06-21 [1] CRAN (R 4.1.0)                   
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.1.0)                   
#>  digest        0.6.27     2020-10-24 [1] CRAN (R 4.1.0)                   
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)                   
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)                   
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.0)                   
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.1.0)                   
#>  future        1.21.0     2020-12-10 [1] CRAN (R 4.1.0)                   
#>  globals       0.14.0     2020-11-22 [1] CRAN (R 4.1.0)                   
#>  glue          1.4.2      2020-08-27 [1] CRAN (R 4.1.0)                   
#>  greta       * 0.3.1.9012 2021-08-06 [1] local                            
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)                   
#>  hms           1.1.0      2021-05-17 [1] CRAN (R 4.1.0)                   
#>  htmltools     0.5.1.1    2021-01-22 [1] CRAN (R 4.1.0)                   
#>  jsonlite      1.7.2      2020-12-09 [1] CRAN (R 4.1.0)                   
#>  knitr         1.33       2021-04-24 [1] CRAN (R 4.1.0)                   
#>  lattice       0.20-44    2021-05-02 [1] CRAN (R 4.1.0)                   
#>  lifecycle     1.0.0      2021-02-15 [1] CRAN (R 4.1.0)                   
#>  listenv       0.8.0      2019-12-05 [1] CRAN (R 4.1.0)                   
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.0)                   
#>  Matrix        1.3-4      2021-06-01 [1] CRAN (R 4.1.0)                   
#>  parallelly    1.26.1     2021-06-30 [1] CRAN (R 4.1.0)                   
#>  pillar        1.6.2      2021-07-29 [1] CRAN (R 4.1.0)                   
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)                   
#>  png           0.1-7      2013-12-03 [1] CRAN (R 4.1.0)                   
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.1.0)                   
#>  processx      3.5.2      2021-04-30 [1] CRAN (R 4.1.0)                   
#>  progress      1.2.2      2019-05-16 [1] CRAN (R 4.1.0)                   
#>  ps            1.6.0      2021-02-28 [1] CRAN (R 4.1.0)                   
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)                   
#>  R6            2.5.0      2020-10-28 [1] CRAN (R 4.1.0)                   
#>  Rcpp          1.0.7      2021-07-07 [1] CRAN (R 4.1.0)                   
#>  reprex        2.0.0.9000 2021-07-02 [1] Github (tidyverse/reprex@5cf02b4)
#>  reticulate    1.20       2021-05-03 [1] CRAN (R 4.1.0)                   
#>  rlang         0.4.11     2021-04-30 [1] CRAN (R 4.1.0)                   
#>  rmarkdown     2.9        2021-06-15 [1] CRAN (R 4.1.0)                   
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)                   
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.1.0)                   
#>  stringi       1.7.3      2021-07-16 [1] CRAN (R 4.1.0)                   
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)                   
#>  styler        1.4.1      2021-03-30 [1] CRAN (R 4.1.0)                   
#>  tensorflow    2.5.0      2021-06-10 [1] CRAN (R 4.1.0)                   
#>  tfruns        1.5.0      2021-02-26 [1] CRAN (R 4.1.0)                   
#>  tibble        3.1.3      2021-07-23 [1] CRAN (R 4.1.0)                   
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)                   
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)                   
#>  whisker       0.4        2019-08-28 [1] CRAN (R 4.1.0)                   
#>  withr         2.4.2      2021-04-18 [1] CRAN (R 4.1.0)                   
#>  xfun          0.24       2021-06-15 [1] CRAN (R 4.1.0)                   
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.1.0)                   
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

vs the opposite order


author: njtierney
date: 2021-08-18
output:
reprex::reprex_document:
session_info: true
title: next-esok_reprex.R

library(conflicted)
library(greta)
#> Error: `fn` must be an R function, not a primitive function

Created on 2021-08-18 by the reprex package (v2.0.0.9000)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_AU.UTF-8                 
#>  ctype    en_AU.UTF-8                 
#>  tz       Australia/Perth             
#>  date     2021-08-18                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                           
#>  backports     1.2.1      2020-12-09 [1] CRAN (R 4.1.0)                   
#>  base64enc     0.1-3      2015-07-28 [1] CRAN (R 4.1.0)                   
#>  cachem        1.0.5      2021-05-15 [1] CRAN (R 4.1.0)                   
#>  callr         3.7.0      2021-04-20 [1] CRAN (R 4.1.0)                   
#>  cli           3.0.0.9000 2021-07-05 [1] Github (r-lib/cli@82178a4)       
#>  coda          0.19-4     2020-09-30 [1] CRAN (R 4.1.0)                   
#>  codetools     0.2-18     2020-11-04 [1] CRAN (R 4.1.0)                   
#>  conflicted  * 1.0.4      2019-06-21 [1] CRAN (R 4.1.0)                   
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.1.0)                   
#>  digest        0.6.27     2020-10-24 [1] CRAN (R 4.1.0)                   
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)                   
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)                   
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.0)                   
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)                   
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.1.0)                   
#>  future        1.21.0     2020-12-10 [1] CRAN (R 4.1.0)                   
#>  globals       0.14.0     2020-11-22 [1] CRAN (R 4.1.0)                   
#>  glue          1.4.2      2020-08-27 [1] CRAN (R 4.1.0)                   
#>  greta       * 0.3.1.9012 2021-08-06 [1] local                            
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)                   
#>  hms           1.1.0      2021-05-17 [1] CRAN (R 4.1.0)                   
#>  htmltools     0.5.1.1    2021-01-22 [1] CRAN (R 4.1.0)                   
#>  jsonlite      1.7.2      2020-12-09 [1] CRAN (R 4.1.0)                   
#>  knitr         1.33       2021-04-24 [1] CRAN (R 4.1.0)                   
#>  lattice       0.20-44    2021-05-02 [1] CRAN (R 4.1.0)                   
#>  lifecycle     1.0.0      2021-02-15 [1] CRAN (R 4.1.0)                   
#>  listenv       0.8.0      2019-12-05 [1] CRAN (R 4.1.0)                   
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.0)                   
#>  Matrix        1.3-4      2021-06-01 [1] CRAN (R 4.1.0)                   
#>  parallelly    1.26.1     2021-06-30 [1] CRAN (R 4.1.0)                   
#>  pillar        1.6.2      2021-07-29 [1] CRAN (R 4.1.0)                   
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)                   
#>  png           0.1-7      2013-12-03 [1] CRAN (R 4.1.0)                   
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.1.0)                   
#>  processx      3.5.2      2021-04-30 [1] CRAN (R 4.1.0)                   
#>  progress      1.2.2      2019-05-16 [1] CRAN (R 4.1.0)                   
#>  ps            1.6.0      2021-02-28 [1] CRAN (R 4.1.0)                   
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)                   
#>  R6            2.5.0      2020-10-28 [1] CRAN (R 4.1.0)                   
#>  Rcpp          1.0.7      2021-07-07 [1] CRAN (R 4.1.0)                   
#>  reprex        2.0.0.9000 2021-07-02 [1] Github (tidyverse/reprex@5cf02b4)
#>  reticulate    1.20       2021-05-03 [1] CRAN (R 4.1.0)                   
#>  rlang         0.4.11     2021-04-30 [1] CRAN (R 4.1.0)                   
#>  rmarkdown     2.9        2021-06-15 [1] CRAN (R 4.1.0)                   
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)                   
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.1.0)                   
#>  stringi       1.7.3      2021-07-16 [1] CRAN (R 4.1.0)                   
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)                   
#>  styler        1.4.1      2021-03-30 [1] CRAN (R 4.1.0)                   
#>  tensorflow    2.5.0      2021-06-10 [1] CRAN (R 4.1.0)                   
#>  tfruns        1.5.0      2021-02-26 [1] CRAN (R 4.1.0)                   
#>  tibble        3.1.3      2021-07-23 [1] CRAN (R 4.1.0)                   
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)                   
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)                   
#>  whisker       0.4        2019-08-28 [1] CRAN (R 4.1.0)                   
#>  withr         2.4.2      2021-04-18 [1] CRAN (R 4.1.0)                   
#>  xfun          0.24       2021-06-15 [1] CRAN (R 4.1.0)                   
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.1.0)                   
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

@njtierney
Copy link

@njtierney njtierney commented Aug 30, 2021

@moodymudskipper

Unfortunately this doesn't prevent my error :( - I tried having a local .Rprofile for my project with the code:

This is really unfortunate, as I need to use greta in my work - and the conflicted package is now a key part of my workflow, and I now need to remove conflicted from my work, which means developing a workaround - which is unfortunately not trivial.

@njtierney
Copy link

@njtierney njtierney commented Aug 30, 2021

I got this to work by adding warn.conflicts = FALSE like so:

library(conflicted)
library(greta, warn.conflicts = FALSE)

@njtierney
Copy link

@njtierney njtierney commented Aug 30, 2021

Ah, for this to work, I needed the following in my local Rprofile:

setHook(
  packageEvent("conflicted", "attach"),
  function(...) {
    invisible(suppressMessages(trace(conflicted:::is_superset, quote(
      if(is.primitive(get(fun))) {
        # hacking the existing exception for "lag"
        pkg <- "dplyr"
        fun <- "lag"
      }
    ), print = FALSE)))
  }
)

library(conflicted)

@njtierney
Copy link

@njtierney njtierney commented Nov 15, 2021

Hi there @hadley / @batpigandme

Sorry to bother you here, but I was just wondering if you have a recommended workaround for this or plans to fix?

It currently means we can't really use conflicted with some projects if we use certain packages.

This introduces a pretty huge amount of friction, as we came to really depend on conflicted, and to switch back to life pre-conflicted is very challenging.

@hadley
Copy link
Member

@hadley hadley commented Nov 16, 2021

Just install #59 as a work around?

@hadley hadley closed this as completed in 56347c1 Nov 16, 2021
@njtierney
Copy link

@njtierney njtierney commented Nov 16, 2021

Thanks, Hadley! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants