Exercise 9.1

require("survival") 
## Loading required package: survival
data(lung)
tab <- xtabs(~ status + sex, lung)
vcd::mosaic(tab, shade = TRUE, legend = TRUE)

library(ggplot2)
require("ggridges")
## Loading required package: ggridges
ggplot(lung) +
  aes(x = age, y = factor(status), fill = factor(status)) +
  geom_density_ridges() + 
  theme_ridges() +
  labs("Age by death/live") +
  theme(legend.position = "none")
## Picking joint bandwidth of 3.17

require("ggcorrplot")
## Loading required package: ggcorrplot
r <- cor(lung[, 7:10], use = "complete.obs")
ggcorrplot(r, hc.order = TRUE, type = "lower", lab = TRUE)

Exercice 9.2

library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                   min = 1,
                   max = 50,
                   value = 30)
        ),
  # Show a plot of the generated distribution
  mainPanel(
         plotOutput("distPlot")
       )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  output$distPlot <- renderPlot({
  # generate bins based on input$bins from ui.R
  my_df <- data.frame(x = faithful[, 2])

  # draw the histogram with the specified number of bins
  ggplot(my_df) +
    aes(x = x) +
    geom_histogram(aes(y = ..density..), colour = "lightgrey", bins = input$bins) + 
    geom_density(colour = "red", adjust = 2) +
    stat_function(fun = dnorm, args = c(mean = mean(my_df$x), sd = sd(my_df$x))) + 
    xlab("x") +
  ggtitle("Distribution")

 })
}

shinyApp(ui = ui, server = server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents