Rstudio: "Rename (symbol) in scope" mixes variable & function names

Created on 13 Jan 2018  Â·  1Comment  Â·  Source: rstudio/rstudio

Possible bug: Shift+Alt+Cmd+M selects both variables and function names, if they are the same. However, it could be safer to not select (base) functions when Rename… is triggered on a user's variable, and vice versa.

Minimal Example:

msg <- "A multi-purpose part of a message"
message(paste("Bla Bla", msg")
write(paste("#", msg, file =…))

Steps to reproduce

  1. select msg
  2. press Shift+Alt+Cmd+M
  3. rename to message (admittedly, this might be bad practice)
  4. press Shift+Alt+Cmd+M again
    => expected: only instance of message variable (previously msg) get selected
    => actual: message() function also gets selected and may thus accidentally renamed

If this is not considered a bug, please consider this a discussion whether Rename… should be made more context-aware by restricting the multi-selection to either variables or function, depending on which type is selected.

About RStudio

Version 1.1.383 – © 2009-2017 RStudio, Inc.
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko)

> sessionInfo()

R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.13.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/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] testthat_2.0.0 magrittr_1.5   usethis_1.1.0 

loaded via a namespace (and not attached):
[1] compiler_3.4.0  backports_1.1.2 R6_2.2.2        rsconnect_0.8.5
[5] rprojroot_1.3-2 tools_3.4.0     yaml_2.1.16     rlang_0.1.6
bug refactoring

Most helpful comment

Thanks for the bug report! I can reproduce this; you are correct that the 'rename in scope' tool doesn't currently understand the difference between variable and function scoping. This is a somewhat tricky problem to solve in general, since doing this correctly requires knowledge of the actual type of the variable being renamed and we don't always have that information. For example:

foo <- something_complicated()
foo(x)

In the above example, it is tough for us to know statically whether the function something_complicated() will return a variable or a function. We could probably implement some handling for the simple cases (where foo is assigned as literal or similar) but anything involving a function call makes this task much harder to get right.

Nonetheless I agree it would be worth attempting to catch these simpler cases!

>All comments

Thanks for the bug report! I can reproduce this; you are correct that the 'rename in scope' tool doesn't currently understand the difference between variable and function scoping. This is a somewhat tricky problem to solve in general, since doing this correctly requires knowledge of the actual type of the variable being renamed and we don't always have that information. For example:

foo <- something_complicated()
foo(x)

In the above example, it is tough for us to know statically whether the function something_complicated() will return a variable or a function. We could probably implement some handling for the simple cases (where foo is assigned as literal or similar) but anything involving a function call makes this task much harder to get right.

Nonetheless I agree it would be worth attempting to catch these simpler cases!

Was this page helpful?
0 / 5 - 0 ratings