| %to% | R Documentation |
%to% allows a single object to be passed through multiple commands, and for
the results to be returned in a single dataframe.
obj %to% blocks
obj |
Object to be passed to multiple instructions |
blocks |
formula-like instructions wrapped in a pair of braces |
Dataframe with column names being the formula LHS values, and a single row containing the results of each column's respective instructions.
The syntax to use for this purpose is to have the object on the LHS of the
%to% operator, and wrap all instructions in a braces pair that effectively
acts as multiple lines of regular R code. However, each line needs to be
named, in a formula syntax fashion:
obj %to% {
inst1 ~ inst1expressions(.)
inst2 ~ inst2expressions(.)
}
The right side of the formula is to be specified similar to how instructions
in purrr:map are, that is, by the ., .x syntax.
If multiple lines are needed for each instruction, one can simply wrap them in yet another braces pair.
Other result assemblers:
%$>%(),
%->%(),
%<-%(),
conserve(),
control()
# returning multiple analysis on same object
testvec <- c(1, 2, 3, 7)
testvec %to% {
mean ~ mean(.)
sd ~ sd(.)
}
# use nested braces for longer expressions
testvec %to% {
mean2 ~ {
x <- . + 3
mean(x * .)
}
sd ~ sd(.)
}
# vector results are stored as lists in the dataframe
testvec %to% {
itself ~ .
itsmean ~ mean(.)
}
testvec %to% {
itself ~ .
itsextra ~ c(2, .)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.