Quiz 19 Instructions

Please complete the following questions and submit a file named app.R to Gradescope for autograding. [Please note this update! For the Shiny app quizzes, submit only the “app.R” only.]

Remember:

  • Do not use global paths in you script. Instead, use setwd() interactively in the console, but do not forget to remove or comment out this part of the code before you submit. The directory structure of your machine is not the same as the one on Gradescope’s virtual machines.
  • Do not destroy or overwrite any variables in your program. I check them only after I have run your entire program from start to finish.
  • Check to make sure you do not have any syntax errors. Code that doesn’t run will get a very bad grade.
  • Make sure to name your submission app.R

Tip: before submitting, it might help to clear all the objects from your workspace, and then source your file before you submit it. This will often uncover bugs.

library(tidyverse) ### We'll use some functions from stringr.

Summary Statistics Shiny App Design

Please create a Shiny app that calculate the summary statistics of any input vector, which may contain missing values. Please use the following functions (from Quiz 8, slightly modified) for the design. When you design the app,

  1. [1 pt] Please use textInput() for the vector input (inputID: vec_Input). Please set the initial value as “1, 2, 3, 4, 5”.
  2. [1 pt] Please add an actionButton (inputID: calculate).
  3. [3 pt] Please create an output using verbatimTextOutput() with the outputID: summary_statistic in the mainPanel of the page.
squared_sum <- function(x){
  return(sum(x^2, na.rm=TRUE))
}


summary_stats <- function(x){
  if (!is.numeric(x)){
    stop("Input vector must be numeric!")
  }
  
  stats <- list(
    n = length(x),
    mean = mean(x, na.rm = TRUE),
    median = median(x, na.rm = TRUE),
    sd = sd(x, na.rm = TRUE),
    min = min(x, na.rm = TRUE),
    max = max(x, na.rm = TRUE),
    Q1 = unname(quantile(x, 0.25, na.rm = TRUE)),
    Q3 = unname(quantile(x, 0.75, na.rm = TRUE)),
    sq.sum = squared_sum(x),
    num.missing = sum(is.na(x))
  )
  return(stats)
}



as.numeric(unlist(strsplit(input$vec_Input, ",")))