The [] and [[]] operators for safeframe objects behaves like for
regular data.frame or tibble, but check that tagged variables are not
lost, and takes the appropriate action if this is the case (warning, error,
or ignore, depending on the general option set via lost_tags_action()) .
Usage
# S3 method for class 'safeframe'
x[i, j, drop = FALSE]
# S3 method for class 'safeframe'
x[i, j] <- value
# S3 method for class 'safeframe'
x[[i, j]] <- value
# S3 method for class 'safeframe'
x$name <- valueArguments
- x
a
safeframeobject- i
a vector of
integerorlogicalto subset the rows of thesafeframe- j
a vector of
character,integer, orlogicalto subset the columns of thesafeframe- drop
a
logicalindicating if, when a single column is selected, thedata.frameclass should be dropped to return a simple vector, in which case thesafeframeclass is lost as well; defaults toFALSE- value
the replacement to be used for the entries identified in
x- name
a literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to the
namesof the object.
See also
lost_tags_action()to set the behaviour to adopt when tags are lost through subsetting; default is to issue a warningget_lost_tags_action()to check the current the behaviour
Examples
if (require(dplyr) && require(magrittr)) {
## create a safeframe
x <- cars %>%
make_safeframe(
mph = "speed",
distance = "dist"
) %>%
mutate(result = if_else(speed > 50, "fast", "slow")) %>%
set_tags(ticket = "result")
x
## dangerous removal of a tagged column setting it to NULL issues warning
x[, 1] <- NULL
x
x[[2]] <- NULL
x
x$speed <- NULL
x
}
#> Warning: The following tagged variables are lost:
#> speed - mph
#> Warning: The following tagged variables are lost:
#> result - ticket
#>
#> // safeframe object
#> dist
#> 1 2
#> 2 10
#> 3 4
#> 4 22
#> 5 16
#> 6 10
#> 7 18
#> 8 26
#> 9 34
#> 10 17
#> 11 28
#> 12 14
#> 13 20
#> 14 24
#> 15 28
#> 16 26
#> 17 34
#> 18 34
#> 19 46
#> 20 26
#> 21 36
#> 22 60
#> 23 80
#> 24 20
#> 25 26
#> 26 54
#> 27 32
#> 28 40
#> 29 32
#> 30 40
#> 31 50
#> 32 42
#> 33 56
#> 34 76
#> 35 84
#> 36 36
#> 37 46
#> 38 68
#> 39 32
#> 40 48
#> 41 52
#> 42 56
#> 43 64
#> 44 66
#> 45 54
#> 46 70
#> 47 92
#> 48 93
#> 49 120
#> 50 85
#>
#> tagged variables:
#> distance - dist