In order to separate the user-facing API from the testable code that
implements user-facing API, all user-facing S3 classes are always
wrapped in the "wrapper"
class. Moreover, instead of overloading
the [[
and $
operators and the .DollarNames
method for all these
user-facing classes, three new methods are introduced, namely:
double_bracket
, dollar_name
and dollar_names
, which have the
exact same semantics. This way, in the test code, accessing actual
data can be clearly distinguished from the user-facing tab-completion
mechanism.
wrap
puts x
in a list and sets that list's class
to "wrapper"
.
unwrap
returns the original wrapped object.
print.wrapper
calls the actual print
method for the
unwrapped object.
str.wrapper
is required because RStudio calls str on
objects from the global environment, whose .default
version calls
the [[
operator, which produces a message. This is confusing to the
end user because they haven't requested an explicit object extraction.
the .DollarNames
method and the $
operator are the
only user-facing entry points into the tab-completion mechanism.
dollar_names
is an equivalent of .DollarNames
and
dollar_name
is an equivalent of the $
operator. All S3 classes
that are publicly exposed via the "wrapper"
class need to define
their own version of these two methods to enable tab completion.
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 31 32 33 34 35 36 37 38 39 40 41 | wrap(x, class)
unwrap(x)
is_wrapper(x)
## S3 method for class 'wrapper'
print(x, ...)
## S3 method for class 'wrapper'
str(object, ...)
## S3 method for class 'wrapper'
.DollarNames(x, pattern = "")
## S3 method for class 'wrapper'
x$i
## S3 method for class 'wrapper'
x[[i]]
dollar_names(x, pattern = "", ...)
dollar_name(x, i)
double_bracket(x, i)
## S3 method for class 'wrapper'
dollar_names(x, pattern = "", ...)
## S3 method for class 'wrapper'
dollar_name(x, i)
## S3 method for class 'wrapper'
double_bracket(x, i)
## Default S3 method:
dollar_names(x, pattern = "", ...)
## Default S3 method:
dollar_name(x, i)
|
x |
object to be wrapped, a |
class |
optional S3 class name assigned alongside the |
... |
further arguments passed to or from other methods. |
object |
|
pattern |
regular expression; only matching names are returned. |
i |
key name, index value. |
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.