Home

Using Claude Code with RStudio on Mac

You have RStudio on Mac for running R code and Claude Code for AI-powered coding assistance. This tutorial shows you how to use both tools together on the same project files. You’ll create an R project, write some code manually, then use Claude Code from Terminal to enhance it with visualizations and analysis—all while RStudio stays open to run and test your code.

Key Concepts

What You’ll Need

Step 1: Open RStudio on Mac

Step 2: Create a New R Project

Step 3: Create a New R Script

Step 4: Write Initial Code Manually

Type this code into your iris.R file:

data(iris)
str(iris)
summary(iris)

Step 5: Open Terminal

Step 6: Navigate to Your Project Folder

Tip: If you’re not sure of the exact path, you can drag and drop the folder from Finder into Terminal and the full path will appear automatically!

Step 7: Launch Claude Code

Step 8: Ask Claude for a Scatter Plot

If Claude Code is slow or unresponsive, just wait for it to initialize. Then, type this request:

Add code to iris.R to create a scatter plot of sepal length vs. width, colored by species. Use ggplot2.

Step 9: Run the New Code in RStudio

Step 10: Refine the Scatter Plot

Step 11: View the Refined Plot

Step 12: Ask Claude for PCA Plot

Step 13: Run the PCA Analysis

Step 14: Ask Claude to Review and Comment

Step 15: Ask Claude to Create R Markdown

Step 16: Knit the R Markdown File

Troubleshooting

Mac Keyboard Shortcuts

Useful shortcuts for switching between apps:

Next Steps

Workflow Summary

This hybrid setup combines the best of both worlds:

The workflow is simple: write or edit code with Claude in Terminal, then immediately test and run it in RStudio. No file copying or manual syncing needed—both applications access the same files seamlessly on your Mac.


Created by Steven Ge on December 11, 2024.