- 3 Minutes Wednesdays
- Posts
- 3MW (Automate Anything With R & GitHub Actions)
3MW (Automate Anything With R & GitHub Actions)
Guten Tag!
Many greetings from Munich, Germany. You can automate so many calculations with GitHub Actions. Unfortunately, there’s one huge obstacle: GitHub Actions can feel really intimidating at first.
So today I’m going to break down how we can automate a simple calculation with GitHub Actions. And even though this project is simple, it will be instructive to see the components needed to get GitHub Actions to work.
3 more video lessons
The text cleaning part of my video course is making a lot of progress. This week, I’ve uploaded three more videos about
combining texts with
paste0()
&str_c()
,using the powers of
glue()
andepoxy()
for easier text handling andreplacing unwanted characters with
str_replace()
.
You can find all of the information on the video course at
And now let’s get back to automating stuff.
Setting Up the Project
Let’s start by creating a new renv-controlled and Git-controlled RStudio project.
In that project, we can create a Quarto file called readme.qmd
and fill it so that it tells us when this document was rendered.
If you try to render this (and really have used {renv}
), then you’ll likely get an error. This means that you need to install the packages that Quarto and R need to render this.
Now if you’ve done that, then your document should render. But don’t get too excited. The output will look really boring like this:
Next, let us render this to a github-flavored format. For that, we can run the following command in the RStudio terminal:
That way, we have a README.md
which will be displayed in our GitHub repo. So now if you commit and push all of these changes to GitHub it should look something like this.
Set up an automation
Now, let’s automate this whole process so that it runs regularly using GitHub Actions. Here’s how to get started.
Inside our repository, create a directory called
.github
.Inside that directory, create a directory called
workflows
.In the
workflows
directory, create amain.yaml
file.
This YAML file is a specific file format you might have already seen from working with Quarto projects. The magic behind GitHub Actions is that we just have to fill this YAML file with steps that need to be run one after the other. Let’s do that next.
Give your action a name
We start by naming our action using the name
property.
Define a trigger
Next, we want to specify when the action should be executed. There are a whole bunch of different triggers we could use but for now we’ll just say that the action shall start when something new is pushed to the master
branch.
Specify the jobs that need to run
Inside of a single action, we can run multiple jobs. Here that won’t be necessary but still we have to define one job. Let’s call it render
:
Specify the type of machine the job runs on
Next, we must define on what kind of machine GitHub is supposed to run all of the steps of our job. A typical thing is to tell the Action to run on a machine that uses the latest version of Ubuntu and has permissions to write to the repository.
Define a first step
Now it’s time to fill the render
job with steps
. The first step is usually to checkout the repository. That’s pretty easy. You just
name the step meaningfully and then
use the pre-defined
checkout
action.
Install Quarto
Next, all of our jobs have to make sure that Quarto is actually installed on the machine that runs the job. That’s why we use the nice setup
action from the quarto-dev/quarto-actions
repository. If you’re curious, you could check out all of the steps that this will run by looking at the corresponding yaml file in the repo.
Here, we told the setup
action to install the latest version of Quarto by additionally using the with
property.
Install R dependencies
Just like we had to install the correct R packages on our local machine, we have to do the same on the GitHub Actions machine. We can do that by just installing {renv}
and running the restore()
command.
Here, I’ve used the run
property to run commands as if I’d run them manually on a command line. Also notice that I’ve used the Posit Package Manager instead of CRAN to speed up the installation process (which has to repeated every time.)
Render README.md
With the same logic we can now render the README.md
file:
Commit and push changes
Finally, you have to make the changes stick. For that, you have to setup git so that it uses the github-actions bot. And one particular thing you have to watch out for is that the bot has permissions to push to the repo.
Thus, it needs to be able to use the repo’s pre-defined GITHUB_TOKEN
environment variable. This can be grabbed from the repos secrets
store via the code ${{ secrets.GITHUB_TOKEN }}
:
Push changes
After setting up all these components, you can now commit and push the changes of the YAML file to your repository. If you didn’t do anything wrong, you should see that the steps run through and your repo should be updated now:
Don’t get discouraged, though, if you’re not able to set up the actions workflow in one go. It’s normal to run into issues for which you need to do a little bit of debugging work. Even for this simple workflow, it took me a couple of runs to work out all the kinks.
I hope this gives you a good idea on how to get started with GitHub Actions. As always, if you have any questions, or just want to reach out, feel free to contact me by replying to this mail or finding me on Linkedin.
See you next week,
Albert 👋
Enjoyed this newsletter? Here are other ways I can help you:
Reply