110 Getting started with Shiny

Written by Matthew Wankiewicz and last updated on 7 October 2021.

110.1 About

Shiny apps are a great way to show the characteristics of a dataset interactively. Shiny apps are easy to create and can exist as a standalone website, or can be included in R Markdown documents as well. Shiny apps can also be used to demonstrate how statistical processes work and can be used for teaching.

Some amazing examples of Shiny apps include:

110.2 The Parts

The process for creating Shiny apps is fairly simple. First, you will want to install the shiny package in R, this package allows you to create your apps.

Next, you can go to the top left in your RStudio window (same applies for RStudio cloud) and click on the blank page with a plus on it. After doing this, go down to “Shiny Web App…” and click on it. This will create a new folder where your Shiny App will be stored. This new file will already contain a default Shiny App which is a simple example of an app.

  • The same can also be done by creating a new R project for a Shiny app.

110.3 Making the App

Shiny apps have three main sections for code. We will briefly look at them now and then go more in depth in the next section.

  1. ui:
    • The ui section will control what the person using your Shiny app will see. This includes things like what the title is, how many tabs will be included on the page and what type of inputs your app will include (sliders, dropdown menus, etc.)
  2. server:
    • The server section will control what the viewer cannot see. The server section can be used to create predictions, plots or other calculations. You can also use the server section to include a text write-up below a plot.
  3. Other things:
    • You can also write code before the ui and server sections. This can be used to create models, do data cleaning or call in other datasets after you have cleaned them in other R scripts.

After you have completed the three sections, you can run your app by either clicking the “Run App” in the top right corner or running the shinyApp function at the bottom of your R script.

110.4 Arguments

ui:

Your ui section will be usually be contained in the fluidPage function. This is where you will include things like your title, the headers for different tabs on the page and any filters or sliders you want to include on your page.

  • To create a title, you can use the titlePanel function, you can simply write the function and then your title on the inside, for example: titlePanel("My Shiny App").

  • Next, you will want to work on the main parts of the page, you can either use the tabPanel function if you plan on having multiple tabs, or the sidebarPanel function if you plan on having one tab/page.

    • If you plan on having multiple tabs, the tabPanel function can be used with sidebarPanel to make your pages. The function’s arguments include a title and then you can move on to customizing the sidebar.
    • To customize the sidebar, first call the sidebarLayout function, this will allow you to include side and main panels.
  • Next, call sidebarPanel and include anything you want to filter your data by or anything you want to calculate. This can include picking teams to make a prediction of which team will win or picking a range of values to see what will fit into the option.

    • The different options for filtering include slider inputs so the user can pick a range of values to include, numeric inputs so the user can pick an exact value, dropdown menus to select a specific option, checkboxes and many more!
    • After you have edited the sidebar to your liking, you can add in the placeholder for the main panel, this will include things like tables, plots, text or maps that will change depending on what happens in the sidebar.
      • If you want to create a changing text box in your main panel, use the textOutput function to do so. Inside the function, write in a keyword that will be used in the server section to change the plot.
      • If you want to create a table, use the tableOutput function and for plots use the plotOutput function.

server:

As we saw in the earlier section, the server part of a Shiny app will be used to write code that will change the app, but the viewer will not see it. When making the server, it will be written as server <- function(input, output) {}. With your code being written inside the {}. The code written in this function will depend on what you want to show the viewer and what you have defined in ui.

  • If you decided to include a text box in your Shiny app, you will need to use the renderText function. This is where you can write sentences and change them depending on what is selected in the sidebar. For example, if you wanted to say which animal was selected out of a list, your function will look like: output$sentence <- renderText({paste("you have selected", input$animal)})
    • A few things to note, you must save each textbox, table or plot under output$keyword, where the keyword is the one you defined in the ui phase. Also, the use of animal after input was to represent what was selected in the sidebar.
  • The same idea goes for plots and tables, this time you will use the renderPlot and renderTable functions to create the outputs.

110.5 Live Coding

  • Example 1:
  • Example 2:

To get your access token from GitHub use usethis::browse_github_token, it will open up a new page and you can access a token from GitHub there. It will also tell you how to store your token after running the function. (This is also covered in the GitHub lessons.)

110.6 After you code your app

Once your code is completed and everything works properly, you can publish your app. Publishing the app requires an account but is free to do. To publish your app, click on the blue circle beside the “Run App” button on the header of app’s code. You will want to create your app using shinyapps.io, this is the free version of publishing your app. Once you do this you will likely have to create your shinyapp account and enter in your secret key for your account. After you’ve done this, your account should be connected and you can publish your app. The url for the app will likely be username.shinyapps.io/file_name. After your app is published you can share it with others.

A video explaining how to publish your Shiny app can be found here.

The next two questions will use the Shiny app located at: https://yhejazi.shinyapps.io/tutorial/

Image of one page of the Shiny App, from Yasmine Hejazi

110.7 Common Mistakes & Errors

One of the tough parts about Shiny apps is that after you compile the code into the app, there can still be errors which appear, but only after you test different combinations in your app. For example, if your app has a dropdown menu to filter options, there may be errors that occur for certain choices that you won’t see until the app has been made. The best way to test for errors in Shiny apps is to test out different combinations in your app once it has been made.

Here are some common issues that occur when making Shiny apps.

Plots or Tables not Showing up

  • If you have a plot/table not showing up, one of the first things you should do is make sure that your output$keyword in the server section matches your keyword defined in the ui section. If you place a keyword which does not exist in the server section, R will not create any plots for that keyword. To fix this, check to make sure your keyword exists and matches the one defined in the ui section.

  • This error can also occur if you try to reference an input which does not exist. Again, the fix for this is to make sure the keyword you are using exists and matches the one defined in ui.

Problem with filter() input ..1

  • Errors like this will usually occur in the spot where your table or plot will be located. These errors are not exclusive to the filter function but can appear for any function. When these errors occur, it is usually because the function is being used incorrectly. For example, trying to filter data from a variable that doesn’t exist or using a ggplot argument which does not make sense.

Some other common issues which can occur include:

  • Missing/extra commas or brackets when defining different inputs on the ui section.
    • Usually shown by “argument is missing, with no default.”
  • Not using the proper render function to display your data. For example, using renderPlot when trying to display a table.
    • If you try to ggplot with renderTable you will get a “cannot coerce class ‘c(“gg,” “ggplot”)’ to a data.frame” error.
    • If you try to display a table with renderPlot you will an error saying “invalid quartz() device size.”

A helpful link for debugging any issues that occur when making Shiny apps is https://shiny.rstudio.com/articles/debugging.html. This website goes through things like debugging and finding where your errors are happening.

110.8 Next Steps

Some helpful links for creating Shiny apps include:

110.9 Questions

  1. What are Shiny apps?
  1. Apps that are shiny
  2. Interactive apps made using RStudio
  3. Interactive apps made using python
  4. Knitted R markdown files
  1. True or False, Shiny apps can include content such as quizzes?
  1. True
  2. False
  1. What does the ui section do on a Shiny app?
  1. The ui section creates plots for your app
  2. The ui section will control what the person using your Shiny app will not see
  3. The ui section will control what the person using your Shiny app will see
  4. The ui section does not do anything
  1. What does the server section do on a Shiny app?
  1. The ui section creates plots for your app
  2. The ui section will control what the person using your Shiny app will not see
  3. The ui section will control what the person using your Shiny app will see
  4. The ui section does not do anything
  1. If I want to add a table to my Shiny app, which function should I use?
  1. renderText
  2. renderObject
  3. renderPlot
  4. renderTable
  1. True or False, you need to pay to publish a Shiny app?
  1. True
  2. False
  1. Which file type should I select to create a Shiny app?
  1. “R Script”
  2. “R Markdown”
  3. “Text File”
  4. “Shiny Web App”
  1. How should input keywords be referred to in the server section?
  1. output$keyword
  2. input$keyword
  3. input.keyword
  4. input-keyword
  1. What should we save our output files as in the server section?
  1. output$keyword
  2. input$keyword
  3. output.keyword
  4. output-keyword
  1. What causes the “Problem with filter() input ..1” error?
  1. The function was not loaded
  2. The dataset does not exist
  3. The function was used incorrectly
  4. None of the above