Introduction
VSCode provides an efficient environment for interactive R programming, enabling real-time execution, debugging, visualization, and data exploration directly within your editor.
This guide covers how to effectively run, debug, and visualize R code interactively, optimizing your data analysis workflow in VSCode.
R Terminal Integration in VSCode
Starting an R Terminal Session
To interactively run R code, start by opening an R terminal in VSCode:
- Open the Command Palette (
Ctrl + Shift + P
orCmd + Shift + P
). - Type and select
R: Create R terminal
.
By default, a standard R console appears:
We recommend the enhanced radian terminal, offering syntax highlighting, auto-completion, and superior user experience:
Session Watcher and Auto-Attach
When using VSCode’s session watcher, your R session auto-connects, providing real-time feedback in the status bar:
Running R Code Interactively in VSCode
- Run Selected Code: Select code, then press
Ctrl + Enter
(Windows/Linux) orCmd + Enter
(Mac). - Execute Line-by-Line: Place cursor on a line and press
Ctrl + Enter
orCmd + Enter
, ideal for debugging or incremental testing.
Using Self-Managed Persistent R Terminals
Persistent terminals (tmux
or screen
) keep your R session active after closing VSCode, ideal for lengthy computations or remote servers.
Start a Persistent Session:
tmux new -s mysession R # or radian
Detach the session: Press
Ctrl + B
thenD
.Reattach session:
tmux attach -t mysession
Configure your .Rprofile
to integrate self-managed terminals with VSCode’s session watcher:
if (interactive() && Sys.getenv("RSTUDIO") == "") {
Sys.setenv(TERM_PROGRAM = "vscode")
source(file.path(Sys.getenv(
if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"
".vscode-R", "init.R"))
), }
Interactive Data Visualization with httpgd
httpgd is an interactive graphics device providing real-time plots directly in VSCode.
Install and Enable:
install.packages("httpgd")
Enable in VSCode
settings.json
:"r.plot.useHttpgd": true
Interactive plot viewer in action:
Using R Markdown in VSCode
VSCode fully supports R Markdown, enabling reproducible analyses with combined narrative, code, and outputs.
Execute Code Chunks: Press
Ctrl + Alt + Enter
(Windows/Linux) orCmd + Option + Enter
(Mac) within code chunks.Render Documents:
::render("your_file.Rmd") rmarkdown
Debugging and Data Exploration
Leverage powerful debugging and data exploration tools built into VSCode:
Debugging R Code
Set breakpoints and step through your scripts using vscode-R-debugger
:
Install required packages:
install.packages("remotes") ::install_github("ManuelHentschel/vscDebugger") remotes
Easily inspect variables, step through code, and debug interactively.
Additional Tips for Enhanced Workflow
- Utilize Shortcuts: Use shortcuts like
Ctrl + Enter
for rapid code testing. - Command Palette Efficiency: Quickly open terminals or manage tasks via the Command Palette (
Ctrl + Shift + P
). - Code Folding and Navigation: Organize scripts effectively by folding sections and using built-in navigation tools.
Read more about efficient coding practices in Recommended VSCode Configurations for R.
Conclusion
Interactive R programming in VSCode significantly streamlines your data science workflow. With real-time execution, powerful debugging tools, interactive visualization via httpgd, and efficient workspace management, VSCode transforms into a comprehensive R IDE. Harness these capabilities to enhance your coding efficiency, improve reproducibility, and deepen insights during data analysis.
References & Further Reading
Explore More Articles
Here are more articles from the same category to help you dive deeper into the topic.
Reuse
Citation
@online{kassambara2025,
author = {Kassambara, Alboukadel},
title = {Interactive {R} {Programming} in {VSCode}},
date = {2025-03-23},
url = {https://www.datanovia.com/learn/tools/r-in-vscode/interactive-r-programming-and-running-code-in-vscode.html},
langid = {en}
}