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.

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:

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:

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.

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:

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:

Define a new function

We automatically get an R script that is filled like this:

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.

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!

Finally, all we have to do is to refactor our module code to use the function inside the render call.

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:

Naturally, after having refactored our module we should run our test app. Let’s see what we get when we do that:

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.

The moment we do that, we can run our small Shiny app. And this time we don’t get an error.

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

or to participate.