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

Jeremie Juste jerem|eju@te @end|ng |rom gm@||@com
Wed May 1 09:42:35 CEST 2019


Hello

I can buy you this feature for a one liner
data[ data[,"columnB"] > 123 ,"columnA"] <- gsub("xxx", "yyy",_POINT_
,fixed=TRUE)

To do this you can use the function  r/copy-left-of-assign-r-fun-end
but you will have to load the function r/get-begin-of-line-point as
well (see below)

I have also included r/eval-no-assign-r-fun-end which evaluate an
expression at the right of the assign operator. It is quit handy


(defun r/get-begin-of-line-point ()
  (interactive)
  (save-excursion
    (beginning-of-line)
     (point)))


(defun r/eval-no-assign-r-fun-end ()
  "eval expression right of <-"
  (interactive)
  (setq begin (point))  
  (save-excursion     
    (re-search-backward "<- " (r/get-begin-of-line-point))
    (forward-char 3)
    (setq end (point)))
  (ess-eval-region begin end 4)
  )

(defun r/copy-left-of-assign-r-fun-end ()
  "kill-ring-save expression  left of <- and yank it at point"
  (interactive)
  (save-excursion     
    (re-search-backward "<- " (r/get-begin-of-line-point))
    (setq begin (point))
    (kill-ring-save begin (r/get-begin-of-line-point))
    )
  (yank)
    )

I would advise using data.table though, which is less verbose and
quicker most of the time

library(data.table)
setDT(data)
data[columnB > 123, columnA:= gsub("xxx", "yyy", _POINT_, fixed=TRUE)]

Hope this helps,

Jeremie




Sven Hartenstein via ESS-help <ess-help using r-project.org> writes:

> 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?
>
> Or is my coding process unusual and you are not in the situation to use
> such a function?
>
> What do you think?
>
> (I am not very familiar with lisp and thus do not try to write such a
> function. I might use a macro.)
>
> Thanks,
>
> Sven
>
> ______________________________________________
> ESS-help using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/ess-help



More information about the ESS-help mailing list