RStudio Autocompletion with Copilot
Last updated on 2026-01-09 | Edit this page
Estimated time: 40 minutes
Overview
Questions
- How does GitHub Copilot work as an autocompletion tool in RStudio?
- What are the best practices for getting useful suggestions from Copilot?
- How can I accept, reject, or modify Copilot suggestions?
Objectives
- Understand how GitHub Copilot generates code suggestions
- Learn techniques for writing effective prompts
- Practice accepting, rejecting, and modifying suggestions
- Develop efficient workflows using Copilot in RStudio
Using an AI assistively

GitHub Copilot functions as an advanced autocompletion tool in RStudio, going beyond simple syntax completion to suggest entire lines or blocks of code. This chapter covers how to use Copilot effectively as an autocompletion assistant.
How Copilot Works
GitHub Copilot uses machine learning models trained on billions of lines of public code to:
- Analyze the context of your current code
- Understand comments and function names
- Predict what you’re trying to accomplish
- Generate relevant code suggestions
Context is Key
Copilot examines:
- Your current file’s code
- Comments you’ve written
- Variable and function names
- The structure of your code
- Files from your RStudio project (depending on your settings)
The more context you provide, the better the suggestions!
Writing Effective Comments for Better Suggestions
Comment-Driven Development
One of the most effective ways to use Copilot is to write descriptive comments first:
Good Example:
R
# Load data from CSV file 'dat.csv', remove rows with missing values, and convert date column to Date type
Less Effective:
R
# Load data from 'dat.csv'
Challenge 1: Practice Writing Effective Comments
Write a detailed comment describing what you want the code to do for the following scenario:
You need to create a function that takes a data frame of student grades, calculates the average grade for each student, and returns only students with an average above 70.
R
# Function to calculate average grades per student and filter for high performers
# Input: data frame with columns 'student_name' and 'grade'
# Output: data frame with columns 'student_name' and 'avg_grade' for students with avg > 70
Accepting and Managing Suggestions
Keyboard Shortcuts
- Tab: Accept the entire suggestion
- Esc: Dismiss the current suggestion
- Ctrl + Shift + L or Cmd + Shift + L: Request a new suggestion
Partial Acceptance
You can accept suggestions word-by-word:
- Ctrl + → or Cmd + →: Accept next word
- This allows you to use parts of a suggestion while continuing to type
Ghost Text
Copilot suggestions appear as gray “ghost text” in your editor. This makes it easy to see suggestions without disrupting your coding flow.
Techniques for Getting Better Suggestions
1. Provide Clear Function Signatures
Start with a clear function definition:
R
# Copilot works better when you define function structure first
calculate_summary_stats <- function(data, group_var) {
# Calculate mean, median, and sd for each group
}
2. Use Meaningful Variable Names
R
# Better - descriptive names help Copilot understand context
student_grades_df <- read.csv("grades.csv")
# Less helpful
df <- read.csv("grades.csv")
3. Break Down Complex Tasks
Instead of asking for everything at once:
R
# Step 1: Load and clean data
# Load CSV file with student information
# Step 2: Calculate metrics
# Calculate average grade per student
# Step 3: Filter results
# Keep only students with average above threshold
Challenge 2: Experiment with Context
Try generating code for the same task with different levels of context:
- Just type:
read.csv( - Add a comment first:
# Load student data from grades.csvthenread.csv( - Add more context:
R
# Data has columns: student_id, name, grade, date
# Load student data from grades.csv with explicit column types
then read.csv(
You should notice that:
- With minimal context, Copilot might just complete the parentheses
- With a comment, it might suggest the filename
- With detailed context, it might suggest the filename AND additional
parameters like
stringsAsFactors = FALSEorheader = TRUE
Working with Different Types of Suggestions
Single-Line Completions
Best for:
- Completing function calls
- Finishing variable assignments
- Adding package imports
Example:
Multi-Line Suggestions
Best for:
- Function implementations
- Code blocks (if/else, loops)
- Multiple related operations
Example:
R
# Function to plot distribution with ggplot2
plot_distribution <- function(data, column) {
# Copilot may suggest entire function body
}
Questions
It is also possible to ask Copilot questions in comments:
- write a comment starting with
# q:followed by your question
For instance
R
# q: What is meant with facetting in ggplot2?
produced
R
# A: Facetting in ggplot2 refers to the process of creating multiple subplots (facets) within a single plot, based on the values of one or more categorical variables.
# This allows for easy comparison of distributions or relationships across different subsets of the data.
Why is the suggestion incomplete?
Communication between RStudio and the AI service is done in chunks and the amount of ‘tokens’ (words/pieces of words) that can be sent at once is limited. The same holds true for the response from the AI service. If your code or comment is very long, or if the AI service generates a long suggestion, it may get cut off.
In that case, it is typically sufficient to accept what has been suggested so far and then request a new suggestion (just wait) to continue from there.
Best Practices for Efficient Workflow
1. Review Before Accepting
Always read the suggestion before pressing Tab key:
- Check for logical errors
- Verify it matches your intent
- Look for security issues
2. Iterate and Refine
- Accept a suggestion as a starting point
- Modify it to fit your specific needs
- Add error handling and edge cases
3. Combine with Traditional Coding
- Use Copilot for repetitive tasks
- Code critical logic yourself
- Let Copilot help with boilerplate
Maintain Your Coding Skills
While Copilot is helpful, continue to:
- Understand the code you’re using
- Practice writing code without assistance
- Learn from the suggestions Copilot provides
Common Patterns and Use Cases
In the following, we highlight some common coding patterns where
Copilot can be particularly useful. Therein, [...]
indicates your cursor position where Copilot will provide
suggestions.
Data Manipulation with dplyr
R
# Copilot excels at suggesting dplyr pipelines
# Filter data for specific conditions and group by category
data %>%
[...]
Copilot suggests the rest..
Note: triggering extensions via comments can also be used at the end or within a pipeline to add the next step or add an intermediate transformation.
Writing Functions
Copilot will suggest the function body, e.g. using regex patterns and validation logic
Challenge 3: Build a Function with Copilot
Use Copilot to help you create a function that:
- Takes a numeric vector as input
- Removes outliers (values > 3 standard deviations from mean)
- Returns the cleaned vector
Start with a descriptive comment and function signature.
R
# Function to remove outliers from a numeric vector
# Outliers are defined as values more than 3 SD from the mean
# Input: numeric vector
# Output: numeric vector with outliers removed
remove_outliers <- function(x, sd_threshold = 3) {
# Let Copilot suggest the implementation
# It might suggest something like:
mean_x <- mean(x, na.rm = TRUE)
sd_x <- sd(x, na.rm = TRUE)
x[abs(x - mean_x) <= sd_threshold * sd_x]
}
Remember to test the function with sample data!
Troubleshooting Suggestions
Copilot Suggests Incorrect Code
- Provide more specific comments
- Add type hints or example data structures
- Break down the task into smaller steps
Advanced Tips
Setting Constraints
R
# Function must handle NA values and return informative error messages
- Copilot generates suggestions based on context from your code and comments
- Write clear, descriptive comments to get better suggestions
- Use Tab to accept, and Esc to dismiss suggestions
- Break complex tasks into smaller steps for more accurate suggestions
- Always review and test AI-generated code before using it
- Combine Copilot assistance with your own coding expertise for best results