tests/testthat/helper_support_functions.R

#' return a data set used for testing
#'
#' @return data frame
#'
#' @examples
#' df <- create_test_data()
create_test_data <- function()
{
  # library(palmerpenguins)
  return(palmerpenguins::penguins)
}

#' verify a PCA plot generated by KMeans with centers=n_clusters
#'
#' @param p PCA plot
#' @param n_cluster number of clusters
#'
#' @return raise exception if expect_equal fails
#'
#' @examples
#' library(palmerpenguins)
#' df <- penguins
#' results <- explore_KMeans_clustering(df, c(2, 3))
#' verify_KMeans_plot(results[[1]], 2)
verify_KMeans_plot <- function(p, n_cluster){
  expect_equal(p$labels$title, paste0("PCA Plot - KMeans with", n_cluster," centers"))
  expect_equal(nlevels(p$data$groups), n_cluster)
}


#' verify PCA plot generated by DBSCAN
#'
#' @param p ggplot PCA plot
#' @param eps numeric size of the epsilon neighborhood.
#' @param minPts numeric number of minimum points in the eps region (for core points)
#' @param n_cluster numeric number of clusters
#'
#' @return
#'
#' @examples
#' library(palmerpenguins)
#' df <- penguins
#' results <- explore_DBSCAN_clustering(df, c(2, 3))
#' verify_DBSCAN_plot(results[[1]], 2)
verify_DBSCAN_plot <- function(p, eps, minPts){
  expect_equal(p$labels$title, paste0("PCA Plot - DBSCAN with eps=", eps,", minPts=",minPts))
}
UBC-MDS/datascience.eda.R documentation built on March 24, 2021, 2:22 a.m.