# using base R style
x <- make_safeframe(cars[1:50, ],
mph = "speed",
distance = "dist"
)
x
## check tagged variables
tags(x)
## robust renaming
names(x)[1] <- "identifier"
x
## example of dropping tags by mistake - default: warning
x[, 2]
## to silence warnings when tags are dropped
lost_tags_action("none")
x[, 2]
## to trigger errors when tags are dropped
# lost_tags_action("error")
# x[, 1]
## reset default behaviour
lost_tags_action()
# using tidyverse style
## example of creating a safeframe, adding a new variable, and adding a tag
## for it
if (require(dplyr) && require(magrittr)) {
x <- cars %>%
tibble() %>%
make_safeframe(
mph = "speed",
distance = "dist"
) %>%
mutate(result = if_else(speed > 50, "fast", "slow")) %>%
set_tags(ticket = "result")
head(x)
## extract tagged variables
x %>%
select(has_tag(c("ticket")))
## Retrieve all tags
x %>%
tags()
## Select based on variable name
x %>%
select(starts_with("speed"))
}
Run the code above in your browser using DataLab