Introduction to Shiny

shiny is very effective package in order to create interactive dashboards on web browsers. The shiny applications can be deployed into Shinyapps Website. Or you can run the example shiny app by writing a shiny::runGitHub code into your RStudio. At the end of this document, you can see how you can run the following sample application.

Example Application

Preparation of Dataset

pti <- c("shiny","tidyverse","ggplot2movies")
pti <- pti[!(pti %in% installed.packages())]
if(length(pti)>0){
    install.packages(pti)
}

### Shiny starter code

library(shiny)
library(tidyverse)
library(ggplot2movies)

# Set randomness seed
set.seed(61)
# Prepare data
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

Define ‘UI’ for application

ui <- fluidPage(

    # Application title
    titlePanel("Movie Length and IMDB Scores"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("years",
                        "Years",
                        min = 2000,
                        max = 2005,
                        value = c(2002,2003),
                        ticks = FALSE,
                        sep = ""),
            selectInput(
                inputId = "genre",
                label = "Genre",
                choices = c("All",genres),
                selected = "Action",
                multiple = TRUE),
            sliderInput("minvotes",
                        "At Least X Votes",
                        min = min(shiny_movie_set$votes),
                        max = max(shiny_movie_set$votes),
                        value = median(shiny_movie_set$votes),
                        ticks = FALSE),
        ),

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

Define ‘server’ logic

server <- function(input, output) {

    output$moviePlot <- renderPlot({
        
        # Control print-outs
        print("===Trigger===")
        print(input$years)
        print(input$genre)
        print(input$minvotes)
        
        plot_df <- shiny_movie_set %>%
            filter(year >= input$years[1] & year <= input$years[2] & votes >= input$minvotes)
        if(!("All" %in% input$genre)){
            plot_df <- plot_df %>% filter(genre %in% input$genre)
        }
        
        print(paste0("Number of rows: ", nrow(plot_df)))
            
        ggplot(plot_df, aes(x=length,y=rating,color=genre)) + geom_point()
    })
}

Run the application

Shiny applications not supported in static R Markdown documents. In order to run the application, you can write the following code in your RStudio.

shiny::runGitHub("pjournal/boun01-canaytore",subdir="shiny",ref='gh-pages')

End of the document. Visit my Progress Journal