knitr::opts_chunk$set(collapse = TRUE, comment = "#>") library(dplyr) library(tribe) print.list <- function(x, ...) str(x)
The tribe package provides verbs for easy manipulation of attributes. These verbs are:
at_mutate
to create or modify attributes; at_select
to select attributes (and NULL
ify the others);at_rename
to rename attributes; at_slice
to extract attributes. The function tribe
is a convenient synonym of attributes
,
with the slight difference that it always returns a named list.
df <- data.frame(x = 1:2, y = 2:3) %>% at_mutate(example="yes", package="dplyr") tribe(df)
Use at_slice
to extract attribute values:
at_slice(df, names)
Each verb has its standard evaluation version; for instance at_slice_
is the standard evaluation version of at_slice
:
at_slice_(df, "class") at_slice_(df, ~ package)
Similarly at_mutate_
is the standard evaluation version of at_mutate
:
df <- df %>% at_mutate_(package = ~ NULL, # deletes the attribute called 'package' example = ~ "no") tribe(df)
The tribe package builds on the magrittr package and brings a new pipe %@>%
similar to the pipe %>%
that enables propagation of attributes.
df <- data.frame(x = 1:2, y = 2:3) %>% at_mutate(example="yes", package="tribe", class = c("my_tbl", "data.frame"))
Attributes just created are often lost when the object passes through dplyr verbs, for instance:
tribe(df %>% mutate(z=3))
With the new pipe %@>%
, most attributes propagate:
tribe(df %@>% mutate(z=3))
Behind the scene, the shield
function operates:
# 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)
One can create a new pipe to adjust attributes propagation settings:
"%newpipe>%" <- make_pipe(propagate="none", keep_also = "example") tribe(df %newpipe>% mutate(z=3))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.