From tlumley at uw.edu  Tue Aug 19 22:01:11 2014
From: tlumley at uw.edu (Thomas Lumley)
Date: Wed, 20 Aug 2014 08:01:11 +1200
Subject: [Rd] capture.output on S4 slot
In-Reply-To: <1405659808262.54155@uni.sydney.edu.au>
References: <1405659808262.54155@uni.sydney.edu.au>
Message-ID: <CAJ55+dKua0a+iG7LqbUgBjOB1qGOsubfqcBnpVutZQu84miwMQ@mail.gmail.com>

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



[Rd] capture.output on S4 slot
Thomas Lumley