- 3 Minutes Wednesdays
- Posts
- 3MW (Using Helper Functions With Golem)
3MW (Using Helper Functions With Golem)
Guten Tag!
Many greetings from Munich, Germany. In today’s newsletter I want to talk more about the Golem workflow. In particular, I want to show you how to not get stuck with the particularities of package development. But before I do that, time for my regular announcements.
Course Progress
I’m super excited to tell you that Part 2 of my data cleaning master class is making progress. All the materials are assembled and I’ve already started recording the new videos. I’ll release the first bunch of them soon.
Part 2 of the course will focus on loading, combining and rearranging all sorts of data sets. In particular, I will show you how to turn messy Excel files into a neat tidy data set. For example, the course will show you how to turn annoying pivot tables into something that you can actually use in R.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/dcff4a4c-31a5-41b7-b934-048cf9b434a7/image.png?t=1721155688)
Pivot tables are nice to work with in Excel but a pain to use once loaded into R.
If you want to get access to the course before the price increases with the launch of Part 2, you can unlock the full course via its landing page.
As always, you can find all of the code on GitHub.
Create helper function
Even if you modularize your golem app (which you should), you don’t want to fill your modules with too much code. In a lot of cases, it makes sense to wrap a long & complicated calculation into a function. That way, you can avoid filling your module with a whole bunch of code like this:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/0a5bf380-4213-485f-8f19-c9a853e93a16/image.png?t=1721155219)
Instead, you can just put that code logic into a separate function with a meaningful name in a separate R script. In effect, your module code gets reduced to something like this:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/4e8e3cd3-9725-4d3b-a42f-80c722166e7c/image.png?t=1721155228)
This can be tedious because you will need to have the discipline to create a new R script in the right place of your Golem package. That’s where Golem can help you. All you have to do is to call the add_fct()
helper from the 02_dev.R
file.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/8a5220fd-e31f-40bd-8f89-372ea4738d7c/image.png?t=1721155239)
This will create an R script tailored for the generate_nice_chart()
function as well as a test file which you can use to do unit testing (if you know how that works).
Example
Let’s make all of this more specific using the Golem app we’ve built over the last weeks. You know, the one that shows you your favorite fruit. In our mod_fruit_picker_server()
function, we have the following code:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/d361dcd1-f8a7-4d29-9154-98239dd58ffc/image.png?t=1721155251)
We could now wrap the glue()
function call into a new function generate_fruit_text()
. I know, not particularly useful as it’s really not a lot of code. But the ideas will be the same whether you refactor 3 lines of code or 300 lines of code. Trust me, bro!
So, let’s call our helper function:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/62839dbc-1aab-4a38-917c-0206a20213ed/image.png?t=1721155260)
Define a new function
We automatically get an R script that is filled like this:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/318bc701-41f8-418f-be8e-66cc04530e35/image.png?t=1721155271)
And right below this Roxygen code (that’s the thing that writes R documentation for you), we can define a new function just like we normally would.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/30200274-c730-47df-82ba-5635ff47d38e/image.png?t=1721155286)
We could even tweak the Roxygen documentation a bit to make our function well-documented. That’s super important. Especially when the function is as complicated as this one. Trust me, bro!
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/b1e2e74e-e6a6-4639-ba59-7c381c2b5aed/image.png?t=1721155297)
Finally, all we have to do is to refactor our module code to use the function inside the render call.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f0b123e6-21be-44c3-8ecd-1e05604b0b6e/image.png?t=1721155308)
Test Our Module
Remember how we created a small Shiny app for every module that we have? That’s part of our “one module = one app” approach. In this particular case, our fruit picker app looked like this:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/5f9285ef-874c-4f7a-8ec0-cc8ee04d5642/image.png?t=1721155319)
Naturally, after having refactored our module we should run our test app. Let’s see what we get when we do that:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/66c71f0a-9c97-460d-aee7-b854aa174282/image.png?t=1721155380)
Oh no! There is an error in the app. It appears as if our Shiny app doesn’t know our new function generate_fruit_text()
. That’s not great.
Load package function
The reason why this happens is because we have to make sure that our R session knows all the function that we want to use. Otherwise, it’s like calling mutate()
without calling library(dplyr)
or library(tidyverse)
first.
And the same thing is true here. We have to make sure that our own package is fully loaded. And we can do that with help from the {devtools}
package.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/c56aaca8-a974-41ae-b19e-a540feac4dcd/image.png?t=1721155333)
The moment we do that, we can run our small Shiny app. And this time we don’t get an error.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/ad1b5454-7911-496e-a050-3ab2804a2763/image.png?t=1721155399)
And as you can imagine, we have to load our own package quite often. Especially if we change a function (otherwise the changes don’t take effect.) So that’s why RStudio has the shortcut crtl + shift + L
for that.
With that, we have solved a major headache that was confusing me when I got started with Golem. Now, you should be ready to go on your own Shiny/Golem adventures. If you have any questions, don’t hesitate to reply to this mail or contact me on LinkedIn.
See you next week,
Albert 👋
Enjoyed this newsletter? Here are other ways I can help you:
Reply