[ESS] Feature idea: insert expression before <- at point

Alex Branham @|ex@br@nh@m @end|ng |rom gm@||@com
Tue Apr 30 22:28:35 CEST 2019


On Tue 30 Apr 2019 at 13:25, Sven Hartenstein via ESS-help <ess-help using r-project.org> wrote:

> Dear ESS users and developers,
>
> when writing R code to manipulate an object or data frame column, I
> often find myself retyping the expression on the left side of "<-" as
> some argument for a function call or assignment on the right side of
> "<-".
>
> Here are two examples. Imagine your point is at _POINT_ and you want to
> insert 'data[,"columnA"]' in the first example and in the second example
> 'data[ data[,"columnB"] < 123 ,"columnA"]' at point.
>
> data[,"columnA"] <- tolower(_POINT_)
>
> data[ data[,"columnB"] > 123 ,"columnA"] <- gsub("xxx",
>                                                   "yyy",
>                                                   _POINT_,
>                                                   fixed=TRUE)
>
> Wouldn't it be handy to have a lisp function which copies the expression
> on the left side of "<-" and inserts it at point?
>
> Or is something like this already available in ESS?

Not to my knowledge, no.

> What do you think?

I don't think it's a terrible idea; I've found myself wanting to do that
several times in the past (though less so now with magrittr pipes).

I guess implementation-wise the tricky bit would be figuring out what to
do in the case of 1) more than one assignment e.g. x <- y <- 2 and also
2) how to find the start of the "left" side. It would be tricky to
differentiate between:


x[[
  1]] <- 1

and

x <- 1
y <- 2

since we can't reliably detect complete R, especially not backwards. I
guess one quick workaround would be to work with indentation --- just to
take all the lines starting with whitespace before the <- until we find
one all-whitespace line or the first line that doesn't start with
whitespace.

Thanks for the suggestion,
Alex



More information about the ESS-help mailing list