Print methods

2 messages Options
Embed this post
Permalink
Doran, Harold

Print methods

Reply Threaded More More options
Print post
Permalink
I've built a package that contains only two functions for a test run. They are:

g <- function(x){
        x <- x^2
        class(x) <- "foo"
        x
}

print.foo <- function(x, ...){
        cat("This is a test:\n")
        cat(x, "\n")
        invisible(x)
        }

Simply testing these functions in the R workspace prior to a build yields:

> g(1:5)
This is a test:
1 4 9 16 25

Now, I includes a NAMESPACE for this package, and it includes only the following:

export(g)

Package build is now succesful. However, here is what occurs:

> library(foo)
> g(1:5)
[1]  1  4  9 16 25
attr(,"class")
[1] "foo"

Why is the attribute printed on screen here whereas this does not occur when the function print.foo is simply sourced into the R workspace?

Thanks,
Harold

> sessionInfo()
R version 2.10.0 (2009-10-26)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] foo_1.0

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Remko Duursma-2

Re: Print methods

Reply Threaded More More options
Print post
Permalink
Because print.foo is not defined if you only include the function "g"
in your namespace.



remko


-------------------------------------------------
Remko Duursma
Post-Doctoral Fellow

Centre for Plants and the Environment
University of Western Sydney
Hawkesbury Campus
Richmond NSW 2753

Dept of Biological Science
Macquarie University
North Ryde NSW 2109
Australia

Mobile: +61 (0)422 096908
www.remkoduursma.com



On Tue, Nov 10, 2009 at 8:14 AM, Doran, Harold <[hidden email]> wrote:

> I've built a package that contains only two functions for a test run. They are:
>
> g <- function(x){
>        x <- x^2
>        class(x) <- "foo"
>        x
> }
>
> print.foo <- function(x, ...){
>        cat("This is a test:\n")
>        cat(x, "\n")
>        invisible(x)
>        }
>
> Simply testing these functions in the R workspace prior to a build yields:
>
>> g(1:5)
> This is a test:
> 1 4 9 16 25
>
> Now, I includes a NAMESPACE for this package, and it includes only the following:
>
> export(g)
>
> Package build is now succesful. However, here is what occurs:
>
>> library(foo)
>> g(1:5)
> [1]  1  4  9 16 25
> attr(,"class")
> [1] "foo"
>
> Why is the attribute printed on screen here whereas this does not occur when the function print.foo is simply sourced into the R workspace?
>
> Thanks,
> Harold
>
>> sessionInfo()
> R version 2.10.0 (2009-10-26)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] foo_1.0
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.