GitHub Copilot
GitHub 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.
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.
- 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
With RStudio Server and RStudio 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:
- Navigate to Tools > Global Options > Copilot.
- Check the box to “Enable GitHub Copilot”.
- Download and install the Copilot Agent components.
- Click the “Sign In” button.
- In the “GitHub Copilot: Sign in” dialog, copy the Verification Code.
- Navigate to or click on the link to https://github.com/login/device, paste the Verification Code and click “Continue”.
- GitHub will request the necessary permissions for GitHub Copilot. To approve these permissions, click “Authorize GitHub Copilot Plugin”.
- After the permissions have been approved, your RStudio IDE will indicate the currently signed in user.
- 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 > Copilot and uncheck “Enable GitHub Copilot”, or
- Use the Command Palette Cmd+Shift+P on Mac or Ctrl+Shift+P on Windows/Linux, search for “Enable GitHub Copilot” and then uncheck “Enable GitHub Copilot”.
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 RStudio 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 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.
- A simple but specific comment providing additional context to Copilot.
- Copilot’s code suggestion, shown in light grey “ghost text”.
- 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 R, Python, or SQL files 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:
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 and 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 Tab. If it is not, you can ignore the suggestion and continue typing or force the normal autocomplete to show by pressing Ctrl+Space. 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 Ctrl+Shift+P.
You can also disable the “Show code completions: Automatically” setting in the Tools > Global Options > Code > Completion section.
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 Tab, while the Enter/Return 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.
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.
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:
# q: What is the definition of standard error?
Might generate an answer like:
# q: What is the definition of standard error?
# a: The standard error is the standard deviation of the sampling distribution of a statistic.
Copilot is must 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 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.
Language support
While R is the primary expected language for RStudio users, Copilot also supports other languages such as SQL, Python, HTML, CSS, JavaScript, and many 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.
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:
<!-- 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:
<!-- 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() {
+= 1;
count document.getElementById("clicks").innerHTML = count;
}function resetClicks() {
= 0;
count 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>
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 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.
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. GitHub Copilot is considered “Third Party Materials” as defined in the RStudio End User License Agreement 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.
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.
A real example might be:
Which might generate something like the below:
Which generated a working example!
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:
Might be used by Copilot to suggest a function that takes a number and returns the square:
Lastly, comments can be used inline within code to provide additional context or guidance to Copilot. For example:
Might return:
However, the comment may not be needed if a descriptive column name is used instead.