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)
- 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.
- Open RStudio and click the Posit Assistant button on the toolbar (upper right)
- In the Assistant panel, click the gear icon, then click Configure LLM providers

- Scroll down to OpenAI Compatible (not the option that just says "OpenAI") and click Configure

- 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 
- 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)
- Download and install ollama or your preferred ollama-compatible local LLM server.
- Open a command prompt / terminal and and install a model locally by typing
ollama pull model_name, then hit Enter.
- You can browse models with the command
ollama ls or by visiting https://ollama.com/search
- Qwen Coder is a good starter model for most coding tasks (
ollama pull qwen2.5-coder:7b)
- Launch ollama
- Open RStudio and click the Posit Assistant button on the toolbar (upper right)
- In the Assistant panel, click the gear icon, then click Configure LLM providers

- Scroll down to Ollama and click Configure

- Enter the local ollama URL (the default is
http://127.0.0.1:11434). Click Test to ensure the connection works, then click Save 
- 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.

- 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.
| | |
|