3MW (Creating apps with R-Shiny)

Guten Tag!

Many greetings from Munich, Germany. Today I’m going to show you how to get started with R-Shiny. This is a nice way to

  • explore your data more interactively, or

  • create data-driven, dynamic dashboards, or

  • build full-fledged tools in the form of web apps.

But before we get started, time for my usual announcements:

Creating beautiful tables in R & Python

The {gt} package is a fantastic package to create nice tables from within R. And the really cool thing is that there’s a Python version of that too. It’s called great_tables. In my newest YT video, I show you how the R version works and in my next video I’ll show you how you can get the exact same result with Python.

As always, all code that you see here can be found on Github.

Anatomy of a Shiny App

Alright, let’s build a Shiny app. The first thing that you need to know it that any Shiny app consists out of three parts:

  1. User Interface (UI): What your user sees in the app

  2. Server function: The brains behind the operation (more on that later)

  3. ShinyApp() function call that ties UI & server together

Take a look at how a skeleton app looks and then we can go through the first two parts separately.

Start with a blank page

Let’s start building out with our UI. The easiest way to create a UI is to use one of the page_*() functions from {bslib}. The default Shiny app uses fluidPage() but a more modern version would be to use page_fluid() instead.

Add input elements

To fill your UI with content, you can fill the user interface with inputs. For example, you could fill your page with a slider input and a drop-down menu. The two functions that create such UI elements are sliderInput and selectInput.

Since all of these things just generate a bunch of HTML & CSS code, you can call your ui variable and that way your UI will show up in the Viewer pane of your RStudio window.

As you’ve seen in the code, every input function needs three things:

  • First an ID,

  • then a label of that UI element,

  • followed by input-specific arguments

Obviously, you don’t only want to make inputs, you also want to see how things change. That’s where outputs come in.

Add outputs

You can add all kinds of different outputs to your UI with dedicated output functions. For example, let’s add a text output using outputText() function. There, you just have to specify an ID.

If you look at your app now, you will see that there is no output yet. Everything still looks the same:

That’s where the server function comes in.

Creating a server function

As I’ve said earlier, the server is the brains behind the Shiny operation. This function makes sure that things actually happen in the app. In the Shiny framework, the server is basically nothing but a function with the arguments input, output, and session. As we’ve seen earlier, the smallest server you can think of is just an empty function.

Whenever something in the user interface is clicked, the server, or more specifically, this function that you call server, should detect what’s going on and react appropriately. But to do that, the server needs to have instructions how it is is supposed to render/display outputs.

Rendering outputs

To render an output, you have to assign a render function inside of your server to the output ID of the thing that you want to render. Sounds overly complicated but is actually quite simple. Let’s have a look at an example.

Eariler, we gave the textOutput() the ID my_generated_text. Hence, we have to assign render instructions to output$my_generated_text. And to assign these instructions, there’s the renderText() function.

Notice the naming convention. Here, we used renderText() to render the textOutput(). Similarly, for all kinds of different outputs, you have different render function. For example, for plotOutput(), there’s renderPlot().

All of these functions work the same. Basically, you throw a code chunk into these render functions via {} and the last thing that is calculated in these code chunks is what’s supposed to appear as the output. If you think about it, it’s just like a regular function() definition in R.

Here, we can use a simple paste() call to stick together what is selected from the drop-down input and from the slider input. And the cool thing is that things that are currently displayed in the UI can be accessed quite easily via input$<input_id>.

To see that in action, you just have to run shinyApp() with your UI and server variables.

So with that, we’ve learned how to create a small Shiny app. As always, hope you enjoyed this newsletter. Next week, we’ll talk about dynamic/reactive events in Shiny. 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:

Join the conversation

or to participate.