Skip to content

Commit 1eac4f9

Browse files
committed
added check.structure function
1 parent d6df47f commit 1eac4f9

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

R/datplot_utility.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,38 @@ check.number <- function(value) {
216216
}
217217
return(result)
218218
}
219+
220+
#' @title Check the Structure to be compatible with datsteps()
221+
#'
222+
#' @description Checks if the object passed to datsteps() will work
223+
#'
224+
#' @param DAT_df A value to check
225+
#'
226+
#' @return TRUE if df can be processen, FALSE if not
227+
#'
228+
#' @export check.structure
229+
230+
check.structure <- function(DAT_df) {
231+
dat_df_structure <- c(NA, NA, NA, NA, NA)
232+
names(dat_df_structure) <- c("is.df", "is.id", "is.var", "is.minDAT", "is.maxDAT")
233+
234+
dat_df_structure["is.df"] <- is.data.frame(DAT_df)
235+
dat_df_structure["is.id"] <- is.character(DAT_df[,1])
236+
dat_df_structure["is.var"] <- is.factor(DAT_df[,2])
237+
dat_df_structure[c("is.minDAT", "is.maxDAT")] <- c(check.number(DAT_df[,3]), check.number(DAT_df[,4]))
238+
239+
if (dat_df_structure[1] == FALSE) {
240+
result <- FALSE
241+
stop("datsteps requires an object of class data.frame")
242+
} else { result <- TRUE }
243+
if (any(dat_df_structure[4:5] == FALSE)) {
244+
result <- FALSE
245+
stop("The 3rd or 4th columns of your data.frame are not numbers.")
246+
} else { result <- TRUE }
247+
if (any(dat_df_structure[2:3] == FALSE)) {
248+
warning("It is recommended to use character vector for the ID column and factor for the variable column.")
249+
}
250+
return(result)
251+
}
252+
253+

tests/testthat/test-datplot_utility.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,35 @@ test_that("sequence returns first and last values", {
3636
})
3737

3838

39+
40+
41+
42+
43+
testdf_wrong <- testdf
44+
testdf_wrong$min <- "TEST"
45+
testdf_wrong_two <- testdf
46+
testdf_wrong_two$max <- factor("börek")
47+
48+
3949
test_that("check.number returns true for numbers false for other", {
4050
expect_true(check.number(numeric(1)))
4151
expect_true(check.number(double(1)))
4252
expect_true(check.number(integer(1)))
4353
expect_false(check.number(character(1)))
4454
expect_false(check.number(factor(1)))
55+
expect_false(check.number(testdf_wrong[3,3]))
56+
expect_true(check.number(testdf_wrong[3,4]))
57+
expect_false(check.number(testdf_wrong_two[3,4]))
4558
})
4659

4760

4861

4962

63+
test_that("check.structure works", {
64+
expect_true(check.structure(testdf))
65+
expect_error(check.structure(testdf_wrong))
66+
expect_error(check.structure(testdf_wrong_two))
67+
})
68+
69+
70+

0 commit comments

Comments
 (0)