test_df = tibble::tibble(
grp = c(rep("a",10),rep("b",10)),
col1 = c(1:10,1:10)
) %>% dplyr::group_by(grp)
my_iface = iface(
col1 = integer + group_unique ~ "an integer column",
.default = test_df
)
print(my_iface)
# the function x defines a formal `df` with default value of `my_iface`
# this default value is used to validate the structure of the user supplied
# value when the function is called.
x = function(df = my_iface, ...) {
df = ivalidate(df,...)
return(df)
}
# this works
x(tibble::tibble(col1 = c(1,2,3)))
# this fails as x is of the wrong type
try(x(tibble::tibble(col1 = c("a","b","c"))))
# this fails as x has duplicates
try(x(tibble::tibble(col1 = c(1,2,3,3))))
# this gives the default value
x()
my_iface2 = iface(
first_col = numeric ~ "column order example",
my_iface,
last_col = character ~ "another col", .groups = ~ first_col + col1
)
print(my_iface2)
my_iface_3 = iface(
col1 = integer + group_unique ~ "an integer column",
.default = test_df_2
)
x = function(d = my_iface_3) {ivalidate(d)}
# Doesn't work as test_df_2 hasn't been defined
try(x())
test_df_2 = tibble::tibble(
grp = c(rep("a",10),rep("b",10)),
col1 = c(1:10,1:10)
) %>% dplyr::group_by(grp)
# now it works as has been defined
x()
# it still works as default has been cached.
rm(test_df_2)
x()
Run the code above in your browser using DataLab