Smart Data Analysis

How AI Assistants Can Elevate Your R Programming

Kamarul Imran Musa

Universiti Sains Malaysia

Learning Objectives

  1. Understand the Data Analysis Workflow (REAAAPP)

  2. Explore AI Assistants and their roles in data analysis

  3. Learn different approaches to integrate AI Assistants with R programming

Based on your experience with R (beginner, intermediate, advanced)

Biography

Career and Interests

  • Public Health Medicine Specialist and Professor in Epidemiology and Statistics

  • Interest in disease modelling using epidemiological, statistical and AI methods

  • Introduce R in the postgrad programme in 2014

  • Listed in the top 2% scientists since 2021

  • Complete biography at myanalytics.com.my

School of Medical Sciences, Universiti Sains Malaysia

Data Analysis in Health and Medicine Using R

Biography

Max Kuhn (tidymodels)

Roger Peng

Jenny Bryan

Peter Diggle

Part 1: Data Analysis Workflow

Understanding the REAAAPP Workflow

The REAAAPP Workflow

A systematic approach to data analysis and statistical modelling

R - Reading Data

E - Exploring Data

A - Making Assumptions

A - Applying models and methods

A - Assessing results and models

P - Presenting results and interpretations

P - Publishing results, data, and codes

Workflow Visualization

%%{init: {'theme':'base', 'themeVariables': { 'fontSize':'18px'}}}%%
flowchart LR
    A[Read] --> B[Explore] --> C[Assume] --> D[Analyze] --> E[Assess] --> F[Present] --> G[Publish]

    style A fill:#e74c3c,stroke:#333,stroke-width:2px,color:#fff
    style B fill:#f39c12,stroke:#333,stroke-width:2px,color:#fff
    style C fill:#f1c40f,stroke:#333,stroke-width:2px,color:#333
    style D fill:#2ecc71,stroke:#333,stroke-width:2px,color:#fff
    style E fill:#3498db,stroke:#333,stroke-width:2px,color:#fff
    style F fill:#9b59b6,stroke:#333,stroke-width:2px,color:#fff
    style G fill:#1abc9c,stroke:#333,stroke-width:2px,color:#fff

Do not rush or you might do stupid mistakes. Know your workflow.

Part 2: AI Assistants

AI Assistants for R Programming

Before AI Assistant

Googling for Codes

Pre-AI Data Analysis Workflow

Google — Copy and paste codes

Read books — Copy and paste codes

Stack Overflow — Ask questions, wait for answers, copy and paste codes

Major AI Assistants for R

Assistant Key Strengths Best Use Case
ChatGPT Broad knowledge, conversational General R coding & learning
Claude Large context, file creation Complex analysis, reports, code review
Gemini Google integration, research Multi-modal data analysis
Copilot IDE integration Real-time coding in RStudio/VS Code

Each AI assistant has unique capabilities — choose based on your specific needs

R Prompt Generator

A Custom Tool for Structured Prompts

Access: claude.ai/public/artifacts/55725b79…

Aligned with REAAAPP workflow — Guides you through each stage

Reduces ambiguity — Clear, structured prompt templates

Improves AI responses — Better prompts = better code

Ensures completeness — Covers all aspects of analysis

Saves time — Reusable templates for common tasks

R Prompt Generator

The R Prompt Generator helps you create effective prompts for AI assistants, making your interaction more productive

Part 3: Integration Approaches

Integrating AI with Your R Workflow

Three Approaches to Integrate AI with R

Based on your workflow preferences and technical comfort level

3 groups of R users and Data Analysts

Approach 1: Web-Based Workflow

Using AI Assistants via Browser

  1. Generate Prompt — Use R Prompt Generator or write your own
  1. Submit to AI — ChatGPT, Claude, or Gemini web interface
  1. Add Context — Share data samples, requirements
  1. Get R Code — AI generates complete R solution
  1. Copy to RStudio — Paste and test in your environment
  1. Iterate — Refine prompts based on results

Advantages: Easy to start, no setup, conversational Limitations: Manual copy-paste, context management

Approach 2: AI Assistant Package

Using AI via R Packages (ellmer)

library(ellmer)

# Set up chat with system prompt
chat <- chat_openai(system_prompt =
  "You are an expert R programmer who prefers tidyverse.")

# Request R code
response <- chat$chat("Generate R code to:
  1. Read CSV file
  2. Calculate summary statistics by group
  3. Create a ggplot visualization")

# Use the generated code
cat(response)

Advanced Features: Branching conversations, automated workflows, batch processing, useful in RStudio and Positron IDE

Approach 3: AI Assistant and IDE Integration

AI Assistants Within Your Coding Environment

GitHub Copilot

  • Autocomplete R code
  • Inline suggestions
  • Works in Positron IDE and VS Code
  • Subscription required

Claude Code

  • Positron IDE and VS Code extension
  • File operations
  • Command execution
  • Advanced features
  • Subscription required

Benefits:

  • Real-time coding assistance
  • Context-aware suggestions
  • Seamless workflow, No copy-paste needed

Best For:

  • Active development
  • Rapid prototyping
  • Complex projects

Approach 3: AI Assistant and IDE Integration

Claude and VS Code

Approach 3: AI Assistant and IDE Integration

Claude Code - CLI

Avoid These Traps

During Analysis

  • Your prompts are vague
  • Not providing additional resources
  • Trying to be perfect (prompts too long, too many resources)
  • Plan too little

After Analysis

  • Do not verify
  • Do not validate
  • Do not clean the codes
  • Let AI be your master
  • Spending too much time redoing

Instead of saving time and be more effective, you will spend time correcting your codes and getting more annoyed

Getting Started

Three Simple Steps

1. Choose Your Approach

  • Start with web-based (easiest)
  • Explore AI packages (ellmer) when ready
  • Progress to AI and IDE integration

2. Use R Prompt Generator

  • Access the tool
  • Follow REAAAPP stages
  • Generate structured prompts

3. Practice and Iterate

  • Start with simple tasks
  • Build your prompt library
  • Share with your team

Summary

REAAAPP Workflow provides structure

+

AI Assistants accelerate analysis

+

Your Domain Expertise ensures quality

=

Efficient, Reproducible Data Analysis

Thank You

Questions?

Email: drki.musa@gmail.com

Website: myanalytics.com.my

LinkedIn: Kamarul Imran Musa

Presentation link: https://bit.ly/ai_assist_r