conflicted icon indicating copy to clipboard operation
conflicted copied to clipboard

conflicts from user-defined functions

Open asadow opened this issue 9 months ago • 4 comments

I'm advising a reader to be wary of over-writing existing functions. I expected conflicted to act as a sort of pro-active solution, but I noticed that it only warns for conflicts between base R and packages. Would it be of interest to have it error for user-defined functions?

library(conflicted)
labels("test")
#> [1] "1"
labels <- function(x) names(x)
labels("test")
#> NULL

filter <- function(x) x[1,]
filter(mtcars)
#>           mpg cyl disp  hp drat   wt  qsec vs am gear carb
#> Mazda RX4  21   6  160 110  3.9 2.62 16.46  0  1    4    4

Created on 2025-03-06 with reprex v2.1.1

asadow avatar Mar 06 '25 14:03 asadow

I have a similar ask. We have a set folders we source in the contents of for users at session start-up, and there can be conflicts in these folders. Conflicted for source() would help me find the conflicts and program in a rule to declare the session wide preferences based on the conflicts found.

nicholas-masel avatar Jul 09 '25 17:07 nicholas-masel

Not clear to me how we could trigger conflicted to look for conflicts in this context — it's registers conflicts when you run library() or similar. When would we trigger the search here? We could possibly use addTaskCallback() to register a top-level task callback, but that would have to be opt-in because it's potentially slow. I guess we could also shim <- like we do with library, but I worry about the unanticipated consequences of shimming such a frequently used function.

hadley avatar Jul 09 '25 20:07 hadley

library(fastcluster)
library(tidyverse)


hclust <- function(x){
  cat("fjeoijfoef",x)
}

library(conflicted)

hc <- hclust(dist(USArrests), "ave")

Why the hclust() not show confilicts? Is this because is it is on the "stat" package?

> getAnywhere("hclust")
3 differing objects matching ‘hclust’ were found
in the following places
  .GlobalEnv
  package:fastcluster
  package:stats
  namespace:stats
  namespace:fastcluster
Use [] to view one of them

yudalang3 avatar Nov 10 '25 11:11 yudalang3

I think it is a realy bug:

> find("cor")
[1] ".conflicts"        "package:S4Vectors" "package:WGCNA"     "package:stats"    
> find("hclust")
[1] "package:fastcluster" "package:stats"      
> cor
Error:
! [conflicted] cor found in 3 packages.
Either pick the one you want with `::`:
• S4Vectors::cor
• WGCNA::cor
• stats::cor
Or declare a preference with `conflicts_prefer()`:
• conflicts_prefer(S4Vectors::cor)
• conflicts_prefer(WGCNA::cor)
• conflicts_prefer(stats::cor)
Run `rlang::last_trace()` to see where the error occurred.
> hclust
function (d, method = "complete", members = NULL) 
{
    if (method == "ward") {
        message("The \"ward\" method has been renamed to \"ward.D\"; note new \"ward.D2\"")
        method <- "ward.D"
    }
    METHODS <- c("single", "complete", "average", "mcquitty", 
        "ward.D", "centroid", "median", "ward.D2")
    method <- pmatch(method, METHODS)
    if (is.na(method)) 
        stop("Invalid clustering method.")
    if (method == -1) 
        stop("Ambiguous clustering method.")
    dendrogram <- c(.Call(fastcluster, attr(d, "Size"), method, 
        d, members), list(labels = attr(d, "Labels"), method = METHODS[method], 
        call = match.call(), dist.method = attr(d, "method")))
    class(dendrogram) <- "hclust"
    return(dendrogram)
}
<bytecode: 0x0000019932bbfd88>
<environment: namespace:fastcluster>

yudalang3 avatar Nov 10 '25 11:11 yudalang3