Files
SolSolAutomazione/R-VsCode/r_vscode_quick_start.md
2026-02-10 10:53:06 +01:00

2.0 KiB

Quick Start: Testing R in VSCode

Step 1: Install R Extensions

  1. Open VSCode
  2. Go to Extensions panel (Ctrl+Shift+X)
  3. Search for and install:
    • "R" by REditorSupport
    • "R Debugger" by RDebugger
    • "R LSP Client" by REditorSupport

Step 2: Create Your First R File

  1. Create a new file (Ctrl+N)
  2. Save it with .R extension (e.g., test.R)
  3. 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

  1. Open Terminal in VSCode (Ctrl+` )
  2. Type R to start R interactive session
  3. Type commands directly:
    > 2 + 2
    [1] 4
    > plot(1:10)
    

Method B: Run Entire Script

  1. Open your .R file
  2. Press Ctrl+Shift+P
  3. Type "R: Run Source" and press Enter
  4. Output appears in R Terminal

Method C: Run Selected Code

  1. Select code in your R file
  2. Right-click → "Run Selected Line(s)"
  3. Or use shortcut: Ctrl+Enter

Step 4: View Plots

  1. Run plot commands in R terminal:
    plot(1:10, main="Test Plot")
    
  2. Plots appear in VSCode's Plot viewer panel

Step 5: Use R Markdown (Optional)

  1. Create new file with .Rmd extension
  2. Add code chunks:
    ```{r}
    summary(mtcars)
    
  3. Click "Knit" button to render document

Step 6: Debug R Code

  1. Set breakpoint by clicking left margin
  2. Press F5 to start debugging
  3. 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