3  Setup

There’s lots of options for AI coding assistant software. What you use will depend on:

Below I’ve provided instructions for some of the options. These certainly aren’t comprehensive.

There are a multitude of other R package options beyond what I cover below, which are covered in Large Language Model Tools for R by Luis D. Verde Arregoitia (in English and Spanish).

I recommend following this page from a computer, not a mobile device. The right hand menu will show and has the navigation pane so you can jump to the section that’s relevant to your set-up needs.

3.1 R packages

If you are using R then we will make use of: install.packages(c("vegan", "ellmer", "tidyverse")). For Python users you can follow along most examples without these, this isn’t an R training course, it’s a course on using AI to assist with coding.

3.2 Options for AI software

3.2.1 Preferred option for R users

What I use in the workshops I teach:

  • The R program from either Positron or the VS Code Integrated Development Environment (IDE)
  • GitHub Copilot
  • R Ellmer package, for which you’ll need an API key to one of the LLM providers

Positron is easier to setup for R users than VS Code. Most of what you can do in VS Code can also be done in Positron, Positron just being a clone of the VS Code source code. So try Positron first. If you can’t use Positron for some reason, comprehensive instructions for VS Code and R are below in (Section 3.8). You may need IT help, especially if your computer is locked down for institutional security!

If you chose this option you will need to follow instructions in this chapter for ‘Getting an API key’ (Section 3.4) (required for ellmer), ‘Ellmer set-up’ (Section 3.6) and ‘GitHub Copilot set-up’ (Section 3.5).

3.2.2 Option 2 for R users

Use RStudio with R. You will need ellmer and gander packages. See instructions below for ‘Getting an API key’ (Section 3.4), ‘Ellmer set-up’ (Section 3.6) and ‘Gander: RStudio friendly alternative to Copilot’ (Section 3.7).

RStudio also has basic GitHub Copilot capabilities. To use these (not essential for the workshop) get a Copilot account (Section 3.5) then in RStudio go to ‘Tools’ -> ‘Global Options’ -> ‘Copilot’ and enable it (follow instructions for entering password to setup).

3.2.3 Option 3 for R users: Positron assistant

Positron includes a built-in AI assistant that can help you with coding tasks.

Positron’s assistant provides similar functionality to GitHub Copilot, including:

  • Code completion and suggestions
  • Natural language to code generation
  • Code explanation and documentation

To use Positron with its AI assistant:

  1. Download and install Positron from https://positron.posit.co/
  2. Follow the instructions in the Positron assistant documentation to set up the AI assistant
  3. You’ll need to configure your AI providers.

3.2.4 Python users

I’m not a Python programmer, however, all the principles I’ll teach also apply to Python. You can easily follow on with the examples in Python.

You will need to have Positron or VS Code with GitHub Copilot. So I recommend you get the VS Code software, then follow these instructions for Python set-up.

Then follow the instructions for ‘GitHub Copilot set-up’ (Section 3.5).

I also recommend you get an API key with OpenRouter (Section 3.4).

You don’t need an ellmer equivalent because most of the LLM providers already provide Python code on their webpages.

3.2.5 Options 4+

There are innumerable AI coding assistants now available. Feel free to BYO if you are using one that you are already comfortable with. However, I strongly recommend using a tool with IDE integration (meaning it can edit your R/Python scripts directly). See Section 3.9.

As a fall-back, you can follow this book via ChatGPT or Copilot, but it will involve lots of cutting and pasting. Not the integrated experience I want you to have.

3.3 Software options summary

Here are some of the software options I’ve looked at. Or if you’ve picked your option from the above list, just jump ahead to the required sections.

A note on ‘VS Code and clones’: VS Code is the most widely used IDE for this kind of work, and several other editors are built directly on its source code, so they behave almost identically and run the same extensions. These clones include Positron (managed by Posit, the group behind RStudio, and generally the easiest of them to set up for R), Cursor and Windsurf. Wherever an option below says ‘VS Code and clones’, any of these will do — pick whichever you find easiest to install. For most R users this will be Positron.

3.3.1 VS Code and clones with GitHub Copilot

Best for: Coders of all abilities who have a fixed budget or just want to try IDE integration.

Pros:

  • Subscription based
  • Basic free tier
  • Editor integration (auto-complete style)
  • Agent mode
  • Multiple LLMs available
  • Easy to use

Cons:

  • Less flexibility and customisation than other agents
  • The GitHub Copilot Agent is less automated than other agent software
  • Limited choice of LLMs (unless you BYO API key)
  • Plans subject to sudden changes (usually reduced model availability as token costs rise)

3.3.2 Ellmer R package

Best for: R users who want to create their own chat bots, or integrate LLMs into their code and shiny apps. For example, to extract data from a large corpus of papers.

Pros:

  • Works anywhere R works
  • Unlimited flexibility
  • BYO API key, can interact with any LLM
  • Automate API calls to LLMs

Cons:

  • Only a basic prompt interface provided (you need to write it)

3.3.3 Gander R package

Best for: Moderately experienced R users who don’t want to leave RStudio.

Pros:

  • Works with RStudio
  • Can see ‘inside of’ R objects, so knows your variable names
  • Can customise how it works to optimise performance and cost
  • BYO API key, can interact with any LLM

Cons:

  • Requires some experience with the R program
  • Not as deeply integrated with RStudio as GitHub Copilot is with VS Code (doesn’t have as many features as Copilot)

3.3.4 Aider

Best for: Experienced python programmers.

Pros:

  • Python based AI agent
  • Interact with the agent via Python code
  • Highly flexible, can use as an everyday coding assistant or to create bespoke agents
  • Can edit files as well as run in agent mode

Cons:

  • Not a straightforward prompt interface
  • Requires Python coding experience

3.3.5 VS Code and clones with Cline

Best for: Experienced programmers who want to fully automate coding workflows.

Pros:

  • Fully automated agents
  • Multiple modes for different behaviours, e.g. plan versus act
  • BYO API key
  • System messages customisable for advanced scientific applications of agents
  • Large user base

Cons:

  • Can be hard to set-up for R users
  • Can get more expensive
  • Does not do inline code editing ‘as you type’ like copilot
  • Not suitable for novice programmers

3.3.6 Positron Assistant

Best for: R users who want an assistant built in to their IDE, rather than added as an extension.

Pros:

  • R friendly developer (Posit, the group who run RStudio)
  • Set-up is simpler than adding a third-party extension

Cons:

  • New software, still on version 0, so it will likely change
  • Limited features to date
  • BYO API Key, so costs are currently higher than buying access directly from the LLM companies, who often discount token costs (at least in the short term, may not last).

3.3.7 Claude Code, Codex and similar

Best for: Users who want a subscription-based, terminal- and cloud-native agent that also runs on the web.

Pros:

  • Fully automated agents, run from the terminal or a browser
  • Strong track record on R and statistics tasks
  • Subscription based, so cost is predictable
  • Runs in isolated cloud sandboxes for web sessions

Cons:

  • Not deeply integrated with RStudio
  • Newer to the R community, fewer worked examples out there yet

There are many other (10s, 100s?) of coding assistants now available. I haven’t tried most of them. Last time I checked none of them worked with RStudio. We’ll come back to Claude Code specifically in Section 2, since it’s really an agent rather than an inline-completion tool.

3.4 Getting an API key

An API key is like a password that allows the AI assistant (e.g. Cline) to send your prompt to a large language model. Your key should be kept private. Usually you’ll have to buy some credits. These allow you to send prompts to the LLM. You’ll be paying per prompt.

Now you need to choose your large language model provider. I’m currently using OpenRouter and Anthropic, which have a diversity of models for generating text, code and reading images. Do some web searching to find out the latest info on providers and models.

What you choose depends on what you want to do and your budget. Some providers offer a free tier. You’ll need to web search for the latest info on this.

For this workshop I recommend you get an OpenRouter API key. This will give you the most flexibility to try different models and to find models that have free options. It’s a bit more expensive than buying tokens directly from the model’s home provider, but gives you more flexibility.

Once you’ve chosen a provider, create an account and follow their instructions for creating an API key. You will probably also need to buy some credit to use the model.

The one caveat is that OpenRouter may not have access to the full capabilities of all LLMs. For example, when Sonnet 3.7 came out you could get vision capabilities via an Anthropic API key but not with an OpenRouter API key.

Important the sign-up for getting an API key is often through a different webpage to the sign-up you may be using for subscription based AI tools (e.g. Claude Code or ChatGPT). Here are a couple of key links:

3.5 GitHub Copilot set-up

Once you have VS Code just follow the simple steps here. Note if you already have a GitHub account make sure you have your login information handy.

3.6 Ellmer set-up

First, you need to get an API key from the provider, see step above on ‘Getting an API key’.

Then, you need to add the key to your .Renviron file:

usethis::edit_r_environ()

Then type in your key like this:

OPENROUTER_API_KEY=“xxxxxx”

If you are using open AI it would be like this:

OPENAI_API_KEY=“xxxxxx”

i.e. use the appropriate name for the provider you are using.

Then restart R. ellmer will automatically find your key so long as you use the recommended environment variable names. See ?ellmer::chat_openrouter (or chat_xxx where xxx is whatever provider you are using).

3.7 Gander: RStudio friendly alternative to Copilot

Gander lets you select code in RStudio and edit it directly through calls to an LLM. Posit’s own announcement post is a good short read too: Introducing Gander.

Follow the steps to set-up Ellmer first.

Then you can install.packages("gander")

Now, you need to do two steps before Gander will work. First you need to add your default LLM to the .Rprofile so Gander knows what LLM to use type: usethis::edit_r_profile()

Now set the option for your LLM, e.g.

options(.gander_chat = ellmer::chat_anthropic()) or options(.gander_chat = ellmer::chat_openai())

Now add a keyboard short-cut to use Gander. Go to the ‘Tools’ menu at the top, click ‘Add-ins’, ‘Browse Add-ins’, then click the ‘Keyboard shortcuts’ button. Find the row for Gander (probably at the bottom), click in the second column for ‘Shortcut’ and type your keyboard shortcut. I used cmd-i (or cntrl-i). Then click ‘Cancel’ once you’ve set the shortcut key (‘Cancel’ as we don’t want to run Gander right now).

If for some reason the shortcut key won’t work you can also gander::gander_addin() at anytime from R.

Now to check it works, open an R project and select a line of R code. Press your shortcut keys. A box should pop up. Type a prompt (like ‘make this code more fun’) and hit enter to test it does something.

I recommend reading the gander reference as there are ways you can customise it to know your data but also save on tokens. We’ll use gander for code completion in Section 1.

3.8 VS Code set-up for R users

VS Code has many extensions that let you create and run entire workflows via using prompts to a large language model. It’s not widely used in the R community yet, but I expect it or one of its clones (like Positron) will be soon. You can create your entire R project, interpret the results and write a draft of your findings without writing any R code.

Most of these tools are not available (as of writing) in RStudio, or have only limited functionality. So you need to use a different IDE (Integrated Development Environment) to run your R code. Here I’ll explain how to set-up VS Code (a popular IDE) so you can use Cline.

3.8.1 Software requirements

To set up VS Code for R and Cline, you’ll need:

  • R programming language
  • VS Code text editor
  • R extension for VS Code
  • Cline AI assistant extension for VS Code

Note that if your computer is controlled centrally by an IT department, you may need to request admin access to install software, or email IT and ask for them to come and help you.

3.8.2 Install R

  1. Go to the official R project website: https://www.r-project.org/
  2. Click the “download R” link in the Getting Started section
  3. Choose a CRAN mirror close to your location
  4. Download the appropriate R installer for your operating system
  5. Run the installer and follow the prompts to complete installation

3.8.3 R packages

  1. Open R or RStudio
  2. Install language server install.packages("languageserver")
  3. Install httpgd install.packages("httpgd") (this helps improve plots in VS Code). NOTE that httpgd seems to often be removed from CRAN, then come back again, I’m not sure why… If you are having trouble you can try install from a different repo, see instructions here: https://community.r-multiverse.org/httpgd

3.8.4 Install VS Code

  1. Go to the official VS Code website: https://code.visualstudio.com/
  2. Click the big blue “Download” button
  3. Download the appropriate VS Code installer for your operating system
  4. Run the installer and follow the prompts
  5. Launch VS Code once installation is complete

3.8.5 Install R extension

  1. Open VS Code
  2. Open the Extensions view in VS Code (click the boxes on left hand side)
  3. Search for “R” in the extensions marketplace
  4. Select the “R” extension published by REditorSupport
  5. Click the “Install” button
  6. Restart VS Code after installation if prompted

More info on VS Code and R here

3.8.6 Connect R and VS Code

  1. Open a new terminal in VS Code (Terminal > New Terminal)
  2. Check that R is installed by running: R --version
  3. Type R to open the R console in the terminal
  4. Now open any R script in VS Code (File > Open)
  5. Run some R code to check that VS Code can connect to R in the terminal. Use the shortcut Ctrl+Enter/Cmd+Enter or press the play button in the top right of the script editor.

If R is not found then open extensions (left hand side, boxes icon), filter by ‘enabled’ then click the R extension. Now click the cog icon in the R extension and select ‘settings’ from the dropdown. Search for ‘rpath’. Check that it has the correct path to R on your computer. Also update ‘rterm’ with the same path. You can find the path by opening a terminal and typing which R (on mac) or in a windows terminal where R.

One way to find out where R is installed on your computer is to open RStudio, go to ‘Tools’ -> ‘General’ then under the field ‘R version’ it will have the path to your R version that may look like this: ‘C:.0’

Copy that path from RStudio. Then go back to the VS Code settings and paste it into the ‘rpath’ and ‘rterm’ settings. You will also need to add the R.exe on the end like this: ‘C:/Users/myname/AppData/Progams/R/R-4.5.0/R.exe’

Now open an R script and run it (e.g. press cmd/cntrl-enter). If everything is connected R will open and it will run the script. If it’s not working you’ll get an error pop-up. Go back and check the steps above.

If you copied it on windows you may also need to reverse the slashes, like I have above.

While you have the extension settings open search for ‘httpgd’ and make sure Plot: Use Httpgd is enabled.

3.8.7 Issues and tips for VS Code with R

This is just a list of issues I’ve had and how I’ve solved them.

Plotting If your R plots look weird (like tiny font), make sure httpgd is enabled. Go back to steps above and see how to do that.

Viewing data There are various extensions for viewing csv and excel files. It is worth looking into these so that when you do View(dat) in R you get a nice table. Some also allow editing.

Getting help to install software My computer is somewhat locked down by IT, so getting this set-up was a bit fiddly and required a few requests to IT to install software.

R markdown There are options in the R extension settings for how to knit markdown. You may need to configure these if you want to knit markdown docs from VS Code. If you are having trouble knitting markdown it may mean that the path to pandoc is not set correctly. There are some helpful instructions here

R terminal crashes If I run too much R code at once (like selecting a big block then running) the terminal tends to crash. Initially I see a little highlighted box saying ‘PTY HOST’. Then I need to close all the terminals (with the bin icon) and start again. Try radian if this is a problem. You can also code run line-by-line or source whole scripts from the terminal (which works fine). I tried debugging this by increasing the buffer but to no avail.

Shortcut keys (on osx) cmd-/ to comment uncomment lines. cmd-shift-p to open the command palette, cmd-b to open the file explorer, cmd-enter to run lines or selection of R code, cmd-shift-c to open terminal in new window, cntrl-shift-` to open a new terminal in VS Code.

3.9 Installing other gen AI VS Code extensions

e.g. if you want to try Cline, Claude Code or Codex

  1. Open the Extensions view in Positron or VS Code (Ctrl+Shift+X)
  2. Search for the genAI assistant of your choice. I’m using Cline and Claude Code currently.
  3. Select the extension
  4. Click the “Install” button
  5. The extension icon (e.g. a robot if using Cline) should appear in the VS Code sidebar (left side).

You’ll have to navigate the extensions menus to input your API key. It won’t work without an API key that gives you access to an LLM. e.g. for Cline:

  1. Click on the extension icon (e.g. a robot for Cline) on the left hand side
  2. Click the cog (if the settings don’t open automatically)
  3. Select your API provider and cut and paste the API key into the box.