I would like to create a verif()
function that accepts a single argument called val
. This argument can only accept a data frame or a list.
This function should work with the %>%
pipe.
This function should return a vector of characters that correspond to the name of the :
- data frame OR
- of each element of the list passed before the
%>%
.
Here's my attempt:
verif <- function(val) {
if(is.data.frame(val)) {
return(deparse(substitute(val), backtick = TRUE))
} else if(is.list(val)) {
return(sapply(val, function(x) deparse(substitute(x), backtick = TRUE)))
} else {
return("Argument must be a data frame or a list.")
}
}
What I would like:
iris %>% verif() # return "iris"
list(iris, cars) %>% verif() # return c("iris","cars")
Many thanks