ENTER key runs a command

rstudio

#1

I'm new to R and on Dell Latitude laptop, Windows 10, and RStudio 1.1.419. In RStudio Console, both ENTER key and CTRL-Enter run a line of code on the laptop keyboard or a USB-connected keyboard. RStudio's Keyboard Shortcut list lists CTRL-Enter as Run. How can I have CTRL-Enter to run and disable Enter key from running a line of code?


#2

The short answer is that I don't think you can do this. Sorry! What have you been trying to do that makes you wish this were possible? Maybe we can figure out a workaround.

Console vs Source in RStudio

In the meantime, here's some orientation to the Console vs Source Pane in the RStudio IDE (I apologize if I'm telling you stuff you already know!)

In the RStudio IDE, there is both the Console, and the Source Pane.

The Source Pane (top left) shows open script files (there will be one called Untitled1 open by default when you launch RStudio). The CtrlEnter shortcut really relates to script files in the Source Pane. The shortcut sends the current line of your script (or multiple selected lines) to the Console to be executed — you'll actually see the line(s) appear in the console.

(My understanding is that it's not quite true that CtrlEnter also runs code in the Console, it's more that Enter runs code and the Ctrl part doesn't do anything in that context).

The Console (bottom left) is where you interact directly with the R interpreter. Like most command line interfaces, Enter always runs whatever you have entered at the prompt (the >). However, R is smart enough to only run code that forms a complete statement. If you write a partial statement and type Enter, then R will allow you to finish the command on the next line. For example:

14
(R added those + signs at the start of each line, to show me I hadn't finished my statement yet)

If you want to write several commands and then run them, put them in a script file. In general, it's better to work on developing script files (so you have a reproducible record of your work), rather than only working "interactively" at the Console.


#3

Yeah, as @jcblum's toggled explanation points out, there's a difference in how that works in the console versus in source. So, the trick is really to use source by working in a script!


#4

Thank you! I took a beginner's R workshop and used someone else's laptop. RStudio on my laptop did not show Source panel as default and I did not realize the difference between Console and Source panels. I'm all set now. Thanks again!


#5

Thank you! Please see my response to jcblum.


#6

The absence of the source pane is indeed confusing if you don't know that one "should" be there.

@mara, is it worth considering that RStudio auto loads a new source window when there is none on launch to prevent such confusion? (Or is there such an option already?)


#7

Not to my knowledge — I think I usually have some scratch going in Untitled-1 (other people's code for reprexes, etc), which means the source pane is open. You could submit a Feature Request.


#8

Same here re open source pane.

The FR will have to wait as I don't have a github account and won't be setting one up from work (if I ever use it, it would be for non-work stuff).


#9

Have fun with R, and I hope you'll keep asking questions :grin:


#10

I'm not sure auto-opening is desirable, as people prefer/need different types of source files—.R vs. .Rmd, yes, but also the divisions within .Rmd: R Notebooks, HTML docs, PDF docs, etc. An alternative may be a switcher popup like the current Rmd one, or less invasively, leaving the source pane open even when there are no files open with big background "New File | Open File" buttons.


#11

Yes, those options could work. I am too used to using R scripts only.


#12

You can go to a new line in the console without running the command by using ShiftEnter


#13

That's handy. Thanks for the tip!