Description Usage Arguments Details Value Note See Also Examples
Usual operators to subset and manipulate an object of class
Container
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ## S4 method for signature 'Container,missing,missing,missing'
x[i, j, ..., drop = TRUE]
## S4 method for signature 'Container,expression,expression,missing'
x[i, j, ..., drop = TRUE]
## S4 method for signature 'Container,expression,missing,missing'
x[i, j, ..., drop = TRUE]
## S4 method for signature 'Container,missing,expression,missing'
x[i, j, ..., drop = TRUE]
## S4 method for signature 'Container,ANY,ANY,ANY'
x[i, j, ..., drop = TRUE]
## S4 method for signature 'Container,ANY,missing'
x[[i, j, ...]]
## S4 method for signature 'Container,ANY,ANY'
x[[i, j, ...]]
## S4 method for signature 'Container'
x$name
## Convenient wrapper for expressions
e(expr)
|
x |
an object of class |
i, j |
elements to subset on and/or extract. These can be:
|
... |
optional arguments passed to |
drop |
a logical. If |
name |
a column |
expr |
an |
To modify subsets of the table
stored in an instance of class
Container
, use either data.table's
:=()
operator or set() function.
Modifications are done by reference.
Package cargo needs to know you are passing (unevaluated)
data.table expressions to the operator [
.
To do so, wrap arguments i
and j
with function e()
. See examples below.
All operators works on the table
slot.
They are not endomorphism and do not return objects of class
Container
. Instead, a subset of slot table
is
returned in various ways that depends on the operator and on the arguments'
signature.
Operator [
always returns a
data.table
, sometimes invisibly. This
depends on what is passed to argument j
.
Operators [[
and $
both return a vector with a class that matches the extracted column's class.
This note is irrelevent for most users, but must still appear somewhere.
The data.table
class relies on the
non-standard evaluation mechanism of R: expressions passed to functions
arguments are not evaluated upon a function call. Instead, R evaluates them
only when it is forced to.
Unfortunately, package methods' dispatch mechanism breaks this feature, because it needs to know the arguments' signature to dispatch a method on it. This is very problematic for what cargo is trying to achieve.
The workaround is to protect expressions from early evaluation. R
only needs to know they are proper expressions, not their result. Hence, the
solution is to wrap expressions passed to i
and j
arguments, so that they
can be evaluated within the frame of table
later. The function e()
is a convenient shortcut doing just that.
Other Container:
Container-accessors
,
Container-class
,
Container-methods
,
Container-validators
,
is_container()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ## Construct an object of class Container from a data.table and a Schema.
myTable <- data.table::data.table(
field1 = c("s1", "s2", "s3"),
field2 = c(1L, 2L, 3L),
field3 = c(47.13, 48.72, 53.32),
field4 = c(-122.56, -79.12, -114.67)
)
mySchema <- Schema(
inputs = c("field1", "field2", "field3", "field4"),
prototypes = list(
Prototype(character()),
Prototype(integer()),
Prototype(numeric()),
Prototype(numeric()))
)
myCont <- Container(myTable, mySchema)
## Say we need to compute conditional sums of values in numeric columns.
myTable[field2 > 0L, base::colSums(.SD),
.SDcols = c("field2", "field3", "field4")]
## The cargo way would be to write
myCont[cargo::e(field1 > 0L), cargo::e(base::colSums(.SD)),
.SDcols = c("field2", "field3", "field4")]
## The latter is equivalent to writing this expression.
cargo::table(myCont)[field1 > 0L, base::colSums(.SD),
.SDcols = c("field2", "field3", "field4")]
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.