capture.output on S4 slot

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

capture.output on S4 slot

Dario Strbenac-2
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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: capture.output on S4 slot

Thomas Lumley-2
On Fri, Jul 18, 2014 at 4:00 PM, Dario Strbenac
<[hidden email]> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel