Chapter 8 Setting Up GitHub Copilot
GitHub Copilot is your AI-powered coding assistant that suggests code in real-time as you type. Let’s get it working in both RStudio and VSCode!
What you’ll accomplish:
- ✅ Activate GitHub Copilot in VSCode
- ✅ Activate GitHub Copilot in RStudio
- ✅ Learn how to use Copilot effectively
- ✅ Understand best practices and limitations
8.1 Prerequisites
Before setting up Copilot, make sure you have:
Student benefits approval required!
If your GitHub Student Benefits application is still pending, wait for approval before proceeding. This usually takes a few hours to 2 business days.
Check your approval status at: https://education.github.com/
8.2 Part 1: Setting Up Copilot in VSCode
VSCode has the most mature and feature-rich Copilot integration.
8.2.1 Step 1.1: Sign In to GitHub Copilot
Open VSCode
Look at the bottom-right corner of the window (Status Bar)
You should see a Copilot icon (looks like a small GitHub logo or sparkle ✨)
Click the Copilot icon → Select “Sign in to Use AI Features”
8.2.3 Step 1.3: Verify Copilot is Active
After authorization, the Copilot icon in the Status Bar should show a checkmark or green indicator:
Status Bar indicators:
- ✅ Green checkmark or sparkle icon: Copilot is active and ready
- ⚠️ Warning icon: Not signed in or subscription issue
- 🚫 Disabled icon: Copilot is turned off (click to re-enable)
8.2.4 Step 1.4: Test Copilot in VSCode
Let’s see Copilot in action!
- Create a new file: File → New File
- Select language: “R”
- Start typing a comment describing what you want to do:
- Pause for 1-2 seconds after typing the comment
- Copilot will suggest code in gray “ghost text”:
# Function to calculate the mean and standard error of a vector
calc_mean_se <- function(x, na.rm = TRUE) {
mean_val <- mean(x, na.rm = na.rm)
se_val <- sd(x, na.rm = na.rm) / sqrt(length(x[!is.na(x)]))
return(list(mean = mean_val, se = se_val))
}- Press Tab to accept the suggestion!
Copilot keyboard shortcuts:
- Tab: Accept the current suggestion
- Esc: Dismiss the suggestion
- Alt+] (Windows/Linux) or Option+] (macOS): Next suggestion
- Alt+[ or Option+[: Previous suggestion
- Ctrl+Enter / ⌘+Enter: Open Copilot suggestions panel (see multiple options)
8.2.5 Step 1.5: Using GitHub Copilot Chat
Copilot Chat lets you ask questions and get help interactively!
Open Copilot Chat: Click the chat icon in the Activity Bar (left side) or press
Ctrl+Alt+I/⌘+Option+IType a question, for example:
How do I create a boxplot in R using ggplot2?
- Copilot Chat will provide a detailed answer with code examples!
Copilot Chat vs Inline Suggestions:
- Inline suggestions (ghost text): Quick code completions as you type
- Copilot Chat: Ask questions, get explanations, refactor code, debug errors
Use both depending on what you need!
8.3 Part 2: Setting Up Copilot in RStudio
RStudio has built-in GitHub Copilot support (version 2023.09.0 and later).
8.3.1 Step 2.1: Enable Copilot in RStudio
Open RStudio
Go to Tools → Global Options
Click “Copilot” in the left sidebar
Check the box: “Enable GitHub Copilot”
RStudio will download the Copilot Agent (this happens once, takes ~1 minute)
8.3.2 Step 2.2: Sign In to GitHub Copilot
After enabling, click the “Sign In” button in the Copilot settings
A dialog will appear showing a verification code (8 characters)
Copy the verification code
Click the link or navigate to: https://github.com/login/device
Paste the verification code and click “Continue”
GitHub will ask for permissions — click “Authorize GitHub Copilot Plugin”
You’ll see a success message: “Congratulations, you’re all set!”
Go back to RStudio — the Copilot settings should now show your GitHub username
8.3.3 Step 2.3: Test Copilot in RStudio
- Close the Global Options dialog
- Create a new R script: File → New File → R Script
- Type a comment describing what you want:
Pause — Copilot will suggest code in light gray text
Press Tab to accept
RStudio Copilot shortcuts:
- Tab: Accept suggestion
- Esc: Dismiss suggestion
- Ctrl+Space (Windows/Linux) / Ctrl+Space (macOS): Force autocomplete (shows both Copilot and regular suggestions)
- Ctrl+Shift+P / ⌘+Shift+P: Open Command Palette → Search “Copilot” for more options
8.4 Part 3: Using Copilot Effectively
Now that Copilot is set up, let’s learn how to use it like a pro!
8.4.1 Best Practices for Copilot
8.4.1.1 1. Write Clear, Descriptive Comments
Copilot works best when you describe what you want in plain English:
✅ Good:
❌ Vague:
8.4.2 Examples: Copilot in Action
8.4.3 Asking Questions with Copilot
You can ask Copilot questions using a special comment format:
Copilot will respond with an answer comment:
# q: What is the difference between aov() and lm() in R?
# a: aov() is a wrapper for lm() specifically designed for ANOVA models.
# It provides a cleaner summary output and assumes categorical predictors.
# lm() is more general and can handle continuous and categorical predictors.For longer conversations:
Use Copilot Chat in VSCode for more interactive Q&A! It’s better suited for extended explanations and troubleshooting.
8.5 Part 4: Copilot Limitations and Ethics
8.5.1 What Copilot Can Do Well
- ✅ Generate boilerplate code (repetitive structures)
- ✅ Suggest function implementations based on names and comments
- ✅ Help with syntax you’re not familiar with
- ✅ Autocomplete common patterns (like dplyr pipes)
- ✅ Translate concepts into code (e.g., “calculate standard error”)
8.5.2 What Copilot Struggles With
- ❌ Complex logic requiring deep domain knowledge
- ❌ Novel algorithms not commonly found online
- ❌ Debugging subtle errors in existing code
- ❌ Understanding your specific data without clear context
- ❌ Security-sensitive code (may suggest insecure patterns)
8.5.3 Academic Integrity
Using Copilot in this course:
- ✅ Allowed: Using Copilot to help write code, learn syntax, get suggestions
- ✅ Allowed: Using Copilot to debug errors and understand concepts
- ❌ NOT allowed: Submitting Copilot-generated code without understanding it
- ❌ NOT allowed: Using Copilot to generate entire assignment solutions without your own analysis
Think of Copilot as a tutor, not a solution generator.
You must: - Understand every line of code you submit - Modify Copilot suggestions to fit your specific problem - Cite when appropriate (e.g., “with assistance from GitHub Copilot”) - Learn from the suggestions, don’t just copy-paste
8.5.4 Privacy and Data
What Copilot sees:
- Your code in the current file
- Comments and variable names
- Code from other files in your project (if indexing is enabled)
What Copilot does NOT see:
- Your data values (only code structure)
- Files you haven’t opened
- Private information unless you include it in code
Best practice: Don’t include sensitive information (passwords, API keys, private data) in your code!
8.6 Video Tutorial: Using GitHub Copilot
Video not loading?
Watch on YouTube: GitHub Copilot in 7 Minutes
8.7 Summary Checklist
Before finishing this guide, make sure you have:
Congratulations! 🎉
You’ve completed the entire setup guide! You now have:
- ✅ A GitHub account with student benefits
- ✅ VSCode configured for R and Quarto
- ✅ RStudio with R and essential packages
- ✅ Quarto for creating documents and reports
- ✅ GitHub Copilot as your AI coding assistant
You’re ready to start the course!
Final Notes: See the Missing Details section below for additional important information!