context("courseraeq functions testing")
library(courseraeq)
library(testthat)
setwd(system.file("data", package = "courseraeq"))
# read dataset
rawdata <- readr::read_delim("signif.txt.tsv", delim = "\t", progress = FALSE)
# sampling 100 random lines from rawdata to speed up testing
index <- sample(1:dim(rawdata)[1], 100)
data <- eq_clean_data(rawdata[index,])
### process_data
# eq_location_clean
test_that("Test eq_location_clean with different strings", {
expect_equal(eq_location_clean("ABCDEF"), "Abcdef")
expect_equal(eq_location_clean("ABCDEF ABCDEF"), "Abcdef Abcdef")
expect_equal(eq_location_clean("ABCDEF: ABCDEF"), "Abcdef")
expect_equal(eq_location_clean("ABCDEF: ABCDEF: ABCDEF"), "Abcdef: Abcdef")
})
# eq_clean_data
test_that("Test eq_clean_data to check class of columns", {
# # read dataset
# rawdata <- readr::read_delim("data/signif.txt.tsv", delim = "\t", progress = FALSE)
# # sampling 100 random lines from rawdata
# index <- sample(1:dim(rawdata)[1], 100)
# data <- eq_clean_data(rawdata[index,])
# check classes
expect_true(is.data.frame(data))
expect_true(lubridate::is.Date(data$DATE))
expect_true(is.numeric(data$LONGITUDE))
expect_true(is.numeric(data$LATITUDE))
expect_true(is.numeric(data$EQ_PRIMARY))
expect_true(is.numeric(data$DEATHS))
})
### map
# eq_create_label
test_that("Test eq_create_label creates a character vector", {
# check that the output is a character
expect_true(is.character(eq_create_label(data)))
# remove DEATHS column
data_test <- data %>%
dplyr::select(-DEATHS)
# check that even without one column the output is a character
expect_true(is.character(eq_create_label(data_test)))
})
# eq_map
test_that("Test eq_map creates a leaflet", {
expect_is(eq_map(data), "leaflet")
expect_is(eq_map(data, annot_col = "LATITUDE"), "leaflet")
})
### timeline plot
# geom_timeline
test_that("Test geom_timeline creates a ggplot object", {
p <- ggplot(data) +
geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS))
expect_is(p, "ggplot")
expect_is(p$layers[[1]]$geom, "GeomTimeLine")
})
# geom_timeline_label
test_that("Test geom_timeline creates a ggplot object", {
p <- ggplot(data) +
geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS)) +
geom_timeline_label(aes(x = DATE, y = COUNTRY, label = LOCATION_NAME, size = EQ_PRIMARY), n_max = 5)
expect_is(p, "ggplot")
expect_is(p$layers[[2]]$geom, "GeomTimeLineLabel")
p <- ggplot(data) +
geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS)) +
geom_timeline_label(aes(x = DATE, y = COUNTRY, label = LOCATION_NAME, size = EQ_PRIMARY), n_max = 8)
expect_is(p$layers[[2]]$geom, "GeomTimeLineLabel")
})
# geom_theme
test_that("Test geom_timeline creates a ggplot object", {
p <- ggplot(data) +
geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS)) +
theme_timeline()
expect_is(p, "ggplot")
expect_is(p$layers[[1]]$geom, "GeomTimeLine")
expect_equal(p$theme$line$colour, "black")
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.