Wednesday, October 26, 2016

Shiny and R: How to Add Themes and Customize Error Messages

R Shiny themes and error messages

The purpose of Shiny is to provide an intuitive and user-friendly interface to R. R is a highly popular statistical environment for doing heavy data analysis and constructing statistical models, and therefore is highly popular among data scientists. However, for a user with a non-coding background, using R to conduct such analysis can become quite intensive. This is where Shiny Web Apps come in. Essentially, Shiny allows for a more intuitive graphical user interface that is still capable of conducting sophisticated data analysis --- without the need for extensive coding on the part of the end user.

In my article on using Shiny with R and HTML, I illustrated how an interactive web application can be created to conduct analysis without the need for direct manipulation of code. In this article, I'll use a slightly different model to illustrate how the Shiny environment can be customized to work with the end user in a more intuitive fashion. Essentially, the goal of this article is to illustrate how a user can:

  1. Build an application by linking the UI and server side
  2. How to customize the themes available in the Shiny Themes library
  3. Implement error messages in order to provide guidance to an end user on how to use a particular program

The program itself that is developed for this tutorial is quite basic: a slider input allows the user to manipulate a variable within the program by means of reactivity, which causes instantaneous changes in the line plot output that is developed by means of reactivity.

This inherent function gives Shiny a significant advantage over using R code as a stand-alone. Traditionally, in order to analyze the change in a particular variable, the code must be manipulated directly (or the data from which the code is reading), and this can ultimately become very inefficient. However, Shiny greatly speeds up this process by allowing the user to manipulate the variables in a highly intuitive manner, and changes are reflected instantly.

However, the whole purpose of Shiny is to make an R Script as interactive as possible. In this regard, the user will want to be able to add features to the program that go well beyond reactivity. Two such aspects of this that I will discuss in this tutorial are:

  1. Using shinythemes in order to customize the appearance of our Shiny appearance
  2. Constructing a validate() function in order to display an alert once variables are manipulated in a certain manner

Shiny Themes

In the UI of our original program, we did not specify a theme, and therefore ended up with quite a basic (default Bootstrap) view of our output:

default Shiny app view

When we defined the shinyUI(fluidPage(…)) function, we didn't specify a theme. In this instance, we're going to specify several of the themes available from the Shiny Themes package.

After we've specified shinyUI(fluidPage(…)), we're also specifying our theme as follows:

shinyUI(fluidPage(theme = shinytheme("cyborg")

the cyborg theme

From the above, we see that selecting the Cyborg theme gives our web app a dark background. As another example, let’s now select the Spacelab theme and see what we come up with:

the spacelab theme

These themes demonstrate more levels of customization depending on the other attributes in the Shiny code, such as additional buttons, tabs, etc. Feel free to experiment with the other themes at the RStudio GitHub account and see what you come up with!

Continue reading %Shiny and R: How to Add Themes and Customize Error Messages%


by Michael Grogan via SitePoint

No comments:

Post a Comment