3MW (Using the Golem Framework for R-Shiny)

Guten Tag!

Many greetings from Munich, Germany. In today’s newsletter, we’ll discuss the Golem framework. In case you’ve never heard of it, golem is an impressive package that gives you systematic way to create enterprise-grade Shiny apps. As such, it comes with a whole lot of opinionated choices on how to set up a Shiny app.

And because this is such an opinionated and large framework, it’s also a bit hard to get started with it. So this is why we’re going to take the next couple of newsletters to go through some of the Golem things one by one.

Today, we’re going to initialize a Golem app so that we understand how the code of such an app is spread out across multiple scripts and files. But first, time for regular announcements.

100 Sales 🥳 

This week I get to share that I’ve sold my 100th video course. Hoooray! 🎉 A big thank you to everyone who has bought one (or even both 🤯 ) of my data visualization or data cleaning classes. I’m super happy that so many of you support and enjoy my work. Glad you have you all onboard 🤗 

As always, you can find all the code that you see here on GitHub.

Installing and Creating a Golem App

The first thing you can do to get started with golem is to install the Golem package (obviously.) Once that is done, the easiest way to create a new Golem app is to start a new RStudio project and select a Golem app from the list of possible projects.

In the following popup window, you can select the path for where your app should be stored and give it a name.

And that’s a first step where you can mess up 🙈 One important thing to keep in mind is that Golem is fundamentally an R package. That’s both cool and bad. The thing is: Packages are highly regulated, especially if you want to publish them on CRAN. That’s why your file and package structure needs to follow certain conventions.

One of these regulations is that the package name can only contain certain characters. And if you don’t follow these naming conventions, you will get a warning message that prevents you from creating a Golem app.

Exploring the Golem App Structure

Provided you chose a name that worked, you will be led to a new RStudio session, and you will see the start file of the Golem app.

That script is meant only for executing the helper functions inside of it once. Once the setup is done, you never have to touch any of those functions again. Let’s try that with one of the first functions. Thus, let us

  • fill in arguments of golem::fill_desc(),

  • execute that function, and then

  • move on to the next function.

Here’s, how the filled out function could look:

Next, you can execute more of these helper functions and or skip all the way to the end. There, a helper function will lead you to the next dev.R file.

Using the dev.R file

The dev.R file is another helper script that gives you a variety of helper functions for your development process. As such, this script isn’t designed to be walked through line by line. Instead, it just gives you a couple of helpful functions in case you want to add something to your Golem app.

If you want to add a new module, you can call the golem::add_module() function and give it a name. In there, you can also specify whether you want to have unit tests for that (if you know what unit tests are).

Or you could add functions and utility functions:

The point of all of these functions is that they create files at the right place inside of your package folder. That’s super important because a Golem app is an R package. And with that comes the restriction that all your R scripts need to go into the R directory. Let’s have a look at that directory next.

Where are the UI and server scripts?

If you look into the R directory, you can find four files in there:

  • app_ui.R

  • app_server.R,

  • run_app.R, and

  • app_config.R.

These files contain the bare minimum code of a Golem app. In the app_ui.R file, you will find a function called app_ui(), and inside it, you will find a fluidPage() call within a tagList(). (You can ignore all the lines that start with #’ for now.)

Inside that tagList(), you can insert all the things you want to place into your UI. In a fresh Golem app, this list is just pre-filled with a headline that contains the name of your Golem app. And in the app_server.R file, you will find an app_server() function where you can place the things you want to put into your app server.

For example, we could fill the UI with a drop-down menu and text output that just prints whatever is selected in the drop-down menu. All we have to do is to put a selectInput() and textOutput() in the app_ui.R file

And in the app_server() function, we add the logic that makes sure the output actually generates some output.

Running the Golem App

Now, you might wonder how to execute and start the app. The way to do that is to use the run_app() function from Golem, which will take care of everything. You could type golem::run_app() into your console, and this will start your app.

Alternatively, what I prefer to do is to hit Ctrl+Shift+P to open the RStudio command palette and select “Run: run_dev.R” from there.

Nice! We officially learned the basics of Golem. Next week, I’m going to explain more on the workflow of how to build out your app with this. And 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:


or to participate.