Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The copy-paste tax

Working through a browser chat looks like this: read the error in the terminal, select it, copy, switch window, paste, add “I’m using R 4.4 and dplyr”, read the answer, copy the code, switch back, paste, fix the indentation it guessed wrong — and then realise it did not know your column names. Every step is a place to lose context.

In the editor it looks like this: select the failing lines, press Ctrl/Cmd+I, ask “why does this fail on the last group?” It already has the file, the language, the versions in your lockfile, and the error in your terminal.

This is the whole argument for the IDE. Not that the model is better — it is the same model. The context is better, and context determines whether the answer fits your project.

The trade you are making

More context means better answers and a wider blast radius. Both go up together.

Browser chatIDE completionsIDE chat / editsAgent mode
Seesyour pastethe open filefiles you namethe project
Changesnothingone linefiles you approvemany files
You reviewbefore pastingas you typethe diffthe diff
Speedslowinstantfastfast
Risk if unreadlowlowmediumhigh

The review burden does not disappear as you move right — it concentrates. One diff, read properly, replaces eight paste decisions made carelessly. That only works if you actually read the diff.

Why researchers, specifically

Genuinely good at: boilerplate you have written forty times; the syntax of a library you use twice a year; explaining code you inherited from someone who left; turning a plot you can describe into a plot that exists; writing the tests you were never going to write; documenting what you already wrote.

What it is not: a statistician; a domain expert on your assay; a guarantee that the method is right; a reason to skip the review; something that knows your data are 1-indexed, in millimetres, and missing 2019.

The honest framing: it removes the friction of writing code, and none of the responsibility for the code being right — and only one of those was ever the bottleneck.

Getting it running

Three things, in this order:

  1. A GitHub account. Use the one you will still have next year.

  2. Copilot enabled on it. A free tier exists; academic verification gives Pro.

  3. VS Code, then the GitHub Copilot extension.

Sign in when prompted. The status bar bottom-right tells you the truth about whether it worked.

Surface 1 — ghost text

Grey text that appears as you type; Tab accepts it.

KeyDoes
Tabaccept the whole suggestion
Escdismiss it
Alt/⌥+]next alternative
Ctrl/⌘+→accept one word only

Write the comment first. Ghost text is a completion engine: it continues what is there. A one-line comment stating the intent is the cheapest prompt in the whole product. Accepting one word at a time is how you stay in charge on lines you half-agree with.

Without a comment it has nothing to continue, so it guesses a filename and stops:

df = pd.read_csv("lab.csv")

With the comment:

# Load the lab export, parse the visit date column as dates,
# and keep only visits in 2025.
df = pd.read_csv("lab_export.csv",
                 parse_dates=["visit_date"])
df = df[df["visit_date"].dt.year == 2025]

The comment stays in the file afterwards. You did not write a prompt — you wrote documentation that happened to also be a prompt.

Surface 2 — inline chat

Select code, press Ctrl/⌘+I, ask in place. The answer arrives as a diff over your selection, which you accept or discard.

It is for small, local, surgical changes: “make this handle missing values”, “vectorise this loop”, “why does this fail on the last group?”, “add type hints”, “rename x to dose_mg throughout this function”.

Build the habit here. The change is scoped to what you selected, so the diff is small enough to read honestly in five seconds. Every other surface is this one with a bigger blast radius. Learn to read the diff where it is easy, and the habit survives contact with agent mode.

Surface 3 — the chat view

A panel beside your code. It has modes, and the mode is the most important setting in the product.

ModeWhat it doesWhen
Askanswers, explains, shows code. Changes nothingyou are learning, or deciding
Editproposes changes to the files you put in contextyou know what you want changed
Agentdecides which files to touch, runs commands, iteratesa task, not an edit

Start every task in Ask. It costs you one question and it is the only mode where being wrong is free.

Agent mode runs terminal commands. It asks first. Read what it is about to run — that prompt is not a formality.

Surface 4 — the sparkle in Source Control

The sparkle icon next to the commit box reads your staged diff and writes the message. It is a small feature and it is the one that quietly improves your repository the most, because the alternative most people ship is update.

Use it, then edit it. The generated message describes what changed. It cannot know why.

Filter participants missing an ID before merge

Rows with no participant_id were silently
dropped by the join, undercounting arm B.

Assisted-by: GitHub Copilot

The first line is generated. The second paragraph is yours. The third line is disclosure — see Verifying & FAIR.

The context system

Everything above is a delivery mechanism. What you attach is what determines the answer.

You typeIt attaches
#file:analysis.Rthat file, whether or not it is open
#selectionexactly what you highlighted
#codebasea search across the project for what is relevant
#terminalLastCommandthe command you just ran and what it printed
@workspace · @terminalthe project · the shell, as chat participants
/explain /fix /tests /docready-made prompts for the four commonest asks

A vague prompt with the right context beats a perfect prompt with no context. Nearly every disappointing answer is a context problem wearing a prompting costume.

One file, committed to the repository

.github/copilot-instructions.md is read automatically on every request.

# Project conventions

- R code uses tidyverse style; pipe is `|>`, not `%>%`
- Never use `setwd()`. Paths come from the `here` package
- Data files live in `data/raw/` and are read-only. Nothing writes there
- Every analysis script starts with `renv::load()`
- Plots are built with ggplot2 and saved via `ggsave()` at 300 dpi
- Do not invent column names. If a column is not in `data/schema.csv`, ask

It is version-controlled, so your conventions arrive with the repository and a new colleague and the assistant get the same briefing on day one. Six lines is enough to start. Add a line every time you correct the assistant for the same reason twice — that is the whole maintenance policy.

Things to ask for, by surface

Explain code you inherited (Ask mode). /explain on its own gives you a narration of the syntax, and you can read syntax. Add the second sentence:

/explain

What is this doing, and what assumption about the data would make it wrong?

The assumption question is the one that finds the bug: the script that assumes one row per participant, in a table that has one row per visit. Variants worth stealing: “explain in terms someone who knows statistics but not R would follow”, “what would break if the input file had an extra column?”, “which line here is the one doing the actual analysis?”

Tests you were never going to write (Edit mode).

/tests

Cover the edge cases, not the happy path: empty input, one row,
a participant with two baseline visits, and a date before the study start.

Nobody writes tests for an analysis script; everyone has been burned by an analysis script. The assistant does not get bored, and edge cases are exactly the boring part.

The error message, without leaving the editor:

#terminalLastCommand #file:merge.R

Explain this error in plain language, then tell me the two most
likely causes given the code. Don't change anything.

The stack trace, the command that produced it, and the file it came from — three pieces of context you would otherwise have copied by hand, with a good chance of copying two of the three.

Documentation (/doc, inline chat). Write the function, get it working, then select it and ask for roxygen or a docstring — the assistant is documenting something real rather than guessing at intent. Then check the @return line against what the function actually returns. That is where these are wrong, and it is the line people skip.

A real task (Agent mode):

The column `sex` is coded 1/2 in the raw files and "M"/"F" everywhere
downstream. Find every place this conversion happens, replace them with
a single function in R/recode.R, and run the tests.

It searches the project, finds four conversions — two of them subtly different — writes the function, edits four files, runs testthat, reads the failure, fixes its own mistake, and re-runs. That is a genuine hour of work in about ninety seconds.

What you do: git switch -c recode-sex before you start. Then read the diff, all of it. The interesting part is not the new function — it is the two conversions that were subtly different, and whether it was right to unify them. That question is yours. It was always yours.

R and RStudio

You are not required to switch editors.

Option

What you get

RStudio

Copilot supported since 2023.09. Tools → Global Options → Assistant → Use code assistant → GitHub Copilot. Completions only — no chat pane, no edit mode, no agent. Off by default on Server and Workbench: an admin must set copilot-enabled=1. If the option is missing, that is why.

VS Code + R extension

Everything above, for .R, .Rmd and .qmd. Needs the R extension, the languageserver package, and — for plots that behave — httpgd.

Positron

Posit’s own editor, on the VS Code codebase. RStudio’s layout, VS Code’s extensions. Its Assistant does have a chat pane; Copilot chat is in preview.

The honest recommendation: if you are productive in RStudio, check the Assistant pane. No option there means Server or Workbench — that is a ticket, not a setting.

Getting R working in VS Code

In R, once:

install.packages(c("languageserver", "httpgd"))

In VS Code, once: Extensions view → install R (REditorSupport) → install GitHub Copilot. Then in Settings, search r.plot.useHttpgd and turn it on, so plots appear in a pane instead of a window that fights your window manager.

Two comforts people miss: send line to console is Ctrl/⌘+Enter, the same as RStudio; and installing radian (a Python package, confusingly) and pointing r.rterm at it gives you syntax highlighting and multiline editing in the REPL. Quarto and R Markdown work with the Quarto extension, and chunks get ghost text like any other R code.

A pipeline from a sentence

#file:visits.csv

Write a tidyverse pipeline: one row per participant, their first visit
date, their number of visits, and mean systolic BP across visits.
Use `|>`. Drop participants with no visits rather than giving them NA.
summary_df <- visits |>
  filter(!is.na(visit_date)) |>
  group_by(participant_id) |>
  summarise(
    first_visit = min(visit_date),
    n_visits    = n(),
    mean_sbp    = mean(sbp, na.rm = TRUE),
    .groups     = "drop"
  )

Three things to check: na.rm = TRUE — it chose that; is a mean over the visits that happen to have a reading the number you want? .groups = "drop" is right, but check — forgetting it is how a later mutate surprises you. And did it use the real column names, or plausible-looking ones? Attaching the CSV is what makes the third item pass.

Visualisation, and the iteration loop

Round 1: “Plot mean_sbp against first_visit, coloured by study arm, with a loess smoother per arm.” Round 2: “Colourblind-safe palette. Move the legend to the bottom. Axis label ‘Systolic BP (mmHg)’.” Round 3: “Facet by site. Free y scales. Save as figures/sbp_by_arm.png at 300 dpi, 180 mm wide.”

You see each version. The plot is the feedback, and it is far faster than describing what you want in advance. This is the loop that does not exist in a browser: prompt → plot → look → prompt. Three rounds take under a minute and you end with a figure you could put in a paper.

Round 3 is the one to keep in the script. Ask it to collapse the three rounds into one clean ggplot call before you commit — chat history is not a figure caption.

The reproducibility asks

Every one of these is a job you have been meaning to do for a year, that takes ninety seconds:

This is the underrated use of the tool — not writing new analysis, but paying down the reproducibility debt in the analysis you already wrote.

Where R specifically trips it up

The fix is one file. Put your dialect in .github/copilot-instructions.md once:

- Base pipe `|>` only, never `%>%`
- dplyr, not data.table
- Always TRUE/FALSE, never T/F
- Every script ends with sessionInfo()

Then stop correcting it by hand every time. Invented function names are the most common R failure and the easiest to catch — run the code. Silent wrongness is the kind to fear.