x <- data.frame(
"analyte" = c("A", "B"),
"tmp" = rep(0L, 2),
"unit" = c("x", "y")
)
str(x)
ac <- eCerto::assert_col
str(ac(df = x, name = "analyte", pos = 1, type = "factor"))
str(ac(df = x, name = "Analyte", pos = 3, type = "character"))
str(ac(df = x, name = " Analyte", pos = 2, type = "factor"))
str(ac(df = x, name = "Analyte", pos = 2, type = "factor", fuzzy_name = FALSE))
str(ac(df = x, name = "test", type = "factor", default_value = "test"))
# this will lead to NAs in column unit because the conversion does not lead to an error
# hence the default value is not used
str(ac(df = x, name = "unit", type = "numeric", default_value = 10))
# this will lead to the specified default data in column unit because the
# conversion attempt does lead to an error
str(ac(df = x, name = "unit", type = "Date"))
str(ac(df = data.frame("test" = "2022-03-31"), name = "test", type = "Date"))
# show type and class of internal default values
x <- data.frame(
"character" = "", "integer" = 0L, "numeric" = 0, "factor" = factor(NA),
"logical" = NA, "date" = Sys.Date(), NA
)
sapply(1:ncol(x), function(i) {
typeof(x[, i])
})
sapply(1:ncol(x), function(i) {
class(x[, i])
})
Run the code above in your browser using DataLab