Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Objects in RStudio context sensitive within entire Project

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?

like image 563
David Go Avatar asked Dec 13 '15 14:12

David Go


People also ask

How do I change a variable name everywhere in R?

You can rename all instances of a variable name by highlighting one instance of the variable name and then using Code > Rename in Scope .

How do I change multiple variable names in Rstudio?

It is achieved by selecting the function or variable we want to change and pressing Ctrl + Shift + Alt + M. It will select all occurrences in scope, you will have to just type a new name.

How do you rename untitled in R?

To rename a file in R, use the file. rename() function. The file. rename() method will rename files, and it can take a vector of both from and to names.


1 Answers

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] 
like image 144
dnlbrky Avatar answered Oct 09 '22 22:10

dnlbrky