library(learnr)
library(testwhat)
knitr::opts_chunk$set(echo = FALSE, message=FALSE)
tutorial_options(exercise.timelimit = 60, exercise.checker=testwhat::testwhat_learnr)

require(tidyverse)
penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv")
penguins<- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-28/penguins.csv")

We are going to use the penguins data to produce a variety of box plots using ggplot.

head(penguins)

Exercise 1

Produce a box plot showing body mass:


boxplot(penguins$body_mass_g)
ex() %>% {
  check_error(.)
  check_function(., "boxplot") %>% check_arg(., "x") %>% check_equal(.)
}

Exercise 2

Produce a box plot that will compare the distribution of body weight by year:


boxplot(body_mass_g~year, data=penguins)
ex() %>% {
  check_error(.)
  check_function(., "boxplot") %>% {
    check_arg(., "formula", arg_not_specified_msg = "The easiest way to do this is to specify a formula") %>% check_equal(.)
    check_arg(., "data", arg_not_specified_msg="Specifying the data argument when using a formula will make your code more readable") 
  }
}

Exercise 3

Produce a boxplot that will describe the distribution of body mass for each species within each year. The observations within a species should appear next to one another.


boxplot(body_mass_g~year+species, data=penguins)
ex() %>% {
  check_error(.)
  check_function(., "boxplot") %>% {
    check_arg(., "formula", arg_not_specified_msg = "The easiest way to do this is to specify a formula") %>% check_equal(., eval=FALSE, eq_fun=function(x, y){str_replace_all(x, "[[:blank:]]", "")==str_replace_all(y, "[[:blank:]]", "")})
    check_arg(., "data", arg_not_specified_msg="Specifying the data argument when using a formula will make your code more readable") 
  }
}


kate-pyper/MM916ProgrammingExercises documentation built on Oct. 15, 2020, 10:40 p.m.