tribe (version 0.1.7)

shield: Attributes protection

Description

The function shield is made to facilitate the propagation of attributes of an object obj through R operations.

Usage

shield(obj, at, propagate = "some", keep_also = NULL)

Arguments

obj

An object.

at

A named list, the attributes to be possibly added to obj.

propagate

character. The method to be applied, one of "all", "most", "some", "none", "many".

If propagate="some" (the default), the attributes of obj are kept unchanged (up to the value of keep_also).

If propagate="all" (not advised), the attributes of the returned object are exactly at (up to the value of keep_also).

If propagate="none" (not advised either), the attributes of the returned object are NULL (up to the value of keep_also).

If propagate="most", new attributes taken from at will be added to obj; however, attributes found in at that have the same name as attributes of obj are not considered.

keep_also

character. A vector of named attributes to be added to the final result.

Value

The object obj with possibly different attributes.

Examples

Run this code
# NOT RUN {
library(dplyr)
df <- data.frame(x = sample(10, 5, rep = TRUE),
                 y = sample(10, 5, rep = TRUE)) %>%
  at_mutate(example="yes",
            package="dplyr", 
            class = c("my_tbl", "data.frame"))
tribe(df)

# Attributes are lost when the object passes through dplyr verbs
df2 <- df %>% 
  mutate(z = 3)
tribe(df2)

# Most attributes are kept
df3 <- shield(df2, tribe(df), propagate = "most")
tribe(df3)

# To keep the class, use 'keep_also'
df4 <- shield(df2, tribe(df), propagate = "most", keep_also = "class")
tribe(df4)

# }

Run the code above in your browser using DataLab