重命名整个项目中对RStudio上下文敏感的对象 [英] Renaming Objects in RStudio context sensitive within entire Project

查看:177
本文介绍了重命名整个项目中对RStudio上下文敏感的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用RStudio开发R项目时遇到问题.我创建了一个对象,然后稍后意识到,我想给它起另一个名字.然后,我不得不手动更改名称,这在较大的项目中很烦人,并且经常会导致错误,因为我很容易看到一行内容. RStudio的replace all函数与我要替换的名称不太匹配,因为它仅在一个文件中匹配,也不考虑变量的引用,请参见示例代码:

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)

在该示例中,我想在函数内仅重命名a .我必须做三次,而replace all会使我的代码外部混乱.我不想编辑该a. IE. Visual Studio可以使用热键Ctrl + .重命名变量.然后,Visual Studio在整个项目中重命名该变量及其引用,无需编辑同名的变量,这些变量没有对已编辑变量的引用.我无法在RStudio中找到类似的选项.有等同的东西吗?

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?

推荐答案

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

此功能使重命名变量的所有实例变得容易.这 工具具有上下文意识;将m更改为m1不会将mtcars更改为 m1tcars.

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.

我找不到该功能的文档.尽管当我将光标放在d的第一个实例(要替换的变量名称)上,然后选择Code-> Rename in Scope时,动画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]

这篇关于重命名整个项目中对RStudio上下文敏感的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆