[R] Print methods

Doran, Harold HDoran at air.org
Mon Nov 9 22:14:47 CET 2009


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




More information about the R-help mailing list