| iface | R Documentation |
An iface specification defines the expected structure of a dataframe, in
terms of the column names, column types, grouping structure and uniqueness
constraints that the dataframe must conform to. A dataframe can be tested
for conformance to an iface specification using ivalidate().
iface(..., .groups = NULL, .default = NULL)
... |
The specification of the interface (see details), or an unnamed
|
.groups |
either |
.default |
a default value to supply if there is nothing given in a
function parameter using the |
An iface specification is designed to be used to define the type of a
parameter in a function. This is done by using the iface specification as
the default value of the parameter in the function definition. The definition
can then be validated at runtime by a call to ivalidate() inside the
function.
When developing a function output an iface specification may also be used
in ireturn() to enforce that the output of a function is correct.
iface definitions can be printed and included in roxygen2 documentation
and help us to document input dataframe parameters and dataframe return
values in a standardised way by using the @iparam roxygen2 tag.
iface specifications are defined in the form of a named list of formulae with the
structure column_name = type ~ "documentation".
type can be one of anything, character, complete, date, default, double, enum, factor, finite, group_unique, in_range, integer, logical, not_missing, numeric, of_type, positive_double, positive_integer, proportion, unique_id (e.g. enum(level1,level2,...),
in_range(min,max)) or alternatively anything that resolves to a function e.g.
as.ordered.
If type is a function name, then the function must take a single vector
parameter and return a single vector of the same size. The function must also
return a zero length vector of an appropriate type if passed NULL.
type can also be a concatenation of rules separated by +, e.g.
integer + group_unique for an integer that is unique within a group.
the definition of an interface as a iface object
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.