• Illumined Insights
  • Posts
  • Shiny Apps in R: An Introduction to Building Interactive Data Visualizations

Shiny Apps in R: An Introduction to Building Interactive Data Visualizations

Part 2: Publishing Your Shiny App

Welcome to the “Illumined Insights” newsletter! Thank you so much for subscribing. This weekly newsletter touches on all things analytics and data science with a focus on areas such as data visualization and sports analytics. This week we move to Part 2 of our series on creating a Shiny application in R. If you missed Part 1, check it out here: Part 1.

Stephen Hill, Ph.D.

In last week’s newsletter we built a simple R Shiny application to explore the Palmer penguins dataset from the “palmerpenguins” R package. We started by building the Shiny “ui” and “server” code. For convenience, that code is provided below. For a more detailed description of this code, check out last week’s newsletter (linked above). The “ui” code (including necessary R packages) is first:

library(shiny)
library(tidyverse)
library(palmerpenguins)

ui <- fluidPage(
  titlePanel("Palmer Penguins Explorer"),
  sidebarLayout(
    sidebarPanel(
      selectInput("species", label = "Penguin species:",
                  choices = unique(penguins$species)),
      selectInput("x_var", label = "X-axis variable:",
                  choices = names(penguins)[4:7]),
      selectInput("y_var", label = "Y-axis variable:",
                  choices = names(penguins)[4:7]),
      sliderInput("size", label = "Point size:",
                  min = 1, max = 10, value = 5)
    ),
    mainPanel(
      plotOutput("penguin_plot")
    )
  )
)

Next is the “server” code:

server <- function(input, output) {
  
  output$penguin_plot <- renderPlot({
    filtered_data <- penguins %>%
      filter(species == input$species)
    
  ggplot(filtered_data, aes_string(x = input$x_var, y = input$y_var)) +
      geom_point(alpha = 0.5, size = input$size, color = "steelblue") +
      ggtitle(paste(input$x_var, "vs.", input$y_var, "for", input$species)) +
      theme_bw() +
      theme(plot.title = element_text(hjust = 0.5))
  })
}

Running this application locally produces a Shiny application that looks like this:

Shiny app

While it may be fine to simply run the application locally, one of the beauties of working with Shiny is the ability to publish your application so that it is accessible online to any user with an internet browser. Let’s take a moment to walk through the steps to publish our application to shinyapps.io. This site, run by the company Posit (the company that produces RStudio), is free to use for the first five applications that you publish. Paid plans are also available for more robust needs.

To get started you’ll need to set up a free account at shinyapps.io. This takes just a few minutes. You will then name your shinyapps.io site. Go ahead and complete Steps 1 and 2 as shown in the screenshot below (I’ve blurred out my user credentials). Don’t do Step 3 yet!

shinyapps.io set-up for R/RStudio

You’re now ready to publish your Shiny application to shinyapps.io. Open your “app.R” file in RStudio. Recall that this file holds your Shiny application code. In the upper-right corner (see screenshot below) there is a small button. This is the Publish button that allows us to publish our application.

Publish button in RStudio

Go ahead and click this button. If you successfully completed Steps 1 and 2 from earlier, you should see an interface that looks similar to the screenshot below:

Publish interface in RStudio

We only have one file that we need to upload “app,R”, so we’ll make sure that it is checked. If other files are present, you can uncheck them as they are not necessary for this application. Your account name will be different and you’ll have a blank space to name your application. I named mine “PenguinExplore1”. Click “Publish” to publish your application to shinyapps.io. Be patient as this can take a few minutes. If you are successful, a browser window with your published application will open.

In the next part of this series, we’ll add a few enhancements to our Shiny application and discover a few other Shiny features.

Are you interested in learning more about data visualization using R? Click below to get notified about my upcoming book “Data Visualization in R”.

Each week we’ll feature a dataset that we find interesting, useful, etc. This week we look at the RAND Corporation’s “Database of Worldwide Terrorism Incidents”. I was looking for some information on a form of regression that I have not used before (Dirichlet Regression, if you happen to be curious) and came across an article that used that particular form of regression to help study this terrorism incident database. This database is relatively easy to work with as it is provided in CSV (comma-separated values) format that can be opened in Excel or easily ingested into the analytics software tool of your choice.

Feedback?

Did you enjoy this week’s newsletter? Do you have a topic, tool, or technique that you would like to see featured in a future edition? I’d love to hear from you!

Support the Newsletter?

Support this newsletter with a “coffee” (optional, but appreciated).

Start Your Own Newsletter?

This newsletter is created on and distributed via Beehiiv, the world’s best newsletter platform. Want to start your own newsletter? Click below to get started. Please note that this is an affiliate link. I may receive a small commission if you sign up for Beehiiv via this link.