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

  1. Open VSCode

  2. Look at the bottom-right corner of the window (Status Bar)

  3. You should see a Copilot icon (looks like a small GitHub logo or sparkle ✨)

  4. Click the Copilot icon → Select “Sign in to Use AI Features”

8.2.2 Step 1.2: Authorize VSCode with GitHub

  1. VSCode will open a browser window asking you to authorize GitHub Copilot

  2. Sign in to GitHub if not already signed in

  3. Review the permissions requested

  4. Click “Authorize Visual-Studio-Code”

  5. You may be asked to allow VSCode to open the link — click “Open”

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!

  1. Create a new file: File → New File
  2. Select language: “R”
  3. Start typing a comment describing what you want to do:
# Function to calculate the mean and standard error of a vector
  1. Pause for 1-2 seconds after typing the comment
  2. 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))
}
  1. 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!

  1. Open Copilot Chat: Click the chat icon in the Activity Bar (left side) or press Ctrl+Alt+I / ⌘+Option+I

  2. Type a question, for example:

How do I create a boxplot in R using ggplot2?
  1. 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

  1. Open RStudio

  2. Go to Tools → Global Options

  3. Click “Copilot” in the left sidebar

  4. Check the box: “Enable GitHub Copilot”

  5. RStudio will download the Copilot Agent (this happens once, takes ~1 minute)

8.3.2 Step 2.2: Sign In to GitHub Copilot

  1. After enabling, click the “Sign In” button in the Copilot settings

  2. A dialog will appear showing a verification code (8 characters)

  3. Copy the verification code

  4. Click the link or navigate to: https://github.com/login/device

  5. Paste the verification code and click “Continue”

  6. GitHub will ask for permissions — click “Authorize GitHub Copilot Plugin”

  7. You’ll see a success message: “Congratulations, you’re all set!”

  8. Go back to RStudio — the Copilot settings should now show your GitHub username

8.3.3 Step 2.3: Test Copilot in RStudio

  1. Close the Global Options dialog
  2. Create a new R script: File → New File → R Script
  3. Type a comment describing what you want:
# Create a function to perform a t-test and return results as a data frame
  1. Pause — Copilot will suggest code in light gray text

  2. 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.3.4 Step 2.4: Additional Copilot Settings in RStudio

In Tools → Global Options → Copilot, you can configure:


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:

# Calculate mean yield for each treatment group, removing NA values

Vague:

# Do the thing

8.4.1.2 2. Provide Context

Give Copilot information about your data structure:

# Data frame 'crop_data' has columns: treatment, yield, block
# Calculate the mean and standard error of yield for each treatment

8.4.1.3 3. Break Down Complex Tasks

Instead of asking for everything at once, work step-by-step:

# Step 1: Filter data for treatment A

# Step 2: Calculate summary statistics

# Step 3: Create a boxplot

8.4.1.4 4. Review and Test Suggestions

Copilot is not always correct!

Always:

  • Read the suggested code
  • Understand what it does
  • Test it with your data
  • Modify if needed

Never blindly accept code without understanding it!

8.4.2 Examples: Copilot in Action

8.4.2.1 Example 1: Data Manipulation

Your comment:

# Load tidyverse and create a summary table with mean, sd, and n for each group

Copilot suggests:

library(tidyverse)

summary_table <- data %>%
  group_by(treatment) %>%
  summarize(
    mean = mean(yield, na.rm = TRUE),
    sd = sd(yield, na.rm = TRUE),
    n = n()
  )

8.4.2.2 Example 2: Statistical Analysis

Your comment:

# Perform ANOVA to test effect of treatment on yield, check assumptions

Copilot suggests:

# Fit ANOVA model
model <- aov(yield ~ treatment, data = crop_data)

# Check assumptions
par(mfrow = c(2, 2))
plot(model)

# ANOVA table
summary(model)

8.4.2.3 Example 3: Data Visualization

Your comment:

# Create a boxplot of yield by treatment using ggplot2, with custom colors

Copilot suggests:

ggplot(crop_data, aes(x = treatment, y = yield, fill = treatment)) +
  geom_boxplot() +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "Yield by Treatment",
       x = "Treatment",
       y = "Yield (kg/ha)") +
  theme_minimal()

8.4.3 Asking Questions with Copilot

You can ask Copilot questions using a special comment format:

# q: What is the difference between aov() and lm() in R?

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!