3.12 AI Tools for R and BI
Artificial intelligence tools are changing how analysts write, debug, and learn code. This section introduces the major categories of AI coding assistants and explains how to use them effectively alongside the R skills developed in this chapter.
3.12.1 Types of AI Coding Assistants
AI coding assistants fall into two broad categories:
GUI-based tools run in a web browser or application window. You type a prompt — such as “write R code to calculate the mean of each column in a data frame” — and the AI returns code you can copy into RStudio. Examples include ChatGPT (OpenAI), Claude (Anthropic), and Google Gemini. These tools are easy to start with and require no installation beyond a web browser.
CLI-based tools (command-line interface) run in your computer’s terminal and work directly with your project files. Rather than copying and pasting code between a browser and RStudio, a CLI tool can read your scripts, edit them in place, run code, and iterate based on errors — all within your project directory. This makes them particularly powerful for multi-step analysis tasks. The primary CLI tool used in this textbook is Claude Code (Anthropic 2025), Anthropic’s terminal-based AI coding agent. We use Claude Code because it works directly within your project directory — reading, editing, and running your files in context — which makes it particularly effective for iterative data analysis. The R skills you learn in this chapter apply regardless of which AI tool you choose. Other CLI and editor-integrated options include GitHub Copilot (Ziegler et al. 2024), which auto-completes code as you type in supported editors, and Cursor, an AI-native code editor.
3.12.2 Working with Claude Code
Claude Code is a terminal-based AI assistant that you interact with by typing natural language commands. Unlike GUI-based tools, Claude Code operates directly in your project directory — it can read your R scripts, understand their context, make edits, and run code to verify that changes work.
A typical workflow with Claude Code looks like this:
- Open your terminal and navigate to your project directory
- Launch Claude Code by typing
claude - Describe what you want in plain English: “Load the absenteeism dataset from data/Absenteeism_at_work.csv and show me the first few rows”
- Claude Code writes the R code, explains what it does, and can execute it
- You review the output, ask follow-up questions, or request modifications
This iterative, conversational workflow is especially valuable when exploring unfamiliar data or debugging code. Rather than searching Stack Overflow or documentation, you can ask the AI to explain an error message, suggest an alternative approach, or walk through the logic of a complex function.
Note that Claude Code requires an Anthropic account and usage incurs API costs. Your instructor may provide access through a class account, or you can create your own at https://console.anthropic.com/. The GUI-based tools (ChatGPT, Claude web, Gemini) offer free tiers that are sufficient for the exercises in this book. AI tools evolve rapidly — consult current documentation for the latest pricing, features, and interface details.
For installation instructions, see the Appendix.
Example: Using Claude Code to Explore Data
Here is what a real interaction with Claude Code might look like. The student types a natural language request, and Claude Code generates R code and explains the result.
Student prompt:
Load the mtcars dataset and tell me which car has the best fuel efficiency
Claude Code response:
# Load the built-in mtcars dataset
data(mtcars)
# Find the car with the highest mpg
best_mpg <- mtcars[which.max(mtcars$mpg), ]
print(best_mpg)“The Toyota Corolla has the best fuel efficiency in the mtcars dataset at 33.9 miles per gallon. It has a 4-cylinder engine with 71.1 horsepower.”
What the student should verify: Does which.max() do what you expect? Is “best fuel efficiency” the same as “highest mpg”? What if there were a tie? Running the code yourself and inspecting the data frame confirms that the answer is correct — but developing the habit of verification is the point.
3.12.3 Why R Skills Still Matter
AI coding assistants can generate syntactically correct R code in seconds. This raises a natural question: why learn R at all?
The answer is that AI tools are powerful but not infallible. They can produce code that runs without errors but produces incorrect results — for example, applying the wrong statistical test, mishandling missing values, or creating a misleading visualization. A practitioner who does not understand R cannot catch these mistakes. The R fundamentals covered in this chapter — data types, data structures, functions, control flow — give you the vocabulary to read AI-generated code critically, verify its logic, and modify it when needed (Denny et al. 2024).
Think of AI coding assistants the way you might think of a GPS navigation system. A GPS can get you to your destination faster, but a driver who does not understand roads, traffic rules, or how to read a map is dangerous behind the wheel. Similarly, an analyst who uses AI to generate code without understanding what that code does is a liability. The goal of this textbook is to make you a skilled driver who also knows how to use GPS effectively.