library(Cairo) 
library(dslabs)
library(shiny)
library(dslabs)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
webshot::install_phantomjs()
## phantomjs has been installed to C:\Users\power\AppData\Roaming\PhantomJS

Loading Data

setwd("C:/Users/power/BST260/Project/BST260_ClassProject")
gapminder<-read.csv("5years.csv",header=T)

No written answer needed

ui = fluidPage(

  titlePanel("Life Expectancy Analysis"),
  
  # Create an app with 2 tabs
  tabsetPanel(
      
    # First tab: Line plot of life expactancy
    # Include dropdown menu of diseases to choose from 
    tabPanel("Life expectancy over time",
             sidebarLayout(
               sidebarPanel(
                 
                 p(),
                 
                 # Adding br() will add space between the text above and the dropdown
                 # menu below
                 br(),
                 
                 # Dropdown menu that allows the user to choose a disease
                 selectInput(inputId = "Country", label = "Select a country",
                             choices = as.list(levels(gapminder$Country)))
               ),
               
               mainPanel(plotOutput("scatterPlot1"))
             )
             ),
    
    # Second tab: Life expectancy and Fertility
    # Scatter plot that compares life expectancy and fertility
    # for each country over time with an animated bar for the years
    tabPanel("Life expectancy and fertility",
             sidebarLayout(
               sidebarPanel(
                 # Add some text and a couple of hyper links before the slider for year
                 p(),
                 
                 # Add some space between the text above and animated
                 # slider bar below
                 br(),
                 
                 # Input: year slider with basic animation
                 sliderInput("Year", "Year:",
                             min = 2014, max = 2018,
                             value = 2014, 
                             step = 1,
                             sep = "",       # keep years in year format and not 1,960 format
                             ticks = FALSE,  # don't show tick marks on slider bar
                             animate = TRUE) 
                 ),
               
                
               
               # Show a scatter plot for all countries
               mainPanel(plotOutput("scatterPlot2"))
             )
             )
    

  )
)

server = function(input, output) {

    # Lineplot for Life expectancy over time for tab 1
    
    output$scatterPlot1 = renderPlot({
                        #the_country<-input$country
                      USA<-subset(gapminder,Country=="United States")
                        gapminder%>%filter(Country %in% input$Country) %>% ggplot() +
                                              geom_line(aes(x = Year, y = Life.expectancy)) +
                                 geom_line(data = USA, mapping=aes(Year,Life.expectancy),color="red")+
                                                xlab("Year") +
                                                ylab("Life expectancy") +
                          ggtitle(sprintf("Life expectancy comparison with United States")) +
                            theme_bw()
                           #scale_x_continuous(limit=c(1960, 2020))
                        })
    
    # Scatterplot of fertility rate vs life expectancy for tab 2
    output$scatterPlot2 = renderPlot({
           # Filter year to be the input year from the slider
           gapminder %>% filter(Year %in% input$Year) %>%
             ggplot(aes(HScore, Life.expectancy, color = Continent)) +
             geom_point(alpha = 0.5) +
             xlab("Happiness Score") +
             ylab("Life expectancy") +
        
           # Set x and y axis limits to keep the same for each year
             #scale_x_continuous(breaks = seq(0, 9), limits = c(1, 8)) +
           #scale_y_continuous(breaks = seq(30, 85, 5), limits = c(30, 85)) +
           # Make the legend titles nicer
             scale_color_discrete(name = "Continent") +
             #scale_size_continuous(name = "Population in millions") +
        
           # Change the title of the plot for each year
           # Returns a character vector containing a formatted combination 
           # of text and variable values
           ggtitle(sprintf("Life expectancy vs. Happiness Score in %d", input$Year)) +
             theme_bw()})

}

shinyApp(ui = ui, server = server)
## 
## Listening on http://127.0.0.1:8676