On May 14, 2013, at 8:02 PM, David Winsemius wrote:

I think you need to read ?setClass and ?setMethod. There is an example of defining a "[" method for a class that inherits from 'data.frame'. I suspect you need to capture the various possibilities for the arguments being present or missing.


setClass("myFrame", contains = "data.frame",
    representation(callexp = "character"))

df1 <- data.frame(x = 1:10, y = rnorm(10), z = sample(letters,10))

mydf1 <- new("myFrame", df1, callexp = "")

setMethod("[",
    signature(x = "myFrame"),
    function (x, i, j, ..., drop = TRUE)
    {callexp <- deparse(substitute(i))
        S3Part(x) <- callNextMethod()
        x@callexp <- callexp
        x
    }
)

mydf1[1:2, ]
#--------------
Object of class "myFrame"
  x          y z
1 1 -1.9119574 f
2 2  0.2719548 i
Slot "callexp":
[1] "1:2"

> mydf1[mydf1$x<5, ]
Object of class "myFrame"
  x          y z
1 1 -0.1065694 u
2 2  0.8571367 l
3 3  1.7259175 z
4 4  0.3618450 x
Slot "callexp":
[1] "mydf1$x < 5"



--
David

Sent from my iPhone

On May 14, 2013, at 5:45 AM, Nhan Vu Lam Chi <nhani...@adatao.com> wrote:

Dear David,

First, I would like to say thank you for your very soon reply. Second, I want to clarify the question because it seems to not carrying exactly what I want to ask. Let take an example on R data.frame:
V1 <- 1
df2 <- df[V1== 1,] # df is a data.frame, this command is correct, right?

The evaluation steps for the above command are:
1. R evaluate V1 > 1 to get TRUE
2. The command becomes df2 <- df[TRUE,] which copies all rows of df to df2

What I want is to capture the "V1 > 1" expression instead of letting R do the evaluation in case of the custom [ function. Assume my class is mydf, the S4 function should be:
setMethod("[", signature(x="mydf"), function(x,i,j,...,drop=TRUE) {
                            e <- substitute(i)
                            // do parsing and custom-evaluation tasks
})

Currently, i is always a vector of type character, numeric or logic due to R evaluation.

I am a newbie to R, so please tolerate my mistakes or misunderstanding. Thanks!
Nhan Vu



On Tue, May 14, 2013 at 12:14 PM, David Winsemius <dwinsem...@comcast.net > wrote:

On May 13, 2013, at 6:38 PM, Nhan Vu Lam Chi wrote:

Hi everyone,
I currently work on a S4 class that has the [ function. I want to capture the unevaluated expression corresponding to the i param using substitute()
function and do a non-standard evaluation. However R automatically
evaluates the expression and give me its value.
For example:
Given mydf[mydf$V1 > 1,] with mydf is an object of my custom S4 dataframe
class and V1 is one of its columns, I want to get the unevaluated
expression mydf$V1 > 1.

My questions are:
1. Is it possible to do that in R?
2. If yes, how to do?

Doesn't this cry out for the S4 class definition of "[" to be answerable?. Because "[" is generic, it could have almost any definition at the whim of the package author.

My R version and OS info are:
R version 2.15.3 (2013-03-01) -- "Security Blanket"
Copyright (C) 2013 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

This is the first time I post to the mailing list, so please forgive any
mistakes and/or advise me if possible.
Regards,
Nhan Vu

     [[alternative HTML version deleted]]

You are forgiven, but  this once, for posting in HTML.

--
David Winsemius
Alameda, CA, USA


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

David Winsemius, MD
Alameda, CA, USA

______________________________________________
R-help@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.

Reply via email to