- 3 Minutes Wednesdays
- Posts
- 3MW (Using the Golem Framework for R-Shiny)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/32b91729-9237-4c49-b7a6-16c7d90fa4a6/image.png?t=1719418334)
In the following popup window, you can select the path for where your app should be stored and give it a name.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/b5e8edf1-7cb5-41c3-a1d9-decaebea163b/image.png?t=1719418408)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/9f5ef97b-3e18-4e4d-8975-1c658496a191/image.png?t=1719418451)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/8f9f128b-5b84-46cc-a918-4efe4c2f357c/image.png?t=1719418535)
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:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/40c450b6-9c78-40c2-86d5-d5cf46f14ed0/image.png?t=1719418761)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/be0ed1a5-48e7-4d8f-8107-2ece83110743/image.png?t=1719418805)
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).
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/22416b54-b28b-4932-92eb-20f21be7633d/image.png?t=1719418913)
Or you could add functions and utility functions:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/34da1476-cde8-496d-8592-8597331ca769/image.png?t=1719418932)
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
, andapp_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.)
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/36b6934e-c43c-43c0-96b5-6e898d471df3/image.png?t=1719419110)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/accd15d7-ba5f-459f-957d-d3ea0a15a190/image.png?t=1719419222)
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
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/39bc8b07-984b-43c4-a3c8-d10d3f3b8c81/image.png?t=1719419578)
And in the app_server()
function, we add the logic that makes sure the output actually generates some output.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/0adf8714-0e34-489c-8852-9c18011143da/image.png?t=1719419592)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/4f598257-a9a5-46bb-ad7e-ede8e95b50e8/image.png?t=1719420147)
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.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/57a029ea-db3c-40a5-ad0f-f5ddf3a620d3/image.png?t=1719419635)
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:
Reply