Code for HM4

ui.R

###~~~ Homework 4 for Sirui and Michelle C ~~~###


### Define the UI

fluidPage(
  titlePanel("Homework 4 by Sirui and Michelle C"),
  sidebarLayout(
    sidebarPanel(
      
      #Dropdown menu for x (brainwt and bodywt) 
      selectInput("valx", "Weigh:",
                  choices = c("brainwt", "bodywt")),
      
      #Dropdown menu for y (sleep_total, sleep_rem, awake)
      selectInput("valy", "Sleep:",
                  choices = c("total" = "sleep_total", 
                              "REM" = "sleep_rem", 
                              "awake" = "awake")),
      
      #Dropdown menu for color (none, vore, conservation)
      selectInput("plt", "Colour:",
                  choices = c("conservation", "order", "vore")),
      
      selectInput("seyes", "Standard Error",
                  choices = c("yes", "no")),
      
      tags$div(class="header", checked=NA,
               tags$a(href="", "Code for the Assignment 4"))
               
      
    ),
    mainPanel(plotOutput("msleep.plot"))
  ))

server.R

### Load the libraries and data

library(shiny)
library(tidyverse)
data("msleep")
ms <- msleep
ms <- select(ms, -c("genus", "sleep_cycle"))
ms <- na.omit(ms)
ms$conservation <- factor(ms$conservation,
                          levels = c("domesticated", "lc", "nt", "vu", "en", "cd"),
                          ordered = T)

### Define the server 

function(input, output){
  
  output$msleep.plot <- renderPlot({
    
    cl <- input$plt; if(cl=="none") cl<- 1
    
    ggplot(ms, aes_string(x=input$valx, y=input$valy, colour=cl)) + 
      geom_point() +
      scale_x_continuous(trans = 'log10', 
                         labels = scales::trans_format("log10", 
                                                       scales::math_format(10^.x))) +
      stat_smooth(method = "lm", 
                  se = ifelse(input$seyes=="yes",TRUE,FALSE))
    
  })
  
}

Dataset: msleep

## Warning: package 'tidyverse' was built under R version 3.5.2
## -- Attaching packages ----------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.1.0     √ purrr   0.2.5
## √ tibble  1.4.2     √ dplyr   0.7.8
## √ tidyr   0.8.2     √ stringr 1.3.1
## √ readr   1.3.1     √ forcats 0.3.0
## Warning: package 'ggplot2' was built under R version 3.5.2
## Warning: package 'tibble' was built under R version 3.5.2
## Warning: package 'tidyr' was built under R version 3.5.2
## Warning: package 'readr' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.2
## Warning: package 'dplyr' was built under R version 3.5.2
## Warning: package 'stringr' was built under R version 3.5.2
## Warning: package 'forcats' was built under R version 3.5.2
## -- Conflicts -------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## # A tibble: 83 x 11
##    name  genus vore  order conservation sleep_total sleep_rem sleep_cycle
##    <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl>
##  1 Chee~ Acin~ carni Carn~ lc                  12.1      NA        NA    
##  2 Owl ~ Aotus omni  Prim~ <NA>                17         1.8      NA    
##  3 Moun~ Aplo~ herbi Rode~ nt                  14.4       2.4      NA    
##  4 Grea~ Blar~ omni  Sori~ lc                  14.9       2.3       0.133
##  5 Cow   Bos   herbi Arti~ domesticated         4         0.7       0.667
##  6 Thre~ Brad~ herbi Pilo~ <NA>                14.4       2.2       0.767
##  7 Nort~ Call~ carni Carn~ vu                   8.7       1.4       0.383
##  8 Vesp~ Calo~ <NA>  Rode~ <NA>                 7        NA        NA    
##  9 Dog   Canis carni Carn~ domesticated        10.1       2.9       0.333
## 10 Roe ~ Capr~ herbi Arti~ lc                   3        NA        NA    
## # ... with 73 more rows, and 3 more variables: awake <dbl>, brainwt <dbl>,
## #   bodywt <dbl>
##      name              genus               vore          
##  Length:83          Length:83          Length:83         
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##                                                          
##                                                          
##                                                          
##                                                          
##     order           conservation        sleep_total      sleep_rem    
##  Length:83          Length:83          Min.   : 1.90   Min.   :0.100  
##  Class :character   Class :character   1st Qu.: 7.85   1st Qu.:0.900  
##  Mode  :character   Mode  :character   Median :10.10   Median :1.500  
##                                        Mean   :10.43   Mean   :1.875  
##                                        3rd Qu.:13.75   3rd Qu.:2.400  
##                                        Max.   :19.90   Max.   :6.600  
##                                                        NA's   :22     
##   sleep_cycle         awake          brainwt            bodywt        
##  Min.   :0.1167   Min.   : 4.10   Min.   :0.00014   Min.   :   0.005  
##  1st Qu.:0.1833   1st Qu.:10.25   1st Qu.:0.00290   1st Qu.:   0.174  
##  Median :0.3333   Median :13.90   Median :0.01240   Median :   1.670  
##  Mean   :0.4396   Mean   :13.57   Mean   :0.28158   Mean   : 166.136  
##  3rd Qu.:0.5792   3rd Qu.:16.15   3rd Qu.:0.12550   3rd Qu.:  41.750  
##  Max.   :1.5000   Max.   :22.10   Max.   :5.71200   Max.   :6654.000  
##  NA's   :51                       NA's   :27