Resources

David Smith | Zero-setup R workshops with GitHub Codespaces | RStudio (2022)

video
Oct 24, 2022
5:13

image: thumbnail.jpg

Transcript#

This transcript was generated automatically and may contain errors.

So thank you everyone. My name is David Smith, and I'm a developer advocate at Microsoft. And if you've ever given a tutorial using R or just tried to share R code with other people and gotten these kind of responses, then this talk is for you.

Now you might get these kind of reactions because somebody didn't read the setup instructions or installed the wrong version of R and so your code doesn't work. Or they're using a different operating system than you expect, or just using an old laptop that can't cope with the data and the code that you provided. But you can resolve all these issues by running in something called GitHub Codespaces. So let's see how this works.

Setting up a GitHub Codespace

I've got here a repository which you can find on GitHub. It has everything you need to recreate what I'm showing you right here. But one of the things that it does include is a .dev container folder. And that is the magic that allows you to click on that code button directly in GitHub and then run the code within Codespaces.

Now this is running in real time. It takes about 10 or 20 seconds. What GitHub is doing right here is setting up a Codespace, kind of like a little virtual machine that has the version of R that I specified, installs all the R package versions that I specified. It also installs Python, my data, my code, everything is all right there ready for anybody who clicks on the button to get exactly in the environment that I intended for somebody to run my code within.

So once that Codespace launches in just a couple of seconds, what we'll see is a web interface that looks much like Visual Studio Code. And in fact, this is Visual Studio Code running directly within the browser. And I can do all the usual things like browsing within my file system that comes from my repository.

This is actually a tutorial that comes from a collection provided by Microsoft, including a Jupyter notebook, which is to teach somebody how to do data exploration using R. So this notebook has popped up directly within Visual Studio Code. And I can interact with it as you would any other notebook, like I can run this cell right here. And the surprising thing is it runs without any errors, without any setup, because all those packages are already pre-installed within the Codespace.

And the surprising thing is it runs without any errors, without any setup, because all those packages are already pre-installed within the Codespace.

So your participants can then interact with the notebook, run the code. Here's a tidyverse function, which is reading in a data file and showing in the first few lines. It's a very, very natural experience, regardless what kind of laptop that they brought to the workshop with them.

Running RStudio within Codespaces

But what if you wanted to run your workshop within RStudio instead of Visual Studio Code? That's easy, too. Because we have RStudio Server installed within this Codespace, just need to go to the Ports tab in Visual Studio Code, and that launches RStudio Server directly within my browser. So this is the same as RStudio on the desktop, just running on a browser.

And I can use that to open files. And again, I'm going to open a file from my repository. They happen to live in the workspaces folder when it's running on the server. And I browse through that. And this time, let's grab an R Markdown document instead, so we can see how that works within Codespaces.

There we have our Markdown document. And we can just use the usual knit function within RStudio to knit it to HTML. And we can see our output really quickly, because it's running on a machine with lots of RAM, lots of CPU. So these computations happen nice and fast.

How DevContainers work

This is all thanks to the magic of DevContainers, which is a standardized way of setting up a container for interactive use like this. You saw that DevContainer folder in my repository. That's where I specified the R versions, the packages, and everything else that I needed to give the same experience for every user that interacts with my repo.

DevContainers, by the way, are 100% open source technology. There is a command line interface, so you can integrate those DevContainers into any application, not just GitHub. And you can find lots of information about them at containers.dev.

But just to give you a little bit of a flavor of how that works, if you're familiar with containers, you'll know about Dockerfiles, where you can specify the software that gets installed into that container. In this particular case, I'm grabbing a prebuilt container from the Rocker project, an awesome project providing containers for R. I chose the version of R. In this case, it's grabbing the latest 4.x version, so I got the very newest 4.2.1 for that particular example. And I'm installing a bunch of packages into that container as well.

There's another file that lets you configure Visual Studio code, set some options if people are using that editor. And you can also specify commands to run at the beginning of the container, like starting the RStudio server, which is how we got that to work.

Summary

So in summary, if you want to do a tutorial or share some code, just create a GitHub repo as you always do. Put all your scripts, data, notebooks, and instructions into there, but then set it up as a dev container, so that all the software to run your code is fixed and ready for anybody to use, simply by clicking on that Run in GitHub Codespaces button directly in GitHub.

Now, GitHub Codespaces is only available right now within GitHub Enterprise and GitHub Education. If your participants don't have access to Codespaces today, they can run it locally using Visual Studio code. And I have another talk which shows you how that works, which you can't quite see. But you can find that link directly on my GitHub repository that you see on your screen right there, with everything you need to make all this work for you. Thank you.