1

Is there any ess function that does the following? Assume you have the line in Emacs like this

1 2 3 4

then once selected (e.g., using the mouse), it is transformed into

c(1,2,3,4)

It would be a wonderful tool. I copy and paste stuff that I transform into vectors quite often. Also, it would be great to have a function that if you select

A B C D E 

it produces

"A", "B", "C", "D", "E"
PinkCollins
  • 191
  • 9

1 Answers1

2

Not a solution based on ess, but both of these are relatively simple to implement using Elisp.

For your first example, that is, 1 2 3 4 => c(1,2,3,4): use the Elisp functions format and replace-regexp-in-string to convert your data as text using a regular expression.

Put this Elisp command somewhere in your init.el:

(defun my-command (start end)
  (interactive "r")
  (insert
    (prog1
      (format "c(%s)"
        (replace-regexp-in-string 
          "\\([[:digit:]]+\\) " 
          "\\1," 
          (buffer-substring-no-properties start end)))
      (delete-region start end))))

Mark a space-separated sequence of numbers, then invoke it with M-x my-command RET, or bind my-command to the key combination of your choice for easy access.

Phil Hudson
  • 1,741
  • 10
  • 13