jb123
jb123

Reputation: 197

Shortcut for executing several lines in 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--------------------

Upvotes: 5

Views: 4160

Answers (3)

Scarabee
Scarabee

Reputation: 5704

In RStudio version 1.0.136 it's now the default behavior:

Press Crtl + Enter anywhere in a multi-line pipe command and it will be run entirely.

Upvotes: 3

Arthur Yip
Arthur Yip

Reputation: 6210

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 ####

Upvotes: 3

Fridriksson
Fridriksson

Reputation: 197

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.

Upvotes: 5

Related Questions