3.1 Keyboard shortcuts
| Shortcuts | Function |
|---|---|
cmd + / |
comment |
shift + cmd + M or shift + ctrl + M |
In script editor: user defined; %>% |
ctrl + P |
In Radian: user defined; %>% |
opt + - |
user defined; <- |
ctrl + ` |
toggle btw editor and terminal |
cmd + 1/2 |
toggle between editors |
For commonly used general keyboard shortcuts (not limited to R), see HERE.
Suggested keyboard shortcuts for R in VS Code.
For user defined shortcuts, you can add them in the
keybindings.jsonfile.
Q: How to run R code interactively?
A: Create an R terminal via command R: Create R Terminal in the Command Palette. Once an R terminal is ready, you could either select the code or put the cursor at the beginning or ending of the code you want to run, press (Cmd + Enter), and then code will be sent to the active R terminal.
If you want to run an entire R file, open the file in the editor, and press Cmd+Shift+S and the file will be sourced in the active R terminal. Alternatively, open Command Palette and type “R: Run Source”.
You can use # %% to create code cells in R scripts, mimicking the behavior of Jupyter notebooks.
Use Shift + Cmd + Enter to run the current cell.
Refer to Run R in Interactive Window for more details.
When you run the whole code cell, output will be shown after the whole cell is executed. If you want to see the output immediately after each line of code, you can add the following to settings.json:
But the output will look cluttered as the code and output are mixed together. So I prefer to keep r.bracketedPaste to true and to see the output after each cell is executed.
Q: Why using VS Code for R programming instead of RStudio?
A: Several reasons:
- Better integration with Copilot, making it easier to write code with AI assistance.
- More responsive and powerful engineering tools such as symbol highlight, find references, rename symbol, etc. integrated to the IDE.
- VS Code has a lot of extensions that can enhance your R programming experience, such as
Markdown Preview Enhance,Live Server, andGitLens. - Git support is better in VS Code, making it easier for version control and collaboration.