Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut for executing several lines in RStudio

Tags:

r

dplyr

rstudio

I'm using RStudio and normally run my code line by line, using Crtl + Enter. In general this works fine but working dplyr I would like to run a whole code-section I wrote using the pipe-operator %>%,

For e.g. I'd like to execute the following code-section at once, at the best independently from the current cursor position within the piped codelines:

mtcars %>%
  filter(carb >= 2) %>%
  group_by(cyl) %>%
  summarise(mean.hp = mean(hp))

I could run this line by line or mark all lines and then press Crtl + Enter but both ways are some kind of annoying. Searching the internet and the RStudio-options available in "Tools/Modify Keyboard Shortcuts" I only found other options like running a whole section, that is starting at

# Example section--------------------
like image 1000
jb123 Avatar asked Apr 21 '16 09:04

jb123


People also ask

What is the keyboard shortcut for running code in RStudio?

Execute code: The 2 shortcuts that I use most often to run the code are Ctrl+Enter and Ctrl+Alt+R. Ctrl+Enter is used when executing the code line by line. Also, a highlighted chunk of code can also be executed by this command. If you are looking to run the complete code, then Ctrl+Alt+R should be used.


2 Answers

Place cursor anywhere in the code chunk and use Crtl + Shift + (hold down Crtl and Shift, then push button). The first time the word in which the cursor is placed gets highlighted, then, when repeted, the whole chunk of code gets highlighted. If you have a nested loop or another nested structure this will highlight the whole innermost loop, then the second inner most loop etc.

When you have the code highlighted you can press Crtl + Enter to execute the code.

like image 136
Fridriksson Avatar answered Sep 21 '22 02:09

Fridriksson


I use the sections feature of RStudio liberally (like you noted) - but you didn't mention the keyboard shortcut Ctrl+Alt+T to run the section (all the code between # comment ---- or #### partitions).

# mutate ----

# rename ####

# filter ####

# summarize ####
like image 26
Arthur Yip Avatar answered Sep 17 '22 02:09

Arthur Yip