<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">(Sorry, Steve; I realized that I originally replied to you instead of the list)</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Okay, the good news is that I know what's going on; the bad news is that there exists no fix that doesn't break the existing syntax of data.table.</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
RCurl was a red-herring. I'm almost certain that loading any library that adds new S4 "[" methods will trigger this behavior. E.g. "Matrix", etc. The reason that my code never worked when you ran it is probably because you had already loading some class like that before running my code.</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
What is happening due to the difference in the way that S3 and S4 check the signatures before method dispatch and R's use of lazy evaluation. Since S3 only checks the first argument, it never evaluates j, which allows data.table to do its cool things with expressions in that argument. Because the S4 "[" method checks the classes of x, i, and j before dispatching it must evaluate j. If j is an expression it either throws an error, or it will not, but it will do the unintended thing of evaluating the expression in the calling frame instead of within the data.table. </div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
As far as I can tell, there is no way to fix this without altering the syntax of data.table. </div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">I do have the following workaround. Adding the following S4 methods allows the use of quoted expressions in j for S4 classes inheriting from data.table that act like unquoted expressions for the S3 data.table. </div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="courier new, monospace">setMethod("[", c(x="data.table", i="ANY", j="ANY"),<br>
function(x, i, j, ...) callNextMethod(...))<br>setMethod("[", c(x="data.table", j="language"),<br> function(x, i, j, ...) data.table(x)[j=eval(j), ...])<br></font></blockquote>
</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
E.g. </div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255);margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<font face="courier new, monospace">> library("Matrix")<div class="im" style="color:rgb(80,0,80)"><br>> setClass("DataTable2", contains="data.table")<br></div>> setMethod("[", c(x="data.table", i="ANY", j="ANY"),<br>
+ function(x, i, j, ...) callNextMethod(...))<br>[1] "["<br>> setMethod("[", c(x="data.table", j="language"),<br>+ function(x, i, j, ...) data.table(x)[j=eval(j), ...])<br>
[1] "["<br>> ## This still doesnt work.<div class="im" style="color:rgb(80,0,80)"><br>> DT2[,v]<br>Error: object 'v' not found<br></div>> ## This does work<br>> DT2[,quote(v)]<div class="im" style="color:rgb(80,0,80)">
<br>[1] 1 2 3 4 5 6 7 8 9</div></font></blockquote><blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255);margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<font face="courier new, monospace">> DT2[,quote(sum(v))]<br>[1] 45</font></blockquote><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"> </div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
I hadn't realized that I was doing something unintended when I started, or maybe I wouldn't have :-) Now R supports S4 classes inheriting from S3 classes pretty well, so it seemed like a good idea at the time. The S4 class I am actually writing is for storing / manipulating MCMC samples. One way to do that is to have a data.frame like object with specific columns, e.g. "chain", "iteration", "parameter", ..., and then add functions that take advantage of this known structure. I want to inherit from the data.frame directly so that it can make use of all the generic functions defined for the data.frame. It is more intuitive to use <font face="courier new, monospace">object[...]</font> rather than <font face="courier new, monospace">object@someSlotName[...].</font><font face="arial, helvetica, sans-serif">That all works great, except that these get samples can get pretty big, so, of course, I want the performance of data.table :-) if I can have it.</font></div>