2.0 KiB
2.0 KiB
Quick Start: Testing R in VSCode
Step 1: Install R Extensions
- Open VSCode
- Go to Extensions panel (Ctrl+Shift+X)
- Search for and install:
- "R" by REditorSupport
- "R Debugger" by RDebugger
- "R LSP Client" by REditorSupport
Step 2: Create Your First R File
- Create a new file (Ctrl+N)
- Save it with
.Rextension (e.g.,test.R) - Type some basic R code:
print("Hello R!") x <- 1:5 mean(x)
Step 3: Run R Code in VSCode
Method A: Using R Terminal
- Open Terminal in VSCode (Ctrl+` )
- Type
Rto start R interactive session - Type commands directly:
> 2 + 2 [1] 4 > plot(1:10)
Method B: Run Entire Script
- Open your
.Rfile - Press Ctrl+Shift+P
- Type "R: Run Source" and press Enter
- Output appears in R Terminal
Method C: Run Selected Code
- Select code in your R file
- Right-click → "Run Selected Line(s)"
- Or use shortcut: Ctrl+Enter
Step 4: View Plots
- Run plot commands in R terminal:
plot(1:10, main="Test Plot") - Plots appear in VSCode's Plot viewer panel
Step 5: Use R Markdown (Optional)
- Create new file with
.Rmdextension - Add code chunks:
```{r} summary(mtcars) - Click "Knit" button to render document
Step 6: Debug R Code
- Set breakpoint by clicking left margin
- Press F5 to start debugging
- Step through code with F10/F11
Quick Test Commands to Try
In R terminal, test these:
# Basic math
2 + 2
sqrt(16)
# Create data
data <- c(1, 3, 5, 7, 9)
mean(data)
# Simple plot
plot(data, type="l", col="blue")
# Check packages
installed.packages()[1:5,]
What You Should See
- ✅ Code completion as you type
- ✅ Syntax highlighting
- ✅ R terminal with
>prompt - ✅ Plots in separate viewer
- ✅ Variable values in debugger
Troubleshooting
- If R isn't found: Check R path in extension settings
- Restart VSCode after installing extensions
- Ensure R is in system PATH