Getting to Know Shiny

RShiny R In-Class Exercise

This in-class exercise introduces Shiny as an example of web-enabled visual analytics application.

Archie Dolit https://www.linkedin.com/in/adolit/ (School of Computing and Information Systems, Singapore Management University)https://scis.smu.edu.sg/
07-17-2021

Shiny: Overview

What is so special about Shiny?

It allows R users:

The Structure of a Shiny app

A Shiny app comprises of two components, namely:

Shiny’s user-interface, ui.R

The ui.R script controls the layout and appearance of a shiny app.

Shiny’s server server.R

The server.R script contains the instructions that your computer needs to build your Shiny app.

You are expected to:

Building a Shiny app

Shiny app example

The preview of Shiny app can be seen below:

The app reads the Exam_data.csv and generate a histogram. The user can select the subject to be displayed, the number of bins for the histogram, and enable/disable the data table. In this simple Shiny app, we were able to load the dataset, work with titlePanel, sidebarLayout(), checkboxInput(), plotOutput(), dataTableOutput(), renderPlot(), and renderDataTable().

Shiny app code

The code chunk can be seen below:

library(shiny)
library(tidyverse)

exam <- read_csv("data/Exam_data.csv")

# Define UI for application
ui <- fluidPage(
    titlePanel("Pupils Examination Results Dashboard"),
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "variable",
                        label = "Subject: ",
                        choices = c("English" = "ENGLISH",
                                    "Maths" = "MATHS",
                                    "Science" = "SCIENCE"),
                        selected = "ENGLISH"),
            sliderInput(inputId = "bin",
                        label = "Number of bins",
                        min = 5,
                        max = 20,
                        value = c(10)),
            checkboxInput(input = "show_data",
                          label = "Show data table",
                          value = TRUE)
        ),
        mainPanel(
            plotOutput("distPlot"),
            DT::dataTableOutput(outputId = "examtable")
        )
    )
)

# Define server logic required
server <- function(input, output) {
    output$distPlot <- renderPlot({
        #unlist - data contains string
        x <- unlist(exam[,input$variable])
        
        ggplot(exam, aes(x)) + 
            geom_histogram(bins = input$bin,
                           color = "black",
                           fill = "light blue")
    })
    
    output$examtable <- DT::renderDataTable({
        if (input$show_data){
            DT::datatable(data = exam %>%
                          select(1:7),
                          options = list(pageLength = 10),
                          rownames = FALSE)
        }
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Reference

Citation

For attribution, please cite this work as

Dolit (2021, July 17). Visual Analytics & Applications: Getting to Know Shiny. Retrieved from https://adolit-vaa.netlify.app/posts/2021-07-17-know-shiny/

BibTeX citation

@misc{dolit2021getting,
  author = {Dolit, Archie},
  title = {Visual Analytics & Applications: Getting to Know Shiny},
  url = {https://adolit-vaa.netlify.app/posts/2021-07-17-know-shiny/},
  year = {2021}
}