conflicted icon indicating copy to clipboard operation
conflicted copied to clipboard

Informative error for conflicted functions intermittently not working in RStudio

Open uhkeller opened this issue 2 years ago • 8 comments

conflicted sometimes behaves very strangely when blocks of code are run from a script editor in RStudio or entered in the RStudio console.

Sometimes calls to "conflicted" functions with more than one argument do not produce the intended error with instructions on how to resolve the conflict, but an error complaining about unused arguments (error in … : unused argument …). Once this has happened it will happen on all similar calls until library() is called (not matter with which package or whether the package is already loaded). Also, entering the name of the conflicted function will print the function definition in the "conflicted" environment rather than throwing the error informing about the conflict.

I have tried to reproduce this with R running in a terminal but couldn't. Since this seems to happen only in RStudio, and only when running parts of scripts interactively, I have uploaded a short video showing this behavior. The script I used follows, but again this only causes problems when individual lines are executed in RStudio as shown in the video.

library(conflicted)
library(plyr)
library(dplyr)
library(glue)

rename
rename(iris, x = Species)
rename(iris)

In the video the strange behavior is always triggered by running the two-argument call to rename() in the console, but I've also seen it being triggered from a script.

I'd be happy of course to help investigating if you can't reproduce this, though I expect it will be reproducible as I've seen this on multiple (Mac and Windows) machines, with different versions of R and RStudio.

https://user-images.githubusercontent.com/2323704/194540717-0f7a3ed4-9041-44e1-863b-466b6af496e4.mp4

uhkeller avatar Oct 07 '22 11:10 uhkeller

I can't reproduce this and I can't even imagine what might cause it which is going to make it hard to fix. Do you have any other packages loaded?

hadley avatar Oct 08 '22 20:10 hadley

No, just the ones in the script. This is before running it:

> sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin21.6.0 (64-bit)
Running under: macOS Monterey 12.6

Matrix products: default
LAPACK: /opt/homebrew/Cellar/r/4.2.1_4/lib/R/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

loaded via a namespace (and not attached):
 [1] compiler_4.2.1  fastmap_1.1.0   cli_3.4.1       htmltools_0.5.3 tools_4.2.1     rstudioapi_0.14
 [7] yaml_2.3.5      rmarkdown_2.16  knitr_1.40      xfun_0.33       digest_0.6.29   rlang_1.0.6    
[13] evaluate_0.16 

And this is after:

> sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin21.6.0 (64-bit)
Running under: macOS Monterey 12.6

Matrix products: default
LAPACK: /opt/homebrew/Cellar/r/4.2.1_4/lib/R/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] glue_1.6.2       dplyr_1.0.10     plyr_1.8.7       conflicted_1.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9       rstudioapi_0.14  knitr_1.40       magrittr_2.0.3   tidyselect_1.1.2 R6_2.5.1        
 [7] rlang_1.0.6      fastmap_1.1.0    fansi_1.0.3      tools_4.2.1      xfun_0.33        utf8_1.2.2      
[13] DBI_1.1.3        cli_3.4.1        htmltools_0.5.3  assertthat_0.2.1 yaml_2.3.5       digest_0.6.29   
[19] tibble_3.1.8     lifecycle_1.0.2  crayon_1.5.2     purrr_0.3.4      vctrs_0.4.2      memoise_2.0.1   
[25] cachem_1.0.6     evaluate_0.16    rmarkdown_2.16   compiler_4.2.1   pillar_1.8.1     generics_0.1.3  
[31] pkgconfig_2.0.3

uhkeller avatar Oct 10 '22 13:10 uhkeller

I have the same version of R. What version of RStudio do you have?

hadley avatar Oct 10 '22 15:10 hadley

The current release, 2022.07.2 Build 576.

uhkeller avatar Oct 10 '22 16:10 uhkeller

I’m using the daily. But that probably doesn’t make a difference. Unfortunately there’s not much I can do since I can’t recreate the problem and no one else has reported it.

hadley avatar Oct 10 '22 21:10 hadley

That's strange. I have now tried this on a colleague's machine that uses a different OS (Windows 10), a different R version (4.2.0), and a different RStudio version (2022.07.1 Build 554) and can reproduce it exactly with no other packages loaded.

I'll try to describe the most simple approach I found to triggering this:

In a new project in RStudio, put these lines in a new R script and run them by selecting them and clicking Run:

library(conflicted)
library(plyr)
library(dplyr)
rename(iris, x = Species)

The informative error message appears.

Error:
! [conflicted] `rename` found in 2 packages.
Either pick the one you want with `::` 
* dplyr::rename
* plyr::rename
Or declare a preference with `conflict_prefer()`
* conflict_prefer("rename", "dplyr")
* conflict_prefer("rename", "plyr")

Now copy the last line, paste it into the console and hit enter. The "unused argument" error message appears.

> rename(iris, x = Species)
Error in rename(iris, x = Species) : unused argument (x = Species)

Here's the sessionInfo() from my colleague's machine:

R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_Europe.utf8  LC_CTYPE=English_Europe.utf8    LC_MONETARY=English_Europe.utf8 LC_NUMERIC=C                    LC_TIME=English_Europe.utf8    

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

other attached packages:
[1] glue_1.6.2       dplyr_1.0.9      plyr_1.8.7       conflicted_1.1.0

loaded via a namespace (and not attached):
[1] Rcpp_1.0.8.3      rstudioapi_0.13  magrittr_2.0.3   tidyselect_1.1.2 R6_2.5.1         rlang_1.0.2      fastmap_1.1.0    fansi_1.0.3      tools_4.2.0      utf8_1.2.2      
[11] cli_3.3.0        DBI_1.1.2        ellipsis_0.3.2   assertthat_0.2.1 tibble_3.1.7     lifecycle_1.0.1  crayon_1.5.1     purrr_0.3.4      vctrs_0.4.1      memoise_2.0.1   
[21] cachem_1.0.6     compiler_4.2.0   pillar_1.7.0     generics_0.1.2   pkgconfig_2.0.3

uhkeller avatar Oct 11 '22 10:10 uhkeller

@uhkeller do you see it every time? Like can you reliably reproduce multiple times in a session?

hadley avatar Oct 11 '22 16:10 hadley

Yes. Each call to library() restores the informative error, but running rename(iris, x = Species) in the console reverts to the "unused argument" error 100% of the time. Running the same command from the script reverts around 50% of the time.

uhkeller avatar Oct 11 '22 18:10 uhkeller

Ok, I'm now seeing this intermittently too.

hadley avatar Oct 24 '22 18:10 hadley

With some help from @kevinushey it looks like this is probably due to this IDE code: https://github.com/rstudio/rstudio/blob/1675d5d7a716a464208179336d3a99b1aaae90e4/src/cpp/session/modules/SessionDiagnostics.cpp#L843-L872

Which breaks conflicted because attach() loses "active binding"ness.

hadley avatar Oct 24 '22 19:10 hadley

Should hopefully be fixed in the next daily builds; the associated fix will also be part of the upcoming IDE release (currently slated for December sometime)

kevinushey avatar Oct 24 '22 20:10 kevinushey

Thanks! Very happy to confirm that I don't manage to trigger the error any more with yesterday's daily build of RStudio.

uhkeller avatar Nov 04 '22 05:11 uhkeller

@uhkeller thanks for reporting this mysterious bug!

hadley avatar Nov 04 '22 12:11 hadley