attachLocally.Object.RdAttaches an Object locally to an environment. By default, the fields of the Object are attached to the parent frame, that is, the calling environment.
# S3 method for class 'Object'
attachLocally(this, private=FALSE, fields=NULL, excludeFields=NULL, overwrite=TRUE,
envir=parent.frame(), ...)If TRUE, private fields are included, otherwise not.
This is only effective if fields==NULL.
A character vector specifying elements to be copied.
If NULL, all elements are considered.
A character vector specifying elements not to
be copied. This has higher priority than fields.
If FALSE, fields that already exists will not be
copied.
The environment where fields are copied to.
Not used.
foo <- function(object, arg1="some value", ...) {
cat("Local objects in foo():\n")
print(ls())
attachLocally(object)
cat("\nLocal objects in foo():\n")
print(ls())
for (name in ls()) {
cat("\nObject '", name, "':\n", sep="")
print(get(name, inherits=FALSE))
}
}
a <- "A string"
obj <- Object()
obj$a <- "Another string"
obj$b <- NA
foo(obj)
#> Local objects in foo():
#> [1] "arg1" "object"
#>
#> Local objects in foo():
#> [1] "a" "arg1" "b" "object"
#>
#> Object 'a':
#> [1] "Another string"
#>
#> Object 'arg1':
#> [1] "some value"
#>
#> Object 'b':
#> [1] NA
#>
#> Object 'object':
#> [1] "Object: 0x600f7803cec0"
print(a)
#> [1] "A string"