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 when used with memoise #45

Closed
rorynolan opened this issue Jul 23, 2019 · 3 comments
Closed

Error when used with memoise #45

rorynolan opened this issue Jul 23, 2019 · 3 comments

Comments

@rorynolan
Copy link

rorynolan commented Jul 23, 2019

I can't really tell what's going on here, but I'm getting an error about setdiff() when I haven't used it at all.

library(conflicted)
library(tidyverse)
library(lubridate)
library(memoise)

tib <- tibble(x = 1:2, y = 3:4)

f <- function(tib) {
  mutate(tib, z = if_else(x == 1, y, x))
}
mf <- memoise(f)

f(tib)
#> # A tibble: 2 x 3
#>       x     y     z
#>   <int> <int> <int>
#> 1     1     3     3
#> 2     2     4     2

mf(tib)
#> [conflicted] `setdiff` found in 2 packages.
#> Either pick the one you want with `::` 
#> * lubridate::setdiff
#> * dplyr::setdiff
#> Or declare a preference with `conflict_prefer()`
#> * conflict_prefer("setdiff", "lubridate")
#> * conflict_prefer("setdiff", "dplyr")

Created on 2019-07-23 by the reprex package (v0.3.0)

@GegznaV
Copy link

GegznaV commented Jul 29, 2019

I also faced a similar issue, when I had to resolve a conflict related to setdiff(), when I did not use it. But I think @rorynolan's example is reproducible enough.

Furthermore, this issue seems to be related: #44

@moodymudskipper
Copy link

moodymudskipper commented Nov 21, 2019

You are using setdiff because it's part of the body of mf (check body(mf)) and mf is not namespaced so the use of setdiff is indeed ambiguous.

It might be a memoise issue, and maybe we should use the :: notation everywhere in manufactured functions as a good practice ? This won't help with overloaded operators though.

We can fix this specific case by substituting setdiff with base::setdiff in the manufacture function's body.

library(conflicted)
library(tidyverse)
library(lubridate)
library(memoise)

tib <- tibble(x = 1:2, y = 3:4)

f <- function(tib) {
  mutate(tib, z = if_else(x == 1, y, x))
}
mf <- memoise(f)
body(mf) <- do.call(substitute, list(body(mf), list(setdiff = quote(base::setdiff))))
class(mf) <- c("memoised", "function") # seems that `body<-` strips off the class :(

f(tib)
#> # A tibble: 2 x 3
#>       x     y     z
#>   <int> <int> <int>
#> 1     1     3     3
#> 2     2     4     2

mf(tib)
#> # A tibble: 2 x 3
#>       x     y     z
#>   <int> <int> <int>
#> 1     1     3     3
#> 2     2     4     2

Created on 2019-11-21 by the reprex package (v0.3.0)

@hadley
Copy link
Member

hadley commented Nov 16, 2021

This is now resolved because lubridate and dplyr use the same definition of setdiff(), but I think this is the correct behaviour and reveals a real issue in memoise.

@hadley hadley closed this as completed Nov 16, 2021
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

4 participants