Shiny Package

shiny package provide interactive web implementations by using R programming language. By using this package, analysis can be made interactively as web applications. Shiny application consists of two main parts:

  1. User interface (ui) part: This part provide the usage implementation and visualization of the analysis
  2. Server part: This part provide analysis and connection between analysis and user interfaces.

Packages and Preparaion of Data Set

#installation of the important three packages which are shiny, which provide this app implementation, tidyverse for calculation, analysis, and the data set ggplot2movies
pti <- c("shiny","tidyverse","ggplot2movies")
pti <- pti[!(pti %in% installed.packages())]
if(length(pti)>0){
    install.packages(pti)
}

##########
### Shiny starter code
##########
#after the installation of the packages we call these packages to our library by using library function
library(shiny)
library(tidyverse)
library(ggplot2movies)

# Set randomness seed
# set.seed function is used to produce random numbers and get same result in each trials
set.seed(61)
# Prepare data
# After the creating of the random numbers, we prepare the data for analysis
# For this we create shiny_movie_set and filter the movies according to the year that greater than equal to 2000
shiny_movie_set <- 
    movies %>% 
    filter(year >= 2000) %>%
    select(title,year,length,rating,votes,Action:Short) %>% 
    gather(genre,value,Action:Short) %>% 
    filter(value == 1) %>% 
    select(-value)

# Get genre list
genres <- 
    shiny_movie_set %>% 
    distinct(genre) %>% 
    unlist(.)

names(genres) <- NULL

Preparing User Interface (UI)

In this part of the shiny app we create the user interface that provides the interactive implementations. That is by using slider input or select button, we can choose the different alternatives. In this example, we implement

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Movies and IMDB Rankings"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("years",
                        "Years",
                        min = 2000,
                        max = 2005,
                        value = c(2002,2003), # by using vector to define value we can obtain two open side slider
                        sep = ""), #sep part provide to not use comma for the huge numbers, i.e. 2000 instead of 2,000
            selectInput(
                inputId = "genres",
                label = "Genre",
                choices = c("All",genres),
                selected = "Drama" , # provide default value for the first appeared page
                multiple = TRUE      # multiple provides the muliple selection at the same time
            ),
            sliderInput(
                "minvotes",
                "At Least X Votes",
                min = min(shiny_movie_set$votes),
                max = max(shiny_movie_set$votes),
                value = median(shiny_movie_set$votes),
                sep = ""
            )
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("MoviePlot")
        )
    )
)

After the creating of the interface, we need to prepare analysis of the data set. This preparation is obtained by using server part.

Preparing Server

server <- function(input, output) {

    output$MoviePlot <- renderPlot({
        
        #we try to 
        plot_df <- shiny_movie_set %>%
            filter(year >= input$years[1] & year <= input$years[2]) %>%
            filter(votes >= input$minvotes)%>%
            filter(genre %in% input$genres | "All" %in% input$genres)
        
        #if(!("All" %in% genres)) {
            #plot_df <- plot_df %>% filter(genre %in% input$genres)} # in this part we do not add "All" movies into the graph for this reason we use "|" as given in the previous line.
        
        ggplot(plot_df, 
               aes(x = length, 
                   y = rating,
                   color = genre)) + geom_point() # provide scatter plot for the data
    })
}

Combination of the UI and Server

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

This shiny can be updated by using different type of the buttons and other useful infomation can be examined by using Shiny Cheatsheet.

In this example the shiny app can be accessed by using shiny::runGitHub(“pjournal/boun01-EbruGecici”,subdir=“Shiny”,ref = “gh-pages”).

Note: In this example, to make more efficient learning information and application, I study with N. Nazlı Gül.