Attribution statement: (choose only one and delete the rest)

# 1. I did this homework by myself, with help from the book and the professor.

At a high level, we want to create a scatter plot, where the user can select what is the x axis and the y axis of the scatter plot. We will use the same dataset as used in our first ggplot homework.

The size and color will be represented by the year attribute, and the user will be able to choose one of the following (for the x and the y axis): new_sp_m014 new_sp_f014 new_sp_m65 new_sp_f65

Read in the same dataset we used for the ggplot homework

The file is: “https://intro-datascience.s3.us-east-2.amazonaws.com/who.csv” and store it in the tb dataframe

 # Read in the dataset
tb <- read.csv("https://intro-datascience.s3.us-east-2.amazonaws.com/who.csv")

Clean up the dataset, just like we did in the ggplot homework.

First, remove na’s from iso2

# Remove NA's from iso2
tb <- tb[!is.na(tb$iso2), ]

Now create the dataframe ‘tbCan’, which is the iso2 for canada (CA)

# Create the dataframe 'tbCan' for Canada (iso2 == "CA")
tbCan <- tb[tb$iso2 == "CA", ]

We will need the imputeTS package (only install if needed)

#install.packages('imputeTS')
library(imputeTS)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

Now we can use ’na_interpolation for new_sp_m014

# Use na_interpolation for new_sp_m014
tbCan$new_sp_m014 <- na_interpolation(tbCan$new_sp_m014)

Create the User Interface of our shiny app

library(shiny)

library(ggplot2)
# Define UI for application (called ui)
# Use a 'fluidPage' using the sidebarLayout, and your sidePanel and mainPanel

ui <- fluidPage(

    # Application title
    titlePanel("Exploring Data"),
    
    # Using sidebarLayout 
    sidebarLayout(

        # Define the sidebarPanel, which has two dropdown boxes (use 'selectInput'), 
        # one for the x-axis and the other is the y-axis for the scatter plot
        sidebarPanel(
            selectInput("xvar", "X-axis variable:",
                        choices = c("new_sp_m014", "new_sp_f014", "new_sp_m65", "new_sp_f65")),
            selectInput("yvar", "Y-axis variable:",
                        choices = c("new_sp_m014", "new_sp_f014", "new_sp_m65", "new_sp_f65"))
                     ),
        
        # Create the mainPanel to show the scatter plot
        mainPanel(
            plotOutput("scatterPlot")
          
        )
     )
)

Now let’s define the server part of the Shiny app

server <- function(input, output)
{
# output the plot with renderPlot()
output$scatterPlot <- 

renderPlot(
  
      {
# inside the renderPlot() function you would want to: Use the input from the 2 selectInput() created in the UI portion to filter data from the tbCan dataframe and make the scatteplot with ggplot2. The scatterplot will be automatically updated whenever there is change in one of the selectInput()

            ggplot(tbCan, aes_string(x = input$xvar, y = input$yvar, color = "year", size = "year")) +
                geom_point() +
                theme_minimal() +
                labs(x = input$xvar, y = input$yvar, color = "Year", size = "Year") +
                scale_color_gradient(low = "blue", high = "red")
      }
   )
}

Now run the shiny App

# Run the application 
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents