Introduction to R Shiny
Data VisualizationFeatured PostRTools & LanguagesConferencesData VisualizationRR Shinyposted by Alyssa Columbus March 7, 2019 Alyssa Columbus
Alyssa is a speaker for ODSC East 2019 this April 30 to May 3! Attend her talk “Data Visualization with R Shiny.”
What is R Shiny?
Shiny is an R package that enables you to build interactive web apps using both the statistical power of R and the interactivity of the modern web. An excellent and efficient alternative to spreadsheets and printed visualizations, R Shiny saves space and time in the construction, automation, and distribution of data visualizations and statistical analyses.
[Related article: Sentiment Analysis in R Made Simple]
What’s in a shiny app?
Using this fluidPage layout, the app uses all available browser width regardless of the viewer’s screen size, resolution, or type, so you, the app developers, don’t need to worry about defining relative widths for individual app components. Also, under the hood, shiny implements layout features available in Bootstrap 2, a popular HTML/CSS framework, but the nice thing about working with shiny is that no HTML, CSS, or even Bootstrap experience is necessary to make an app.
Starting a new R Shiny app
In RStudio, navigate to File > New Project > Shiny Web Application. Name your application and save it in your desired directory. You now have a sample R Shiny app to customize and make your own! Note that the basic anatomy of an R Shiny app is represented here with the ui and server. Notice also that there are a few basic inputs and outputs including a sliding numeric input and histogram plot output.
5 Best Practices for Shiny App Development
- Watch out for commas! Missing or extra commas in your ui are by far the most common errors that are fixed by shiny app developers.
- When debugging, always run the entire script, not just up to the point where you’re developing code since you can only see where you went wrong by running the entire application. Often, the best way to see what’s wrong is to run the app and review the error in your RStudio Console or shiny app’s ui.
- Test/run your app externally in your browser to see how it truly looks before it’s deployed to production. This will also correct some common errors related to downloading content from the app.
- Use the shinytest and shinyloadtest packages to write unit tests for your app, make sure it won’t crash if multiple people use it at once, and measure performance degradation.
- View your reactlog to check your app’s reactive dependencies and their execution. In a fresh R session, type `options(shiny.reactlog = TRUE)`, launch your app as you normally would, and press CTRL + F3 in the app to view your reactlog. More information about reactlogs can be found in the RStudio Documentation.
Share Your App with the World!
To share your shiny app with the world, you need to host it on a server. Here are 3 convenient ways for you to do this:
- Host on shinyapps.io
RStudio provides a service called shinyapps.io which lets you host your apps for free. It is integrated into RStudio so that you can publish your apps with the click of a button, and it has a free version. The free version allows a certain number of apps per user and a certain number of activities on each app, but it should be good enough for just starting out with Shiny. It also lets you see some basic stats about the usage of your app.
Hosting your app on shinyapps.io is the easy and recommended way of getting your app online. Go to www.shinyapps.io and sign up for an account. When you’re ready to publish your app, click on the “Publish Application” button in RStudio and follow their instructions. You might be asked to install a couple packages if it’s your first time.
After a successful deployment to shinyapps.io, you will be redirected to your app in the browser. You can use that URL to show off to your boss or family and friends what a cool app you wrote.
- Host on a Shiny server
The other option for hosting your app is on your private Shiny server, which is also free. Instead of RStudio hosting the app for you, you’ll have it on your private server. This means you’ll have a lot more freedom and flexibility, but it also means you’ll need to have a server and be comfortable administering a server. I currently host all my apps on my Shiny server just because I like having the extra control, but when I first learned about Shiny, I used shinyapps.io for a few months.
- Host on an AWS EC2 Instance
My coworkers and I use this hosting environment at my workplace. It’s fast, efficient, and I highly recommend it. Work with your IT department, and use this tutorial to set it up.
[Related article: Key Principles of Data Storytelling]
Shiny in Style! – Additional Features of R Shiny
- Shiny in RMarkdown: You can include Shiny inputs and outputs in an RMarkdown document! This means that your RMarkdown document can be interactive. See Shiny Interactive Elements in RMarkdown for more information.
- Shinydashboard layouts: You can build a Shiny dashboard in a very similar fashion to the way you can build a simple shiny app. See the documentation for the shinydashboard package for more information.
- Shiny Loading Animations: See the shinycssloaders package for more information on how to add loading animations for your overall app and its individual components.
- If your app has a large codebase: Split app.R into ui.R, server.R, and global.R. ui.R and server.R should store ui and server objects respectively. Use the global.R file to store all objects that you want to have available to both your ui and server objects (e.g. packages, data, custom-built functions, etc.)
- When testing your app in an external browser: If you close your browser window and the app is still running, you need to press “Esc” to kill it manually. By adding a single line to the end of your server code:
Your Shiny app will automatically stop running whenever your browser tab (or any session) is closed.
Add-on Packages for Shiny
- shinythemes – adds Bootstrap CSS themes to Shiny applications
- shinydashboard – wraps the Admin LTE library for creating dashboard interfaces
- DT – interactive tables
- dygraphs – interactive time-series charts
- ggvis – similar to ggplot2, but the plots are focused on being web-based and are more interactive
- googleVis – interface to the Google Charts API
- loggit – effortless exception logging
- reticulate – R interface to Python modules, classes, and functions
- promises – enables asynchronous evaluation of R code
- And many more! Explore them at gallery.htmlwidgets.org.
Remember to use the best practices given in this blog post, but don’t be afraid to be creative and have fun in your Shiny app design. If you have any questions about the content in this post or R Shiny in general, contact me via my website: alyssacolumbus.com.
Want to learn more about R Shiny in person? Attend ODSC East 2019 and check out Alyssa’s talk, “Data Visualization with R Shiny.”