[Rd] capture.output on S4 slot

Thomas Lumley tlumley at uw.edu
Tue Aug 19 22:01:11 CEST 2014


On Fri, Jul 18, 2014 at 4:00 PM, Dario Strbenac
<dstr7320 at uni.sydney.edu.au> wrote:
> Hello,
>
> capture.output produces a different result if the S4 object was created with a constructor than if the body of the constructor is copied and pasted.
>
> setClass("TransformParams", representation(
>   transform = "function",
>   otherParams = "list")
> )
>
> setGeneric("TransformParams", function(transform, ...)
> {standardGeneric("TransformParams")})
> setMethod("TransformParams", character(0), function()
> {
>   new("TransformParams", transform = function(){}, otherParams = list())
> })
>
>> capture.output(TransformParams()@transform)
> [1] "function () "             "{"
> [3] "}"                        "<environment: 0x363bd60>"
>> capture.output(new("TransformParams", transform = function(){}, otherParams = list())@transform)
> [1] "function(){}"
>
> Why is the function split into three parts if a constructor is used ?
>
> --------------------------------------
> Dario Strbenac
> PhD Student
> University of Sydney
> Camperdown NSW 2050
> Australia


Dario,

When you use the constructor, the environment of the function is the
environment inside the constructor; when you use new() it is
R_GlobalEnv

The way functions print is that they print their environment when it
isn't something special like R_GlobalEnv. Since capture.output
captures the printed output, that's what it sees.

The function isn't split up; the printed output is.

   -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



More information about the R-devel mailing list