How to use R with Cursor, the AI-powered code editor
Combine R's powerful features for working with data with Cursor's powerful features for editing code
The R ecosystem mostly assumes you’re using RStudio or Positron, but it’s fully possible to use VSCode and its forks, including Cursor, the AI-native IDE.
This guide explains how to set up R for use with Cursor and vice versa.
Video explainer
If you prefer video explainers, you can see my YouTube video, which covers the same material as this guide.
Pros and cons of using R with Cursor
Pros:
Extremely powerful interface for AI coding workflows
Really nice GUI tools for git
Powerful cross-language tooling for stuff like JSON parsing, Github Actions, and Dockerfile scripts
Cons:
Annoying to use with
renv
(see final section of this doc)Subscription costs $20 per month (although you can skip the subscription and get usage-based pricing with an API key instead)
Setup
Step 1. Install R for your operating system from CRAN.
Go to the official R website (https://www.r-project.org).
Download the appropriate version for your operating system.
Run the installer and follow the prompts.
Step 2: Install Cursor IDE
Visit the Cursor website (https://cursor.com).
Click on the download link, which should automatically detect your operating system.
Once downloaded, run the installer.
Step 3: Install Pandoc for your operating system
Visit the Pandoc website (https://pandoc.org/).
Go to the “Installing” tab.
Choose one of the following methods:
Download and run the installer directly.
Use a package manager:
Windows: Use Scoop or Chocolatey
macOS: Use Homebrew
If using a package manager, open a terminal and run the appropriate command:
Scoop:
scoop install pandoc
Homebrew:
brew install pandoc
Verify the installation by running
pandoc --version
in the terminal.Restart your computer to ensure Pandoc is properly recognized.
Step 4: Install R tools (Windows users only)
Go to the CRAN R project website.
Find the R tools page.
Download and run the R tools installer.
Step 5. Install Quarto CLI for your operating system.
Go to the Quarto website (https://quarto.org/).
Download the appropriate version for your operating system.
Run the installer and follow the prompts.
Step 6: Create a user library folder and install required IDE dependencies via a setup script
Open a terminal. Make sure it’s your operating system’s terminal and not an R terminal!
Navigate to a folder where you want to put the R setup script.
Clone my R setup GitHub gist:
git clone https://gist.github.com/chriscarrollsmith/88d7dc3b4ac5255c4d7fb7f180fa72eb
Navigate to the cloned folder:
cd 88d7dc3b4ac5255c4d7fb7f180fa72eb
Run the script with
Rscript Rconfig.R
Wait for all packages to install (this may take a few minutes).
Step 7: Install and configure Cursor extensions
Open Cursor IDE.
Toggle the primary sidebar using the button in the upper right corner.
Click on the “Extensions” tab.
Search for “REditorSupport” in the search bar.
Install the R extension.
Click on the gear icon next to the installed R extension and select “Extension Settings.”
Enable the following settings:
Check
R: Plot Use HTTPGD
Check
R: Use Renv
(only if you plan to userenv
for dependency management)Check
Set R: Lib Paths
to “Users/{your-username}/.R/library” (Windows users should add a “C:” in front)
Install the Quarto extension by searching for it in the Extensions tab and clicking “Install.”
Usage
Step 1: Create an R project in Cursor
Create a new project folder.
Open the folder in Cursor.
Create a new file with an
.R
extension.
Step 2: In order to get tooltips and syntax highlighting to work in Cursor, you may need to:
Open an R terminal
Hit Ctrl+Shift+P and select "R: Attach Active Terminal".
Step 3: Use AI-assisted coding
In your R file, press
Ctrl+K
to bring up the AI prompt box.Select your preferred AI model (e.g., Claude 3.5 Sonnet).
Enter a prompt (e.g., “Write me a ggplot demo in R”).
Click “Generate” to create the code.
Step 3: Run and view plots
Wrap your plots in
print()
statements to ensure they display in Cursor.Run the code to generate and view the plots.
Additional Tips:
Use tab completion to accept code suggestions.
Highlight code and click the “Chat” button to add code snippets to AI chat and ask for explanations or modifications.
Construct your custom chat context by adding files to the chat with the
+
button or even pasting in a link to documentation (which will be automatically scraped and pulled into the chat).
A note on project-level environments with renv
Cursor depends on some R packages to provide R language support. If you’ve executed the steps in this tutorial correctly, then you have installed these globally at the level of the user and instructed Cursor to look for them in the user library even in a project that uses renv
.
But I have had inconsistent results getting this to actually work in an renv
project. If you have double-checked the Set R: Lib Paths
setting but still see warnings that you need to install languageserver
or httpgd
, you can resolve this error by installing languageserver
and httpgd
as “Suggests” dependencies in the DESCRIPTION
file of your renv
project and then attaching the terminal to VSCode. Alternatively, you can manually add your user library to the library paths used by your project by calling `.libPaths(“path/to/your/user/library”)`.