Skip to content

Commit

Permalink
Merge pull request #102 from thisisnic/ncrane-#101
Browse files Browse the repository at this point in the history
Fix #101 - throw an error if the x argument supplied to vis_dat is not a data.frame
  • Loading branch information
njtierney committed Nov 28, 2018
2 parents 1f28b70 + 5620c8a commit fca499d
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Authors@R: c(
comment = "Mara Averick reviewed the package for rOpenSci, see
https://github.com/ropensci/onboarding/issues/87"),
person("Stuart", "Lee", role = c("ctb")),
person("Earo", "Wang", role = c("ctb"))
person("Earo", "Wang", role = c("ctb")),
person("Nic", "Crane", role = c("ctb"))
)
Description: Create preliminary exploratory data visualisations of an entire
dataset to identify problems or unexpected features using 'ggplot2'.
Expand Down
25 changes: 25 additions & 0 deletions R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,28 @@ all_numeric <- function(x, ...){
all(as.logical(lapply(x, is.numeric)))
}
# Can I capture moving from a value to NA, or, from NA to another value?


#' Test if input is a data.frame
#'
#' @param x object
#'
#' @return an error if input (x) is not a data.frame
#'
#' @examples
#' \dontrun{
#' # success
#' test_if_dataframe(airquality)
#' #fail
#' test_if_dataframe(AirPassengers)
#' }
#'
test_if_dataframe <- function(x){
if (!inherits(x, "data.frame")) {
stop("vis_dat requires a data.frame but the object I see has class/es: ",
paste(class(x), collapse = ", "), call.=FALSE)
}
}



6 changes: 6 additions & 0 deletions R/vis-compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ vis_compare <- function(df1,
# make a TRUE/FALSE matrix of the data.
# Tells us whether it is the same (true) as the other dataset, or not (false)

# throw error if df1 not data.frame
test_if_dataframe(df1)

# throw error if df2 not data.frame
test_if_dataframe(df2)

if (!identical(dim(df1), dim(df2))) {
stop("vis_compare requires identical dimensions of df1 and df2")
}
Expand Down
3 changes: 3 additions & 0 deletions R/vis-cor.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ vis_cor <- function(data,
na_action = "pairwise.complete.obs",
...){

# throw error if data not data.frame
test_if_dataframe(data)

if (!all_numeric(data)) {
stop("data input can only contain numeric values, please subset the data to the numeric values you would like.")
} else {
Expand Down
3 changes: 3 additions & 0 deletions R/vis-dat.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ vis_dat <- function(x,
warn_large_data = TRUE,
large_data_size = 900000) {

# throw error if x not data.frame
test_if_dataframe(x)

# add warning for large data
if (ncol(x) * nrow(x) > large_data_size && warn_large_data){
stop("Data exceeds recommended size for visualisation, please consider
Expand Down
3 changes: 3 additions & 0 deletions R/vis-expect.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
#'
vis_expect <- function(data, expectation, show_perc = TRUE){

# throw error if data not data.frame
test_if_dataframe(data)

data_expect <- expect_frame(data, expectation)

# calculate the overall % expecations to display in legend -------------------
Expand Down
3 changes: 3 additions & 0 deletions R/vis-guess.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#' @export
vis_guess <- function(x, palette = "default"){

# throw error if x not data.frame
test_if_dataframe(x)

# x = messy_df
# suppress warnings here as this is just a note about combining classes
d <- suppressWarnings(vis_gather_(x)) %>%
Expand Down
3 changes: 3 additions & 0 deletions R/vis-miss.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ vis_miss <- function(x,
large_data_size = 900000,
warn_large_data = TRUE){

# throw error if x not data.frame
test_if_dataframe(x)

# add warning for large data
if (ncol(x) * nrow(x) > large_data_size && warn_large_data) {
stop("Data exceeds recommended size for visualisation, please consider
Expand Down
26 changes: 26 additions & 0 deletions man/test_if_dataframe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test-vis-compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ test_that("vis_compare will not accept two dataframes of differing dims",{
expect_error(
vis_compare(iris, iris_add))
})

test_that("vis_compare fails when an object of the wrong class is provided", {
testthat::expect_error(vis_compare(iris, AirPassengers))
testthat::expect_error(vis_compare(AirPassengers, iris))
testthat::expect_error(vis_compare(AirPassengers, AirPassengers))
})
4 changes: 4 additions & 0 deletions tests/testthat/test-vis-cor.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ test_that("vis_cor creates the right plot",{
test_that("vis_cor sends an error when used with the wrong data",{
testthat::expect_error(vis_cor(iris))
})

test_that("vis_cor fails when an object of the wrong class is provided", {
testthat::expect_error(vis_cor(AirPassengers))
})
4 changes: 4 additions & 0 deletions tests/testthat/test-vis-dat.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ test_that("vis_dat fails when the wrong palette is provided",{
cat(sprintf("FreeType version: %s\n", ver))
expect_error(vis_dat(typical_data, palette = "wat"))
})

test_that("vis_dat fails when an object of the wrong class is provided", {
testthat::expect_error(vis_dat(AirPassengers))
})
3 changes: 3 additions & 0 deletions tests/testthat/test-vis-expect.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ test_that("vis_expect creates the right plot",{
vis_expect_plot_show_perc_true)
})

test_that("vis_expect fails when an object of the wrong class is provided", {
testthat::expect_error(vis_expect(AirPassengers, ~.x < 20))
})
4 changes: 4 additions & 0 deletions tests/testthat/test-vis-guess.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ test_that("vis_guess creates the right plot",{
test_that("vis_guess fails when the wrong palette is provided",{
testthat::expect_error(vis_guess(typical_data, palette = "wat"))
})

test_that("vis_guess fails when an object of the wrong class is provided", {
testthat::expect_error(vis_guess(AirPassengers))
})
4 changes: 4 additions & 0 deletions tests/testthat/test-vis-miss.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ test_that("vis_miss creates the right plot",{
vdiffr::expect_doppelganger("vis_miss show percent in columns", vis_miss_plot_show_perc_col)
vdiffr::expect_doppelganger("vis_miss no show percent in columns", vis_miss_plot_show_perc_col_t)
})

test_that("vis_miss fails when an object of the wrong class is provided", {
testthat::expect_error(vis_miss(AirPassengers))
})

0 comments on commit fca499d

Please sign in to comment.