Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a hack to be able to run the current line or selection in RStudio without moving the cursor?

Tags:

r

rstudio

UPDATE (April 2013): As per answer below, RStudio no longer jumps cursor on selection.


I'm running RStudio 0.97.168.

I like to use the script editor in RStudio like a console. Thus, I run a line of code and then edit it a little bit and re-run it. I often also explore objects by selecting some of the code and running the selection and then progressively altering the selection. At present RStudio always moves the cursor after running a line of code. The cursor can move to a variety of places. Typically the cursor moves to the next line of R code, but depending on the context, it could move to the end of the code block or the next line. It's really frustrating having to constantly move the cursor back to where I want it.

While I often appreciate the default cursor movement behaviour, I'd like to have the option to run the selection or the current line without the cursor moving. I've raised this as a suggestion on RStudio support.

I'd like to be able to have a shortcut key like "Cmd+Alt+Enter" that runs the current line or selection and does not move the cursor in the script editor. I realise that this is not currently supported, but I was wondering whether there might be some creative hack that could enable the cursor not to move after running a command or even a patch or perhaps some sort of external macro.

like image 987
Jeromy Anglim Avatar asked Nov 28 '12 04:11

Jeromy Anglim


People also ask

How do I run a current line in R?

Shortcuts to run R codeCtrl + Enter – Runs the current line and jumps to the next one, or runs the selected part without jumping further. Alt + Enter – Allows running code without moving the cursor to the next line if you want to run one line of code multiple times without selecting it.

How do I run a specific line in RStudio?

Executing a Single Line To execute the line of source code where the cursor currently resides you press the Ctrl+Enter key (or use the Run toolbar button): After executing the line of code, RStudio automatically advances the cursor to the next line.

How do I run a section of R code?

To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter. In the next figure data was read, and a histogram created, with the lessR Histogram function.

How do I run a Rscript in RStudio?

You can open an R script in RStudio by going to File > New File > R script in the menu bar. RStudio will then open a fresh script above your console pane, as shown in Figure 1-7. I strongly encourage you to write and edit all of your R code in a script before you run it in the console.


1 Answers

For anyone who ends up here in 2020:

  • Ctrl(or Cmd) + Enter: Will run current line and jump to the next one. If a code portion is selected, run the selected code without jumping further.
  • Alt + Enter: – Will run the current line of code without moving the cursor to the next line, useful if you want to run it multiple times.

(Source)

like image 173
caesarsol Avatar answered Nov 15 '22 04:11

caesarsol