library(learnr)
library(testwhat)
knitr::opts_chunk$set(echo = 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 scatter plots using the base R function plot().

head(penguins)

Exercise 1

Produce a plot of bill length against bill depth:


plot(bill_length_mm~bill_depth_mm, data=penguins)
ex() %>% {
  check_error(.)
  check_or(.,
           check_function(., "plot") %>%{
             check_arg(., "formula") %>% check_equal(.)
             check_arg(., "data")
           },
           override_solution_code(.,"plot(penguins$bill_depth_mm, penguins$bill_length_mm)") %>% check_function(., "plot") %>%{
             check_arg(.,"x")
             check_arg(.,"y")
           }
           )

  check_or(.,
           override_solution_code(.,"plot(penguins$bill_depth_mm, penguins$bill_length_mm)") %>% check_function(., "plot") %>%{
             check_arg(.,"x") %>% check_equal(.)
             check_arg(.,"y") %>% check_equal(.)
           },
           check_function(., "plot") %>%{
             check_arg(., "formula")
             check_arg(., "data")
           }
  )

}

Exercise 2

Produce a plot that will compare the bill length against the bill depth, with different colours indicating the three different species "Adelie","Gentoo","Chinstrap". Adelie should be coloured purple, Gentoo should be orange and Chinstrap should be red.


plot(bill_length_mm~bill_depth_mm, data=penguins, col=ifelse(penguins$species=="Adelie", "purple",ifelse(penguins$species=="Gentoo","orange","red")))
ex() %>% {
  check_error(.)
  check_function(., "ifelse") %>% check_result(.) %>% check_equal(.)
  check_or(.,
            check_function(., "plot") %>%{
              check_arg(., "formula") %>% check_equal(.)
              check_arg(., "data")
              check_arg(., "col") %>% check_equal(.)
            },
           override_solution_code(.,'plot(penguins$bill_depth_mm, penguins$bill_length_mm, col=ifelse(penguins$species=="Adelie", "purple",ifelse(penguins$species=="Gentoo","orange","red")))') %>% check_function(., "plot") %>%{
             check_arg(.,"x")
             check_arg(.,"y")
           }
  )

  check_or(.,
           override_solution_code(.,"plot(penguins$bill_depth_mm, penguins$bill_length_mm)") %>% check_function(., "plot") %>%{
             check_arg(.,"x") %>% check_equal(.)
             check_arg(.,"y") %>% check_equal(.)
             check_arg(., "col")
           },
           check_function(., "plot") %>%{
             check_arg(., "formula")
             check_arg(., "data")
           }
  )

}


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