- 3 Minutes Wednesdays
- Posts
- 3MW (Using Shiny Modules With Golem)
3MW (Using Shiny Modules With Golem)
Guten Tag!
Many greetings from Munich, Germany. This week, we are going to dive deeper into the Golem framework. Specifically, I’m going to show you the workflow of creating and adding modules inside your Golem app. But first, time for my regular announcements.
How to deal with blank line charts
Ever wanted to create a line chart with {ggplot2} only to find that the chart is empty? This happens when you try to use a non-numeric variable as x- or y-aesthetic and don't specify the group aesthetic.
In those cases, ggplot even tells you that you probably need to set the group aesthetic. And in my newest YT video, I show you exactly how that works:
As always, you can find all the code that I show you on GitHub.
Create a module
As we’ve learned a couple of weeks ago, it’s good practice to structure your app into Shiny modules. The easiest way to do that is to manually navigate to our R/dev.R
file of the Golem app we’ve created last week. Alternatively, you could open this also very easily with the command palette:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/a8241f31-0493-4169-8e34-a5ce8b68a695/image.png?t=1719901856)
There, we can use the golem::add_module()
function to create a module with the name of our choosing.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/0c3fc2c0-5ea0-4643-9a38-987c6009f1ef/image.png?t=1719901868)
Once we execute this, Golem will be kind enough to create two files for us:
A file called
mod_<module_name>.R
inside theR
directory.A test file inside the
tests
directory of our package.
For now, you can ignore the test thing. This is something about unit tests, which we’ll talk about some other time. I want to focus on the module file here.
Module Structure
Inside the module file, there are more or less two things. First, there’s the module UI function:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f062e894-d399-444b-b7e0-8a89d7942d33/image.png?t=1719901884)
Second, there’s the corresponding server function:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/a4cc4050-f798-46f2-8fd0-d464f6110f4e/image.png?t=1719901894)
Both functions are decorated with a whole bunch of extra notation which allows for describing what this function does. This is how packages and documentations for functions are built. For now, you can just ignore this. All you have to know is that you can use your knowledge about modules inside the module function that we’ve just created.
And a little convenient tidbit: At the bottom of that file, you see that you’re already provided with code that you can copy into app_ui.R
and app_server.R
to use your new module.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/a605db32-a637-4380-8c68-de8b5d5a054c/image.png?t=1719901915)
Testing Modules
What you can do now is to
fill the UI with the things we’ve put into
app_ui.R
last week andwrap the ID into the
ns()
function.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/5c059bd4-63e0-4c40-ab4c-07c63a232c66/image.png?t=1719901930)
And into the module server we put in the things that we used in app_server.R
last week.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/39e6489a-d042-493c-83af-7ffd61bfc08a/image.png?t=1719901945)
Now, I don’t want you to put the module calls into the full app just yet. First, I want to demonstrate how you can use the Golem structure to use the philosophy of small modules. You know, the thing we’ve covered a couple of newsletters ago.
Inside the script that already contains the module code, you can add code that creates a Shiny app. If you throw in a Shiny app in there, you can just stick in your module and execute this whole script. That way, you can just test your one module.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/a1f6ba82-aceb-41e0-8ff9-1f599b92f150/image.png?t=1719902016)
The cool thing about this is that if you later call golem::run_app()
, this extra tiny app that tests only the module is ignored. Instead, Golem will just create the app that you specified in the app_ui.R
and app_server.R
scripts.
Handling Packages in a Golem App
Before I let you run off and play with Golem on your own time, let me highlight one thing about packages inside your Golem app. If you want to use functions from, say, the Tidyverse, you will have to load the package somehow. There are more or less three ways to do that:
You could always use the full name of a function. For example, if you want to use
mutate()
from{dplyr}
in your code, you’ll have to write outdplyr::mutate()
.Inside the function where you want to use, say,
mutate()
, you could addmutate()
to the#' @importFrom
call that you already see in the template that Golem gave you. You know, that one line from your module UI skeleton:
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/363eb058-d758-4be0-af87-42464d272f4c/image.png?t=1719902045)
In our mutate()
example, you could do the exact same thing by adding another @importFrom
statement in front of that function and specifying @importFrom dplyr mutate
.
Or if you want to load all of dplyr, you could just throw in an
@import dplyr
statement.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/4e1d0b91-bb85-4296-8bfe-babd0270f1b5/image.png?t=1719902070)
This is one of the occasions where using Golem comes with a bit of overhead and additional struggle. Admittedly, this type of coding might feel weird to you at first, but you can get used to this quite quickly. In the long run, adopting this style helps to create a robust Shiny app.
With that, you should have everything to get started creating modules for your Golem app. Next week, I’m going to talk a little bit more about workflow with Golem. 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