---
title: "GitHub Copilot"
date-meta: 2023-08-25
---

[GitHub Copilot](https://docs.github.com/en/copilot) is an "AI pair programmer that offers autocomplete-style suggestions as you code". GitHub Copilot is available as an opt-in integration with RStudio. To provide feedback or report bugs, please open a [GitHub Issue on the RStudio repository](https://github.com/rstudio/rstudio/issues).

## Prerequisites

-   To use GitHub Copilot, you must have a GitHub account and an active subscription to Copilot for Individuals or Copilot for Business. For more information, see [billing for GitHub Copilot](https://docs.github.com/en/billing/managing-billing-for-github-copilot/about-billing-for-github-copilot).
-   Students and faculty can use GitHub Copilot for free as part of the GitHub Education program. For more information, see the [GitHub Education page](https://education.github.com/).
-   To use GitHub Copilot in RStudio, you must have a compatible version of RStudio installed. GitHub Copilot is available for RStudio Desktop 2023.09.0 and later. GitHub Copilot is disabled by default in RStudio Server and Posit Workbench, but can be enabled by an administrator.
-   To use GitHub Copilot, you must have access to the internet in order to send requests to the Copilot APIs and receive suggestions from GitHub Copilot.

## Setup

::: {.callout-note title="Using GitHub Copilot with RStudio Server and Posit Workbench"}
With RStudio Server and Posit Workbench, GitHub Copilot must first be explicitly enabled by a system administrator. This can be done by adding the line:

```         
copilot-enabled=1
```

to `/etc/rstudio/rsession.conf`.
:::

To enable GitHub Copilot in RStudio:

1.  Navigate to Tools \> Global Options \> Assistant.\
2.  Select "GitHub Copilot" from the "Use code assistant" dropdown.\
3.  Download and install the Copilot Agent components.\
4.  Click the "Sign In" button.\
5.  In the "GitHub Copilot: Sign in" dialog, copy the Verification Code.\

![](images/copilot_sign-in.png){fig-alt="GitHub Copilot: Sign in"}

6.  Navigate to or click on the link to <https://github.com/login/device>, paste the Verification Code and click "Continue".\
7.  GitHub will request the necessary permissions for GitHub Copilot. To approve these permissions, click "Authorize GitHub Copilot Plugin".\
8.  After the permissions have been approved, your RStudio IDE will indicate the currently signed in user.\
9.  Close the Global Options dialogue, open a source file (`.R`, `.py`, `.qmd`, etc) and begin coding with Copilot!

To disable GitHub Copilot either:

-   Navigate to Tools \> Global Options \> Assistant and select a different option in the "Use code assistant" dropdown, or\
-   Use the Command Palette <kbd>Cmd+Shift+P</kbd> on Mac or <kbd>Ctrl+Shift+P</kbd> on Windows/Linux, search for "AI Assistant" and then select a different option.

### Using GitHub Copilot with Proxy Servers

If you are using RStudio in an environment with limited public internet access, you may need to first instruct the GitHub Copilot agent to use your enterprise's internal proxy server. For RStudio Server and Posit Workbench, this can be done by a system administrator, by setting:

```         
copilot-proxy-url=<url>
```

in `/etc/rstudio/rsession.conf`. Alternatively, for RStudio Desktop installations, you can set the environment variable:

```         
COPILOT_PROXY_URL=<url>
```

in an appropriate R start-up file; for example, your own `~/.Renviron`, or in your R installation's `etc/Rprofile.site` file. Please consult `?Startup` in an R session for more details.

### Using Custom Certificates with GitHub Copilot

If you are using RStudio in an environment with custom SSL certificates, you may need to first instruct the GitHub Copilot agent to use your enterprise's custom SSL certificates. For RStudio Server and Posit Workbench, this can be done by a system administrator, by setting:

```         
copilot-ssl-certificates-file=<path>
```

in your `/etc/rstudio/rsession.conf`. Note that this is functionally equivalent to setting the `NODE_EXTRA_CA_CERTS` environment variable for the GitHub Copilot agent process -- see https://nodejs.org/api/cli.html#node_extra_ca_certsfile for more details.

You can also enable **Tools > Global Options > Assistant > "Use the system certificate store"**. This works in all modes and is the recommended approach on RStudio Desktop, where `rsession.conf` is not available. When enabled, RStudio trusts the operating system certificate store (the Windows Certificate Store or macOS Keychain) in addition to Node.js's built-in certificate authorities, which is usually all that is needed behind a TLS-inspecting proxy whose root CA the machine already trusts. The setting applies to both the GitHub Copilot agent and the Posit Assistant. Restart the R session for the change to take effect.

This option is additive: it supplements `NODE_EXTRA_CA_CERTS` and `copilot-ssl-certificates-file` rather than replacing them. It requires the bundled Node.js (or any configured Node.js 22.17.0 or newer); with an older configured Node.js the option is ignored.

::: callout-warning
In some enterprise environments -- particularly certain SASE/ZTNA products such as Check Point Harmony -- the operating system trust store contains several CA certificates that share the same subject name but use different keys. The operating system tolerates this (it tries each name match until one verifies), but Node.js and OpenSSL select the first certificate matching the subject and may fail with `CERT_SIGNATURE_FAILURE` if that is not the right one. In such environments, enabling **"Use the system certificate store"** can prevent the assistant from connecting rather than help it; leave the option disabled.

Note also that the GitHub Copilot language server reads the operating system trust store on its own, independent of this setting and of `NODE_EXTRA_CA_CERTS`. Because `NODE_EXTRA_CA_CERTS` can only add certificates, supplying a clean single-CA PEM does not exclude conflicting duplicates that the language server picks up from the system store for GitHub Copilot. When duplicate same-subject certificates cause connection failures, the reliable fix is to remove the duplicate or invalid certificates from the operating system trust store.
:::

### External Services Used by GitHub Copilot

The GitHub Copilot agent process will communicate with the following external services when authenticating the current user, and also when generating code suggestions:

-   github.com
-   copilot-proxy.githubusercontent.com
-   api.githubcopilot.com
-   default.exp-tas.com

If you are having trouble setting up GitHub Copilot in an environment with limited public internet access, you may need to ensure that your network allows access to these services.

### Copilot Diagnostics

The GitHub Copilot agent used by RStudio can also generate a diagnostics report. This report is accessible from the Assistant preferences pane, using the `Diagnostics` button:

![](images/copilot-diagnostics-button.png){fig-alt="GitHub Copilot: Diagnostics button"}

After clicking this button, you will be presented with a small diagnostics report produced by the Copilot Agent. For example:

![](images/copilot-diagnostics-report.png){fig-alt="GitHub Copilot: Diagnostics report"}

This report may be useful when diagnosing Copilot connectivity issues.

## Using Copilot

Copilot offers autocomplete-style suggestions as you code as "ghost text". This ghost-text is similar to the existing autocomplete available in RStudio but importantly is a generated suggestion rather than an exact auto-completion.

![](images/copilot-rstudio.png){fig-alt="Copilot in RStudio"}

1.  A simple but specific comment providing additional context to Copilot.
2.  Copilot's code suggestion, shown in light grey "ghost text".
3.  The Copilot status bar, which indicates whether RStudio is waiting on a response to be generated, a completion response has been received, or no completions are available.

GitHub Copilot primarily relies on the context in the file you are actively editing. Any comments, code, or other context provided within the active document will be used as a "prompt" that Copilot will then use to provide a suggested completion. To expand the scope of the context used by Copilot beyond just the active document, there is a setting to also index and read from other [supported files](#language-support) in the current project. This setting can be toggled on or off in the Tools \> Global Options \> Copilot \> "Index project files with GitHub Copilot" setting.

For general advice on how to use Copilot, please see:

-   [How to use GitHub Copilot: Prompts, tips, and use cases](https://github.blog/2023-06-20-how-to-write-better-prompts-for-github-copilot/)\
-   [Learning GitHub Copilot video series](https://learn.microsoft.com/en-us/shows/introduction-to-github-copilot/)

::: {.callout-warning title="Trust but Verify"}
While Copilot often generates useful and functional code, it is important to note that the suggestions are not always valid code examples or completely solve the intended problem. Copilot suggestions are non-deterministic and Posit does not guarantee the quality, accuracy, or security of the outputs. It is important to review the suggestions and ensure that they are both accurate and appropriate for the intended use case. Copilot may generate code that contains insecure coding patterns, bugs, or outdated practices. You should always use best practices in code review, testing, and adherence to security standards when using Copilot. For more information, see the [FAQs on GitHub Copilot](https://github.com/features/copilot#faq) and [GitHub Copilot for Business](https://docs.github.com/en/copilot/overview-of-github-copilot/about-github-copilot-for-business).
:::

### Autocomplete vs Copilot

The existing autocomplete in RStudio is a static list of possible completions based on the session state. Copilot is a dynamic predicted suggestion that is generated based on the document and project context, including comments, code elsewhere in the document or project, and the currently focused characters. Copilot will also provide suggestions for code that is not currently in the active document.

Similar to normal autocomplete, Copilot suggestions will begin after a short delay. If you want to prompt Copilot directly in a new file, you can start typing a descriptive variable or function name and then pause for a moment. Copilot will then generate a suggestion based on the current context.

At times, normal autocomplete and Copilot may seem to conflict with each other. In these cases, it is best to review the Copilot suggestion and determine if it is appropriate for the current context. If it is, you can accept the suggestion by pressing <kbd>Tab</kbd>. If it is not, you can ignore the suggestion and continue typing or force the normal autocomplete to show by pressing <kbd>Ctrl+Space</kbd>. You can also toggle Copilot on and off for a particular document using the `Copilot: Toggle Automatic Completions` command, accessible from the Command Palette via <kbd>Ctrl+Shift+P</kbd>.

You can also disable the "Show code completions: Automatically" setting in the Tools \> Global Options \> Code \> Completion section.

![](images/autocomplete-settings.png){fig-alt="Autocomplete settings in RStudio"}

### Code

The primary use case for Copilot is to generate code. Copilot will use the context of the active document to generate suggestions for code that might be useful.

Copilot suggestions will be presented as grey "ghost text" which may be accepted by pressing the <kbd>Tab</kbd>, while the <kbd>Enter/Return</kbd> key will ignore the suggestion. Some suggestions will be a single entire line, inline of other code, or others with broader context may span multiple lines.

![](images/copilot_ghost-text.png){fig-alt="Copilot ghost text"}

Code suggestions are typically most useful when applied to a well-scoped and specific problem. When trying to solve larger problems or write longer functions, it is best to break the problem down into smaller pieces and use Copilot and your own expertise to generate code for each chunk. Similar to how a chef might use a recipe to cook each dish that makes up a larger meal, Copilot can be used to generate code for smaller pieces of a larger problem.

::: {.callout-info title="Copilot response limits"}
There are upper limits to the amount of characters or code that can be returned at any one time, commonly referred to as "tokens". If the token limit is exceeded on a suggestion, Copilot will usually return a truncated suggestion. This is most common when Copilot is used to generate code for a long function or other multi-line code block. To avoid truncated outputs, it is best to scope the problem down into several smaller prompts or problems. Alternatively, after accepting the "partial" response, you could finish writing the rest of the code yourself or add additional inline comments to prompt Copilot to suggest additional outputs to complete the remainder of the code snippet.
:::

### Questions

While Copilot is primarily intended to generate code, it can also be used to directly answer simple questions. To specifically ask a question of Copilot, use a comment with a `# q:` at the beginning and a question mark at the end. Copilot's completion should be an "answer" comment `# a:`.

For example, the below:

``` r
# q: What is the definition of standard error?
```

Might generate an answer like:

``` r
# q: What is the definition of standard error?
# a: The standard error is the standard deviation of the sampling distribution of a statistic.
```

::: {.callout-note title="Chat interfaces"}
Copilot is most effective when used to generate code, so it is best to use purpose-built generative AI interfaces for chat, such as the [`{chattr}` R package](https://mlverse.github.io/chattr/) which is an R interface to local or remote LLMs (Large Language Models). `{chattr}` includes a RStudio add-in as well as a built in app for interacting with LLMs via RStudio's Viewer Pane.
:::

### Comments

When using Copilot to solve a broad problem, it is a good idea to provide a high-level goal at the beginning of the file. This will help Copilot provide suggestions that are relevant to the specific problem you are trying to solve.

``` r
# This script will do x
# using the packages x,y,z
# other constraints or details
```

A real example might be:

``` r
# Create a robust function to calculate the standard error of a vector
# The function name will be calc_se with an argument x for the vector, with 
# an argument to remove missing values called na.rm, and a default value of TRUE
# The function should have type checking to only allow numeric vectors
```

Which might generate something like the below:

``` r
calc_se <- function(x, na.rm = TRUE) {
  if (!is.numeric(x)) {
    stop("x must be numeric")
  }
  if (na.rm) {
    x <- x[!is.na(x)]
  }
  sqrt(var(x) / length(x))
}
```

Which generated a working example!

``` r
calc_se(1:10)
#> [1] 0.9574

# note the function also properly indicates the need for a numeric input
calc_se(letters)
#> Error in calc_se(letters) : x must be numeric
```

For smaller problems or to provide context or guidance within a function or code block, comments can be used to provide a basic prompt for Copilot.

Comments in code such as:

``` r
# create a function that takes a number and returns the square
```

Might be used by Copilot to suggest a function that takes a number and returns the square:

``` r
# create a function that takes a number and returns the square
square_number <- function(x) {
  x^2
}
```

Lastly, comments can be used inline within code to provide additional context or guidance to Copilot. For example:

``` r
library(dplyr)

mtcars |> 
   group_by(cyl) |> 
   summarize(
      mean = mean(mpg),
      # add the standard error for mpg
   )
```

Might return:

``` r
mtcars |> 
   group_by(cyl) |> 
   summarize(
      mean = mean(mpg),
      # add the standard error for mpg
      se = sd(mpg) / sqrt(n())
   )
```

However, the comment may not be needed if a descriptive column name is used instead.

![](images/copilot_se-inline.png){fig-alt="Copilot inline completion"}

### Language support {#language-support}

While R is the primary expected language for RStudio users, Copilot also supports other languages. Copilot will use the file extension or other context to determine the language of the active document. In multi-language Quarto documents for example, Copilot could use the active language within a code chunk or the body of the document to generate suggestions.

Languages supported by Copilot include:

- C
- C++
- CSS
- HTML
- Java
- JavaScript
- LaTeX
- Markdown
- Python
- Quarto
- R
- R Markdown
- SQL
- TypeScript
- XML
- YAML

Comments in other languages can be used to generate suggestions in the active document. For example, a HTML comment (`<!-- COMMENT BODY -->`) might be used in the body of a `.qmd` file:

``` html
<!-- Using HTML and JavaScript return a clickable counter that is displayed -->
<!-- there should also be a button to reset the counter to zero -->
```

Where the above HTML comment might generate the below HTML and JavaScript:

``` html
<!-- Using HTML and JavaScript return a clickable counter that is displayed -->
<!-- there should also be a button to reset the counter to zero -->
<html>
<body>
<script>
var count = 0;
function countClicks() {
  count += 1;
  document.getElementById("clicks").innerHTML = count;
}
function resetClicks() {
  count = 0;
  document.getElementById("clicks").innerHTML = count;
}
</script>
<p>Click the button to count clicks.</p>
<button type="button" onclick="countClicks()">Click Me!</button>
<button type="button" onclick="resetClicks()">Reset</button>
<p>Clicks: <a id="clicks">0</a></p>
</body>
</html>
```

![](images/copilot_javascript.png){fig-alt="Copilot HTML and JavaScript completions"}

## Support and Terms of Service

Posit does not provide support or assistance for any code written or generated in RStudio, with or without Copilot. Posit does not support the Copilot output, or test the logic used by Copilot to generate code from prompts.

GitHub Copilot is a proprietary tool from GitHub. If you want to use GitHub Copilot, you need a [subscription for GitHub Copilot](https://docs.github.com/en/billing/managing-billing-for-github-copilot/about-billing-for-github-copilot) in your personal GitHub account or to be assigned a seat by an organization with a subscription for GitHub Copilot for Business. Individual snippets of source code or the contents of entire files may be sent to GitHub's servers to generate suggestions, for more information about how this information is handled, please see the [Copilot Privacy Statement](https://docs.github.com/en/site-policy/privacy-policies/github-general-privacy-statement#personal-data-we-collect).

Students and faculty can use GitHub Copilot for free as part of the GitHub Education program. For more information, see the [GitHub Education page](https://education.github.com/).

By using GitHub Copilot, you acknowledge that your use of GitHub Copilot is governed by their terms of service and you agree to abide by their [terms of service](https://docs.github.com/en/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot). GitHub Copilot is considered "Third Party Materials" as defined in the [RStudio End User License Agreement](https://posit.co/about/eula/) and Posit assumes no liability or other obligations with respect thereto and, without limiting the foregoing, is not liable for any loss or damage resulting from the use or access thereof.
