36

I have an issue when developing an R project using RStudio. I create an object, and then realise later, that I want to give it another name. I then have to manually change the name, which in larger projects is annoying and often results in errors, as I easily oversee one line. The replace all function of RStudio doesn't quite match the name I am trying to replace, as it only does so in one file, it also doesn't consider only the references of the variable see sample code:

f <- function(a){
    b <- a
return(a+b)
}
a <- 5;
a <-  a + f(1)

In that sample, I'd like to rename a only inside the function. I'd have to do that thrice, while replace all would mess up my code outside the function. I don't want to edit that a. I.e. Visual Studio has an option of renaming a variable using the hotkey: Ctrl + .. Then Visual Studio renames the variable and its references in the entire project, without editing equally named variables that don't have a reference to the edited one. I haven't been able to find an option like that in RStudio. Is there an equivalent?

David Go
  • 740
  • 1
  • 6
  • 13
  • 1
    I suppose you're looking for the `Whole word` checkbox right under the search field, this will ensure that only a single `a` is a match, but it also works if the word is close to symbols. E.g. it matches `a – Molx Dec 13 '15 at 14:12
  • 1
    A similar question has already been asked: http://stackoverflow.com/questions/14688446/search-all-open-files-in-r-studio – Raad Dec 13 '15 at 14:15
  • I don't search for a `Whole word` option nor a functionality that navigates through the entire project. The functionality I'm looking for is something that automatically searches for references of a variable and substitutes just them - not an identically named variable. Both of your suggestions would'nt work in my example where I only want to rename the `a` within the function `c`. – David Go Dec 13 '15 at 14:44
  • Would you like a unicorn with that? – Roland Dec 13 '15 at 14:53
  • Remark to *common in other programming languages*: it is not a matter of the programming language, but a matter of the development enviroment (=DE). Which DE you used for other programming languages? Eventually you can use it also for R. – jogo Dec 13 '15 at 15:13
  • Judging from your answer I can imagine that RStudio isn't capable of doing a reference based variable search, is that the case? That would already answer my question. – David Go Dec 13 '15 at 15:14
  • You are totatlly right. Such a search depends on the IDE the one I use in C# is Visual Studio 2013 which doesn't support R – David Go Dec 13 '15 at 15:17
  • I don't know if RStudio can. I never needed such a feature. – jogo Dec 13 '15 at 15:19
  • It might be worth mentioning that using separate files for each function would remedy this issue for variable scope (within a function). This would also make it easier to bring your code into an R package at some later date. – Jonathan Lisic Dec 13 '15 at 15:21
  • @JonathanLisic good point! however, I usually create my classes in one file. Using for each of the included functions a seperate file would make it harder to oversee dependencies within the code/project in my opinion. – David Go Dec 13 '15 at 15:30
  • Would be nice to see this as a [`rename refactoring`](http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_rename.htm) option, as is available in IDEs like Eclipse – Megatron Jun 17 '16 at 15:12

1 Answers1

55

RStudio IDE v1.0 includes a feature called "Rename in scope" that aims to do this:

This feature makes it easy to rename all instances of a variable. The tool is context aware; changing m to m1 won’t change mtcars to m1tcars.

RStudio Rename in Scope animated GIF

I cannot find documentation for the feature. The example from the animated GIF works though when I place the cursor on the first instance of d (the variable name to replace), and then select Code -> Rename in Scope. However, when I try the same steps but starting from the second instance, it does not work. So apparently you need to start from the place where the variable is assigned?

## Example from animated GIF
library(dplyr)
library(magrittr)
library(ggplot2)

d <- mtcars %>%                      ## Instance 1
  filter(cyl > 4) %>%
  select(hp, mpg)

ggplot(data = d, aes(x=hp, y=mpg)) + ## Instance 2
  geom_point() +
  geom_smooth()

In practice, there still seem to be bugs that prevent the feature from working. For instance, the example below does not work unless the header is removed.

## Header ####
example <- 1:10
example[1]
dnlbrky
  • 7,922
  • 2
  • 47
  • 59
  • 1
    Trying to get this feature to work led me to this question. Has anybody been able to find documentation on how to use this new feature? – BLT Nov 19 '16 at 10:39
  • 5
    Thank you for the answer, I find the feature (potentionally) quite useful. However, it is limited. Appart from the issue you described I find it particularly problematic that the scope is limited to one file. If I i.e. use the feature to edit the name of a variable of an Object which is defined in another file, the name will be changed only in the chosen script. – David Go Dec 20 '16 at 15:48