[R] Subsetting Method [ Revisited

Steven McKinney smckinney at bccrc.ca
Thu Jan 10 03:48:38 CET 2008


print("Test") appears to be the last
line of your "[.myObject" function,
so its value will be returned by the function.  
This may not be what you want.

Presumably you want some subset of
the input object returned, so put that
below the 
print("Test")
line.

You could use the invisible() function
to wrap the print, or put it after
while you are developing and debugging your
extractor function.

Try

print("Test")
invisible()

or

invisible(print("Test"))

if these need to be at the end of your extractor.



Steven McKinney


-----Original Message-----
From: r-help-bounces at r-project.org on behalf of Gregory Wall
Sent: Wed 1/9/2008 3:59 PM
To: r-help at r-project.org
Subject: [R] Subsetting Method [ Revisited
 

Hello Everyone:

As usual, thanks in advance for any help. 

I was hoping to get some more advice on this question:

I'm trying to write a subsetting routine for an S3 object I've created --
lets call it myObject. 

myObject has a few attributes basically of type string and numerics. Its
atomic value is just a vector of ints.

I want to write my own subsetting routine to subset myObject in an
unconventional way.

So my method looks like this:

"[.myObject" <- function( obj, i, j, ...){

..... parameter checking and calculations....

print("Test")

}

If I call this method in an R session I get the following results:

obj <- "an instance of a myObject"

obj[]

OUTPUT:
Test
Test

Error: index of length zero.

I'm not sure how to prevent it from printing twice. I don't understand why
"print.default() is returning what it was asked to print, which then gets
printed again automatically" as was mentioned in a previous thread. 

Does the solution involve creating a child class as mentioned here:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/77057.html

I'm having a difficult time following the execution steps for this R code:

obj[] 

Again, thanks very much for any suggestions.

______________________________________________
R-help at r-project.org 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.




More information about the R-help mailing list