Auto-completion using « with() » environment


#1

When using the function « with() » the auto-completion doesn’t work for variable names. I find the “with()” environment convenient to avoid problems with the “attach()” function and avoid having to refer to the data each time I’m calling a variable.

Is there a way for the auto-completion to work in the with function or another workaround the “attach()” function ? What would be of “good practice” in this case ?

Thank you


#2

What are you trying to achieve? I don’t think that attach() is a recommended “good practice” if you can avoid it.


#3

You’re right that RStudio currently doesn’t understand how to provide autocompletions within e.g. with() or within(). We’re tracking this here currently:


#4

Thank you @kevinushey, this answers my question.

@jim89 To answer you question. I’m trying to evaluate expressions without having to call a data.frame each time and still have the convenience of the auto-completion that rstudio offers. cf the exemple below using cars:

Exemple :
#1 - with with()
x <- with(cars, speed + dist)

#2 - with attach
attach(cars)
x <- speed + dist
detach(cars)

#3 - base R
x <- cars$speed + cars$dist

  • In the first exemple the auto-completion doesnt work so it can be a bit tedious with long variables
  • In the second exemple the auto-completion does work but it’s hard to write and read with the name of the data being repeted each time
  • In the third exemple, the auto-completion works and it is easy to read but it’s usually considered bad practice to use “attach()” and can lead to mistakes.