How to Setup and Use AI Coding Assistants in RStudio

Summary

Researchers who use RStudio for writing R code now have the option to install and use an AI coding assistant. However, RStudio gives the AI access to the datasets in the R environment, so researchers must be cautious about which AI service and model they choose. This article shows how to setup AI coding assistants in RStudio, and explores important considerations.

 

Overview

AI coding assistants are tools that utilize generative AI to create, edit, fix, and explain code. These tools often integrate into development environments and editors such as VSCode and RStudio. This integration allows the AI agent to directly read and modify your code, check it into GitHub, run tools against it, test it live, etc. Some examples of what you could ask an AI agent to do include:

  • Explain what this R script does.
  • Create a README file in markdown format explaining the code and the flow of data.
  • When I run the code with sample-data.csv, formula x gives me unexpected results. Check if it's broken, and if so, fix it.
  • Look at my datasets and tell me which statistical significance tests are appropriate.
  • Run my script and create a narrative in plain English based on the outputs and results, including plots where appropriate.

Researchers who work with sensitive data, such as PHI, must be careful about activating AI tools in RStudio since this software gives the AI agents access to the datasets. Researchers should avoid Claude Code, Codex, Kimi Code and similar tools as they currently are not approved for PHI. Two options to mitigate the risk include connecting to the UM GPT Toolkit and using one of the institutionally approved models for PHI (currently that is only the GPT models). Another option is to run a local LLM server, such as ollama, with a locally-installed model (e.g. Kimi, Qwen Coder, Devstral, DeepSeek, etc.). Note that this document is meant to provide helpful information, but it is your responsibility as a researcher to confirm with information assurance which options are allowed for the data classification of your datasets.

 

Configuration

Using UM GPT Toolkit (billed to a department short code)

  1. You must get a UM GPT Toolkit API key first, which requires a department bill code. See Create or Retrieve a U-M GPT Toolkit API Key.
  2. Open RStudio and click the Posit Assistant button on the toolbar (upper right)
  3. In the Assistant panel, click the gear icon, then click Configure LLM providersUploaded Image (Thumbnail)
     
  4. Scroll down to OpenAI Compatible (not the option that just says "OpenAI") and click ConfigureUploaded Image (Thumbnail)
     
  5. Enter the API key you created in step 1, and the API endpoint URL provided by the UM GPT team (the URL typically ends in /v1). Click Test to ensure the connection works, then click Save Uploaded Image (Thumbnail)
     
  6. In addition, RCode offers code completion through a couple of providers including a Posit service and GitHub CoPilot. This setting can be found under the Tools menu -> Global options -> Assistant -> Code suggestions. However, researchers should not enable this setting without a brief consultation with information assurance.

 

Using Ollama + Local Model (free; requires GPU card with 4GB+ VRAM, and 16GB+ RAM)

  1. Download and install ollama or your preferred ollama-compatible local LLM server.
  2. Open a command prompt / terminal and and install a model locally by typing ollama pull model_name, then hit Enter.
    1. You can browse models with the command ollama ls or by visiting https://ollama.com/search
    2. Qwen Coder is a good starter model for most coding tasks (ollama pull qwen2.5-coder:7b)
  3. Launch ollama
  4. Open RStudio and click the Posit Assistant button on the toolbar (upper right)
  5. In the Assistant panel, click the gear icon, then click Configure LLM providersUploaded Image (Thumbnail)
     
  6. Scroll down to Ollama and click ConfigureUploaded Image (Thumbnail)
     
  7. Enter the local ollama URL (the default is http://127.0.0.1:11434). Click Test to ensure the connection works, then click Save Uploaded Image (Thumbnail)
     
  8. In addition, RCode offers code completion, but the AI providers cannot be set to a local ollama server as of Fall 2026.

 

Usage

  • Once your AI coding agent is configured, you will see a chat interface in the Posit Assistant panel.
  • The chat has various edit modes available (most AI coding agents offer this), which you can change with the hand () icon:
    • Normal - the agent prompts you before making any edits.
    • Plan - generates a plan and displays it for your approval before starting the changes. This is useful for larger tasks or when asking for many related changes.
    • Auto - edits the code directly without prompting first. This is useful after you accepted a plan to avoid being prompted frequently to approve each change.
    • Cleaning - the agent looks at your datasets and recommends data cleaning actions.Uploaded Image (Thumbnail)
       
  • The recommended flow for simple questions and minor changes is the Normal mode.
  • The recommended flow for larger asks, fixing bugs, and analysis, is Plan mode first, then review the plan and edit as needed, then switch to Auto mode.
  • Keep in mind that AI agents can figure out a lot about your data and suggest code, but they do not have the context about your study and data source. Therefore, you should always review the plans that it creates, the code it writes, and work through multiple iterations.

 

Notes

  • This article is provided for informational purposes only. Always check with your IRB and information assurance or cyber security team before using real, protected data with any AI tool.

 

Resources

 

About the Author

Gabriel Mongefranco is a Mobile Data Architect at the University of Michigan's Eisenberg Family Depression Center. Gabriel has over a decade of experience with automation, data analytics, database architecture, dashboard design, software development, and technical writing. He supports U-M researchers with data cleaning, data pipelines, automation and enterprise architecture for wearables and other mobile technologies.

 |   |   | 

 

Print Article

Related Articles (2)

Your AI assistant can generate a working prototype in minutes — but trusting it without testing is a recipe for disaster. These 10 practical tips for writing code with generative AI come from Automators Anonymous at the University of Michigan, where members build clinical, research, and operational tools using Power Automate, Power BI, Power Apps, JavaScript, SQL, SharePoint, and AI. From prompt engineering to data validation, this is the survival guide we wish we had when we started.
A short tutorial on how to get started with generative AI tools at the University of Michigan, and tips for avoiding common pitfalls. Focus is on U-M GPT and language models GPT 3.5, GPT 4 Turbo, DALL·E 3, and Llama 2.