Question 1.

Install the following packages shiny, packrat, rsconnect.

library(shiny)
library(packrat)
library(rsconnect)

In this question, you will build and publish your first shiny app. Following these steps to do so

  1. Create a new R Script (File -> New R Script).

  2. Copy these code to the file

  3. Save the file as app.R file into a newly created folder.

  4. Press Ctrl + Shift + Enter to run the app locally.

  5. Go to https://www.shinyapps.io/

  6. Create an account

  7. Go to https://www.shinyapps.io/admin/#/dashboard. Copy the secret code.

It should look like this

rsconnect::setAccountInfo(name='fall20',
              token='E130D64F22776383660DA7EA5251EC04',
              secret='mJI7rgq2Wf46g2Wf46g2WNOa+E6NRqr26yG3N5')
  1. Go back to the app (If you closed it, hit Ctrl + Shift + Enter to reopen it), click to Publish on the top right corner.

  2. Paste the secret code and follow the instruction to publish the app on the Internet.

Question 1 App

# Set User Interface
ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  
  sidebarLayout(
    
    sidebarPanel(
      sliderInput(
        inputId = "var1",
        label = "Decide a number",
        min = 1,
        max = 100,
        value=10
      )
      
    ),
    
    mainPanel(
      # Output: Histogram ----
      plotOutput(outputId = 'blah1')
    )
    
  )
  
)


######################################
# Main codes for the app
server <- function(input, output) {
  
  output$blah1 <-renderPlot({
    m = input$var1
    hist(rnorm(mean=m, n=1000))
  }
  )
  
  
}

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

Question 2

Write a shiny app that can plot barplot of two categorical variables in the titanic dataset.

Notice: Two different shiny apps should be in two different folders.

Question 2 App

library(tidyverse)
library(shiny)

d = read_csv('titanic.csv')

variables_names = names(d)

ui <- fluidPage(
  
  titlePanel("Barplots"),
  
  sidebarLayout(
    
    sidebarPanel(
      
      selectInput(
        inputId ="var1",
        label = "Select a Categorical Variable",
        choices = variables_names, selected = "Pclass"
      ),
      
      selectInput(
        inputId ="var2",
        label = "Select a Categorical Variable",
        choices = variables_names,
        selected = "Sex"
      )
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Histogram ----
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    d = read_csv('titanic.csv')
    v1 = input$var1
    v2 = input$var2
    
    
    library(ggplot2)
    
    r = ggplot(d, aes(x = as.factor(d[[v1]]), fill = as.factor(d[[v2]])))+
      geom_bar()+
      labs(x = v1, fill = v2)
    
    return(r)
    
  })
  
}
# app
shinyApp(ui = ui, server = server)

Question 3

Write a shiny app that can plot barplot of two categorical variables in an uploaded dataset.

Question 3 App

library(tidyverse)
library(shiny)
library(DT)

ui <- fluidPage(
  
  titlePanel("Barplot Visualization"),
  
  sidebarLayout(
    
    sidebarPanel(
      fileInput('f1', label = 'Upload data for visualization', accept = '.csv'),
      
      selectInput('v1', label='Select a Categorical Variable', choices=''),
      selectInput('v2', label='Select a Categorical Variable', choices='')
      
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server
server <- function(input, output, session) {
  
  myData <- reactive({
    inFile = input$f1
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  }
  )
  
  output$show_plot <- renderPlot({
    
    inFile = input$f1
    v1 = input$v1
    d <- read.csv(inFile$datapath, header = TRUE)
    
    v1 = input$v1
    v2 = input$v2
    
    
    library(ggplot2)
    
    r = ggplot(d, aes(x = as.factor(d[[v1]]), fill = as.factor(d[[v2]])))+
      geom_bar()+
      labs(x = v1, fill = v2)
    
    return(r)
    
  })
  
  
  observeEvent(input$f1,{ 
    inFile = input$f1
    data <- read.csv(inFile$datapath, header = TRUE)   
    updateSelectInput(session, 'v1', choices = names(data))}
  )
  
  observeEvent(input$f1,{ 
    inFile = input$f1
    data <- read.csv(inFile$datapath, header = TRUE)   
    updateSelectInput(session, 'v2', choices = names(data))}
  )
  
}


shinyApp(ui = ui, server = server)

Question 4

Write a shiny app that can plot the scatter plot of two numeric variables and colored by a categorical variable in an uploaded dataset. Example of the plot: https://bryantstats.github.io/math421/slides/6_viz.html#15

Notice: that you can only run 5 apps on shinyapps.io. If you are running out of apps, you may want to create another account and use multiple accounts. You may want to try publishing a few times. You could also embed all your shiny apps in an Rmarkdown. Do the follows to embed an shiny app to an rmarkdown.

Question 4 App

library(tidyverse)
library(shiny)
library(DT)

ui <- fluidPage(
  
  titlePanel("Visualization"),
  
  sidebarLayout(
    
    sidebarPanel(
      fileInput('f1', label = 'Upload data for visualization', accept = '.csv'),
      
      selectInput('v1', label='Select a Numeric Variable', choices=''),
      selectInput('v2', label='Select a Numeric Variable', choices=''),
      selectInput('v3', label='Select a Categorical Variable', choices='')
      
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server
server <- function(input, output, session) {
  
  myData <- reactive({
    inFile = input$f1
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  }
  )
  
  output$show_plot <- renderPlot({
    
    inFile = input$f1
    v1 = input$v1
    d <- read.csv(inFile$datapath, header = TRUE)
    
    v1 = input$v1
    v2 = input$v2
    v3 = input$v3
    
    
    library(ggplot2)
    
    r = ggplot(d, aes(x = d[[v1]], y = d[[v2]], color = as.factor(d[[v3]])))+
      geom_point()+
      labs(x = v1, y = v2, color = v3)
    
    return(r)
    
  })
  
  
  observeEvent(input$f1,{ 
    inFile = input$f1
    data <- read.csv(inFile$datapath, header = TRUE)   
    updateSelectInput(session, 'v1', choices = names(data))}
  )
  
  observeEvent(input$f1,{ 
    inFile = input$f1
    data <- read.csv(inFile$datapath, header = TRUE)   
    updateSelectInput(session, 'v2', choices = names(data))}
  )
  
  observeEvent(input$f1,{ 
    inFile = input$f1
    data <- read.csv(inFile$datapath, header = TRUE)   
    updateSelectInput(session, 'v3', choices = names(data))}
  )
  
}


shinyApp(ui = ui, server = server)

Question 5

Write a shiny app create a plot for the titanic dataset. The user inputs two variables and can change the range of Age (i.e., use sliderInput).

Question 5 App

library(tidyverse)
library(shiny)

d = read_csv('titanic.csv')

variables_names = names(d)

ui <- fluidPage(
  
  titlePanel("Bar Plot"),
  
  sidebarLayout(
    
    sidebarPanel(
      
      selectInput(
        inputId ="var1",
        label = "Select a Categorical Variable",
        choices = variables_names, selected = "SibSp"
      ),
      
      selectInput(
        inputId ="var2",
        label = "Select a Categorical Variable",
        choices = variables_names,
        selected = "Sex"
      ), 
      
      sliderInput(inputId = "age",
                  "Select Age Range:",
                  min = min(d$Age, na.rm=TRUE),
                  max = max(d$Age, na.rm=TRUE),
                  value= c(1, 80))
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    v2 = input$var2
    
    
    library(ggplot2)
    
    d <- d %>% filter(Age>input$age[1], Age<input$age[2])
    
    ggplot(d, aes(x = d[[v1]], fill = as.factor(d[[v2]])))+
      geom_bar()+
      labs(x = v1, fill = v2)
    
    
  })
  
}
# app
shinyApp(ui = ui, server = server)

Question 6

Write a shiny app create plots for the titanic dataset. The user inputs two variables and can choose a multiple choice option (i.e., useradioButtons).

Question 6 App

library(tidyverse)
library(shiny)

d = read_csv('titanic.csv')

variables_names = names(d)

ui <- fluidPage(
  
  titlePanel("Plots for the Titanic Data"),
  
  sidebarLayout(
    
    sidebarPanel(
      
      selectInput(
        inputId ="var1",
        label = "Select a Numeric Variable",
        choices = variables_names, selected = "SibSp"
      ),
      
      selectInput(
        inputId ="var2",
        label = "Select a Categorical Variable",
        choices = variables_names,
        selected = "Sex"
      ),
      
      radioButtons(inputId = "plot_choice", 
                   label = h3("Select Plot:"),
                   choices = c("Boxplot" = "boxplot",
                               "Density Plot" = "density",
                               "Bar Plot" = "barplot"),
                   selected = 'barplot')
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    v2 = input$var2
    
    
    library(ggplot2)
    
    if(input$plot_choice == 'boxplot')
    {
      ggplot(d, aes(x = d[[v1]], color = as.factor(d[[v2]])))+
        geom_boxplot()+
        labs(x = v1, color = v2)
    }
    
    else if(input$plot_choice == 'density')
    {
      ggplot(d, aes(x = d[[v1]], color = as.factor(d[[v2]])))+
        geom_density()+
        labs(x = v1, color = v2)
    }
    
    else
    {
      ggplot(d, aes(x = d[[v1]], fill = as.factor(d[[v2]])))+
        geom_bar()+
        labs(x = v1, fill = v2)
    }
    
    
    
  })
  
}
# app
shinyApp(ui = ui, server = server)

Question 7

Write a shiny app create a plot for the titanic dataset. The user inputs two variables and can choose a tick on some options of a variable (i.e., usecheckboxGroupInput).

Question 7 App

library(tidyverse)
library(shiny)

d = read_csv('titanic.csv')

variables_names = names(d)


ui <- fluidPage(
  titlePanel("Bar Plot"),
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId ="var1",
        label = "Select a Categorical Variable",
        choices = variables_names, selected = "Survived"
      ),
      
      selectInput(
        inputId ="var2",
        label = "Select a Categorical Variable",
        choices = variables_names,
        selected = "Sex"
      ),
      
      checkboxGroupInput(inputId = "Pclass", label = "Select Passenger Class",
                         choices = names(table(d$Pclass)), inline = TRUE,
                         selected = "1"),
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    v2 = input$var2
    
    d <- d %>% filter(Pclass %in% input$Pclass)
    
    library(ggplot2)
    
    ggplot(d, aes(x = as.factor(d[[v1]]), fill = as.factor(d[[v2]])))+
      geom_bar()+
      labs(x = v1, fill = v2)
    
    
  })
  
}
# app
shinyApp(ui = ui, server = server)

Question 8

Write a shiny app create a plot for the titanic dataset. Make uses of selectInput, sliderInput, radioButtons, and checkboxGroupInput.

Question 8 App

library(tidyverse)
library(shiny)

d = read_csv('titanic.csv')

variables_names = names(d)


ui <- fluidPage(
  titlePanel("Plotting the Titanic Data"),
  sidebarLayout(
    sidebarPanel(
      
      selectInput(
        inputId ="var1",
        label = "Select a Numeric Variable",
        choices = variables_names, selected = "Fare"
      ),
      
      selectInput(
        inputId ="var2",
        label = "Select a Categorical Variable",
        choices = variables_names, selected = "Sex"
      ),
      
      sliderInput(inputId = "age",
                  "Select Age Range:",
                  min = min(d$Age, na.rm=TRUE),
                  max = max(d$Age, na.rm=TRUE),
                  value= c(1, 80)
      ),
      
      checkboxGroupInput(inputId = "Pclass", label = "Select Passenger Class",
                         choices = names(table(d$Pclass)), inline = TRUE,
                         selected = "1")
      ,
    
    radioButtons(inputId = "plot_choice", 
                 label = h3("Select Plot:"),
                 choices = c("Boxplot" = "boxplot",
                             "Density Plot" = "density",
                             "Bar Plot" = "barplot"),
                 selected = 'density')
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    v2 = input$var2
    
    
    library(ggplot2)
    
    d <- d %>% filter(Age>input$age[1], Age<input$age[2])
    d <- d %>% filter(Pclass %in% input$Pclass)
    
    if(input$plot_choice == 'boxplot')
    {
      ggplot(d, aes(x = (d[[v1]]), color = as.factor(d[[v2]])))+
        geom_boxplot()+
        labs(x = v1, color = v2)
    }
    
    else if(input$plot_choice == 'density')
    {
      ggplot(d, aes(x = (d[[v1]]), color = as.factor(d[[v2]])))+
        geom_density()+
        labs(x = v1, color = v2)
    }
    
    else
    {
      ggplot(d, aes(x = (d[[v1]]), fill = as.factor(d[[v2]])))+
        geom_bar()+
        labs(x = v1, fill = v2)
    }
    
  })
  
}
# app
shinyApp(ui = ui, server = server)

Question 9

Use the below data to write this shiny app: https://fall20.shinyapps.io/app9/. Check out the app carefully to see its features.

The data: https://covidtracking.com/data/download/all-states-history.csv

Question 9 App

library(tidyverse)
library(shiny)

d = read_csv('https://covidtracking.com/data/download/all-states-history.csv')

variables_names = names(d)

d <- d %>% 
  filter(state %in% c('CA','FL','TX','MA','NY','OH'))


ui <- fluidPage(
  titlePanel("Covid 19 by States"),
  sidebarLayout(
    sidebarPanel(
      
      checkboxGroupInput(inputId = "state", label = "Select state to compare",
                         choices = c("CA","FL","TX","MA","NY","OH"), inline = TRUE,
                         selected = "CA")
      ,
      
      selectInput(
        inputId ="var1",
        label = "Select a Numeric Variable",
        choices = variables_names, selected = "positive"
      ),
      
      sliderInput(inputId = "Date",
                  "Select Date Range:",
                  min = min(d$date, na.rm = TRUE),
                  max = max(d$date, na.rm = TRUE),
                  value = c(min(d$date),max(d$date)),
                  timeFormat = "%d %b"
      ),
      
      radioButtons(inputId = "plot_choice", 
                   label = h3("Select Plot:"),
                   choices = c("Line Plot" = "line_plot",
                               "Point Plot" = "point_plot"),
                                selected = 'line_plot')
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    
    library(ggplot2)
    
    d <- d %>% filter(date>input$Date[1], date<input$Date[2])
    d <- d %>% filter(state %in% input$state)
    
    if(input$plot_choice == 'line_plot')
    {
      ggplot(d, aes(y = (d[[v1]]), x = (d$date), color = d$state))+
        geom_line()+
        labs(x = "Date", y = v1, color = d$state)
    }
    
    else
    {
      ggplot(d, aes(y = (d[[v1]]), x = (d$date), color = d$state))+
        geom_point()+
        labs(x = "Date", y = v1, color = d$state)
    }
    
  })
  
}
# app
shinyApp(ui = ui, server = server)

Question 10

Make uses of selectInput, sliderInput, radioButtons, and checkboxGroupInput to write the second app on the data used on question 9.

The data: https://covidtracking.com/data/download/all-states-history.csv

Question 10 App

library(tidyverse)
library(shiny)

d = read_csv('https://covidtracking.com/data/download/all-states-history.csv')

variables_names = names(d)


ui <- fluidPage(
  titlePanel("Covid 19"),
  sidebarLayout(
    sidebarPanel(
      
      checkboxGroupInput(inputId = "dataquality", label = "Select Data Quality Grade",
                         choices = c("A+","A","B","C","D","F"), inline = TRUE,
                         selected = c("A+","A"))
      ,
      
      selectInput(
        inputId ="var1",
        label = "Select a Numeric Variable",
        choices = variables_names, selected = "recovered"
      ),
      
      selectInput(
        inputId ="var2",
        label = "Select a State",
        choices = names(table(d$state)), selected = "MA"
      ),
      
      sliderInput(inputId = "Date",
                  "Select Date Range:",
                  min = min(d$date, na.rm = TRUE),
                  max = max(d$date, na.rm = TRUE),
                  value = c(min(d$date),max(d$date)),
                  timeFormat = "%d %b"
      ),
      
      radioButtons(inputId = "plot_choice", 
                   label = h3("Select Plot:"),
                   choices = c("Line Plot" = "line_plot",
                               "Point Plot" = "point_plot",
                               "Bar Plot" = "bar_plot"),
                   selected = 'line_plot')
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    
    library(ggplot2)
    
    d <- d %>% filter(date>input$Date[1], date<input$Date[2])
    d <- d %>% filter(dataQualityGrade %in% input$dataquality)
    d <- d %>% filter(state %in% input$var2)
    
    if(input$plot_choice == 'line_plot')
    {
      ggplot(d, aes(y = (d[[v1]]), x = (d$date)))+
        geom_line()+
        labs(x = "Date", y = v1)
    }
    
    else if(input$plot_choice == 'bar_plot')
    {
      ggplot(d, aes(y = (d[[v1]]), x = (d$dataQualityGrade)))+
        geom_col()+
        labs(x = "Data Quality Grade", y = v1)
    }
    
    else
    {
      ggplot(d, aes(y = (d[[v1]]), x = (d$date)))+
        geom_point()+
        labs(x = "Date", y = v1)
    }
    
  })
  
}
# app
shinyApp(ui = ui, server = server)

Question 11

Write a shiny app on own selected data. Make uses of selectInput, sliderInput, radioButtons, and checkboxGroupInput.

Question 11 App

library(tidyverse)
library(shiny)

d = read_csv('insurance.csv')

variables_names = names(d)


ui <- fluidPage(
  titlePanel("Plotting Health Insurance Data"),
  sidebarLayout(
    sidebarPanel(
      
      selectInput(
        inputId ="var1",
        label = "Select a Numeric Variable",
        choices = variables_names, selected = "charges"
      ),
      
      selectInput(
        inputId ="var2",
        label = "Select a Categorical Variable",
        choices = variables_names, selected = "sex"
      ),
      
      sliderInput(inputId = "bmi",
                  "Select BMI Range:",
                  min = min(d$bmi, na.rm=TRUE),
                  max = max(d$bmi, na.rm=TRUE),
                  value= c(min(d$bmi),max(d$bmi))
      ),
      
      checkboxGroupInput(inputId = "Region", label = "Select Region",
                         choices = names(table(d$region)), inline = TRUE,
                         selected = c("northeast","northwest")
      ),
      
      radioButtons(inputId = "plot_choice", 
                   label = h3("Select Plot:"),
                   choices = c("Boxplot" = "boxplot",
                               "Density Plot" = "density"),
                   selected = 'boxplot')
    ),
    mainPanel(
      
      plotOutput(outputId = 'show_plot')
    )
  )
)

# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    v2 = input$var2
    
    
    library(ggplot2)
    
    d <- d %>% filter(bmi>input$bmi[1], bmi<input$bmi[2])
    d <- d %>% filter(region %in% input$Region)
    
    if(input$plot_choice == 'boxplot')
    {
      ggplot(d, aes(x = (d[[v1]]), color = as.factor(d[[v2]])))+
        geom_boxplot()+
        labs(x = v1, color = v2)
    }
    
    else
    {
      ggplot(d, aes(x = (d[[v1]]), color = as.factor(d[[v2]])))+
        geom_density()+
        labs(x = v1, color = v2)
    }
    
    
  })

}
# app
shinyApp(ui = ui, server = server)

Question 12

Follow this below example to include navbarPage to the app in Question 11. You can also write another app that make uses of selectInput, sliderInput, radioButtons, checkboxGroupInput and navbarPage.

Question 12 App

library(tidyverse)
library(shiny)

d = read_csv('insurance.csv')

variables_names = names(d)


ui <- navbarPage("Navigation",
                 tabPanel("Numeric and Categorical Plots",
                          titlePanel("Plotting Health Insurance Data"),
                          sidebarLayout(
                            sidebarPanel(
                              
                              selectInput(
                                inputId ="var1",
                                label = "Select a Numeric Variable",
                                choices = variables_names, selected = "charges"
                              ),
                              
                              selectInput(
                                inputId ="var2",
                                label = "Select a Categorical Variable",
                                choices = variables_names, selected = "sex"
                              ),
                              
                              sliderInput(inputId = "bmi",
                                          "Select BMI Range:",
                                          min = min(d$bmi, na.rm=TRUE),
                                          max = max(d$bmi, na.rm=TRUE),
                                          value= c(min(d$bmi),max(d$bmi))
                              ),
                              
                              checkboxGroupInput(inputId = "Region", label = "Select Region",
                                                 choices = names(table(d$region)), inline = TRUE,
                                                 selected = c("northeast","northwest")
                              ),
                              
                              radioButtons(inputId = "plot_choice", 
                                           label = h3("Select Plot:"),
                                           choices = c("Boxplot" = "boxplot",
                                                       "Density Plot" = "density"),
                                           selected = 'boxplot')
                            ),
                            mainPanel(
                              
                              plotOutput(outputId = 'show_plot')
                            )
                          )
                 ),
                 
                 tabPanel("Bar Plots",
                          titlePanel("Plotting Health Insurance Data"),
                          sidebarLayout(
                            sidebarPanel(
                              
                              selectInput(
                                inputId ="var3",
                                label = "Select a Categorical Variable",
                                choices = variables_names, selected = "children"
                              ),
                              
                              selectInput(
                                inputId ="var4",
                                label = "Select a Categorical Variable",
                                choices = variables_names, selected = "smoker"
                              ),
                              
                              sliderInput(inputId = "bmi1",
                                          "Select BMI Range:",
                                          min = min(d$bmi, na.rm=TRUE),
                                          max = max(d$bmi, na.rm=TRUE),
                                          value= c(min(d$bmi),max(d$bmi))
                              ),
                              
                              checkboxGroupInput(inputId = "Region1", label = "Select Region",
                                                 choices = names(table(d$region)), inline = TRUE,
                                                 selected = c("northeast","northwest"))
                            ),
                            
                            mainPanel(
                              
                              plotOutput(outputId = 'show_plot2')
                            )
                          )
                 )
)


# server is a function! 
server <- function(input, output) {
  
  
  output$show_plot <- renderPlot({
    
    v1 = input$var1
    v2 = input$var2
    
    
    library(ggplot2)
    
    d <- d %>% filter(bmi>input$bmi[1], bmi<input$bmi[2])
    d <- d %>% filter(region %in% input$Region)
    
    if(input$plot_choice == 'boxplot')
    {
      ggplot(d, aes(x = (d[[v1]]), color = as.factor(d[[v2]])))+
        geom_boxplot()+
        labs(x = v1, color = v2)
    }
    
    else
    {
      ggplot(d, aes(x = (d[[v1]]), color = as.factor(d[[v2]])))+
        geom_density()+
        labs(x = v1, color = v2)
    }
    
    
  })
  
  output$show_plot2 <- renderPlot({
    
    v3 = input$var3
    v4 = input$var4
    
    
    library(ggplot2)
    
    d <- d %>% filter(bmi>input$bmi1[1], bmi<input$bmi1[2])
    d <- d %>% filter(region %in% input$Region1)
    
    ggplot(d, aes(x = as.factor(d[[v3]]), fill = as.factor(d[[v4]]))) + 
      geom_bar() + labs(x = v3, fill = v4)
    
    
  })
  
  
}
# app
shinyApp(ui = ui, server = server)

Question 13

Present your shiny app in Question 12 on the final exam day.