Display name
Display name

Reputation: 4501

R Studio Keyboard Shortcuts to Execute Specific Code

library(ggplot2)                        # line 1
p1 <- ggplot(mtcars, aes(disp, mpg)) +  # line 2
  geom_point()                          # line 3
                                        # line 4
ggplot(mtcars, aes(mpg, disp)) +        # line 5
  geom_point()                          # line 6
                                        # line 7
10 + 10                                 # line 8  

I know that I can highlight and code above in R Studio and press Ctrl+Enter on my keyboard to run that code.

I see other ways to do this on R Tutorials online but can't figure out how people are doing it. I looked through R Studio's shortcuts by pressing Alt+Shift+K but couldn't come up with what I'm looking for. Here's what I want as keyboard shortcuts:

  1. Line #2. Place the cursor right before the g in ggplot(mtcars... and run only the code up to the end of line 3. How do I do this?
  2. Place the cursor anywhere on line 4 and run only lines 5 and 6. Essentially "run the next chunk".

Can you tell me the keyboard shortcut and procedure to do these two things? This is assuming I'm in an R script, not in R Markdown (although that answer would be nice if it exists as well).

Upvotes: 0

Views: 1919

Answers (1)

ode2k
ode2k

Reputation: 2723

You can use the following keyboard shortcuts:

Ctrl+Alt+B runs the code from the beginning of the document up to that line. (Question #1)

Ctrl+Alt+T to run the code in each section (in the current section your cursor is). (Question #2)

https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts

Upvotes: 3

Related Questions