Using Github Copilot to work
I just installed VS Code and subscribed to Github Copilot. If you aren’t familiar, Github Copilot is basically ChatGPT, but directly in-line when coding or writing (e.g. right in the editor). There is no easier way to get excited about the advent of large language models than setting this up with the ability to write in:
Latex
R
Stata
Python
And others!
Some of this is also about moving to a really good IDE (integrated development environment) – VS Code is quite powerful and I’ve really enjoyed my experience so far.
Let me first show you an example. I can tell, roughly, Github Copilot what kind of code I’d like to construct using words in a comment. It will then write the code directly below. As you will see, however, this isn’t without issues — it will get a variable name wrong, and have no idea! Each time when you see “suggested” lines of code or writing — that’s Copilot doing it on the fly!
What follows are some notes on how to set up VS Code and Github Copilot to work with Latex, R, and Stata.
Installing VSCode
Download and install VSCode from here.
Setting up VSCode
There are a lot of keybindings, etc., that are worth learning in VS Code, but I’m only a beginner, and not the right person to teach them. Personally, I installed an extension to mimic the Emacs bindings, which made the transition easier. Your mileage may vary, but this was a good resource on getting started with VS Code.
I will instead highlight the basic features that I thought were mind-blowing.
Install the Latex Workshop Extension
Click on the extensions image on the left (Shift-Apple-X on a Mac) and search for Latex Workshop
.
Getting the Latex extension to work
I ran into getting the compilation to work here. You may not! Let me show you how this should look, first:
On the top of the editor, right in the middle, I’ve put a red box around the triangle where you can compile the Latex file. The button next to it should open up the viewer on the right, which will show your compiled PDF file.
In my case, I ran into a problem where VS Code couldn’t find my latex compiler. (specifically, the latexmk program). To fix this issue, I used the solution on this page, which involved changing some of my profile defaults (to point at the right compiler). So if you’re having issues, take a look here!
Install the Stata Extension(s)
This involves two extensions.
First, install the Stata Enhanced
extension.
Then, install the stataRun
extension.
Only modifications that I had to do to get this working was to go into the stataRun extension and change the program from StataSE to StataMP (you can edit your preferences for an extension by going to the extension and clicking the little gear next to where the install/uninstall button is):
What do this let you do? Similar to the Stata editor, it lets you send code to Stata directly using keystrokes:
shift+cmd+a: Run All Lines → This will run the entire file
shift+cmd+s: Run Selection → To run a given selection. If no selection is made it default to Run All
shift+cmd+c: Run Current Line → This will run the current line code
shift+cmd+d: Run Downward Lines → from current line to end
shift+cmd+f: Run Frontward Lines → from first line to current line
N.B. Stata was really not very good with Copilot. This is consistent with people’s experience that LLMs are just not very good at coding in Stata. Owen Ozier wrote about this this year!
Install the R Extension
The R extension is, aptly, just called R. For me, it worked out of the box.
shift+Enter: Run Selection → To run a given selection.
shift+cmd+S: Source file → Run the whole file
Update: It is very useful to install the httpgd package in R for using R inside VS Code. You can do this with install.packages(“httpgd”).
Update 2: I found these set of additional R extensions quite handy as well. They seem to make the main terminal that is in R the same terminal that is active on your computer, which I like.
Install the Github Copilot Extension
I personally am paying for the Copilot using my research budget. It is ten dollars a month, and seems very worth it.
However, my understanding is that if you are a student or teacher, you can get a free license! (I just didn’t realize this when I initially signed up) See here for setting this up as a student. If you are a faculty member, you should be able to get one for free as well by signing up here. However, it sounds like it might take a few days to work properly:
This will seem to do nothing to your editor, but will add this little icon on the bottom of your editor.
Some examples
R
As discussed above, one easy task is to define a scripted program that does a particular task, such as downloading the population for states and making a plot. To try this out, write the following in a comment at the top of a blank R script:
##This program downloads population counts for all states in the US in 2018.
##It then makes a bar chart for each state, with states on the y-axis, and population on the x-axis, from largest to smallest state.
##It uses the tidycensus package to download the data, and ggplot2 to make the bar charts.
This approach requires the tidycensus
package installed (and ggplot2).
After you enter this comment, hit return
twice, and wait for Copilot to propose a line of code. Then hit tab
to accept the proposals. As you move across lines, it will write out the code.
One interesting fact — you will have to hit return more than once, because it wants the proper line breaks!
Latex
This is where things got even more amazing. Consider the writing from a recent paper that we just got published. I can open a new latex document, and enter our opening paragraph, and then the first two sentences. Github Copilot proposes the rest of the paragraph. It’s not even a totally unreasonable proposal! [It’s not correct, in our setting, but it’s still a remarkable attempt!]
I’ve had other settings where it will propose explanations for regression tables that are not even half-bad. It’s a pretty killer tool to have for someone who struggles to get words down on the page (like me) — the words may not be great, but then you can edit them and fix it up. Pretty remarkable.
What’s next?
This works in a huge number of languages, including Python and SQL, and it is very useful at simplifying tasks in those languages. I think one under appreciated aspect of writing code this way is it does a great job documenting things at the same time; I’m hoping this will improve the structure of my code.
I’m very curious how you’ve used it, or any issues you’ve run into. Let me know in the comments below or on twitter!
Copilot is pretty incredible. Still not enough to pull me away from rstudio for R work, but for everything else it's got me solidly in vscode so I can use copilot.
You may want to update with a suggestion to sign up for Copilot chat: https://twitter.com/jlperla/status/1669215796592275459