Re: [Rd] I want to get a reference to this time series object

2009-09-16 Thread mtmorgan

Quoting Abhijit Bera :


I'm trying to get a reference to this object in C

SWX.RET[1:6,c("SBI,"SPI","SII")]

While i am able to access and use a plain SWX.RET object, I'm getting
confused on how to create an object with the array subscripts like above.

Here is what I tried to do. It doesn't work because "[" is obviously not an
operation or function on SWX.RET. So how do I get a reference to this
object?

#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{

SEXP e,c,portSpec,portData,portConstr,portVal,tsAssets;
int errorOccurred,nx,ny,i,j;
double *v;
const char *x,*y;

Rf_initEmbeddedR(argc, argv);

// loading fPortfolio
PROTECT(e = lang2(install("library"), mkString("fPortfolio")));
R_tryEval(e, R_GlobalEnv, NULL);
UNPROTECT(1);


// creating a default portfolioSpec object
PROTECT(e=lang1(install("portfolioSpec")));
PROTECT(portSpec=R_tryEval(e,R_GlobalEnv, NULL));

// creating a portfolioData object


PROTECT(e=lang4(install("c"),mkString("SBI"),mkString("SPI"),mkString("SII")));



Here you might just as well construct this at the C level

  SEXP idx_j = PROTECT(NEW_CHARACTER(3));
  SET_STRING_ELT(idx_j, 0, mkChar("SBI"));
  ...

(is a PROTECT necessary on mkChar? I don't think SET_STRING_ELT will  
allocate memory).



PROTECT(c=R_tryEval(e,R_GlobalEnv,NULL));

PROTECT(e=lang3(install("["),install("SWX.RET"),c));


Here the call you are trying for is "["(SWX.RET, i, j) so

  SEXP idx_i = PROTECT(NEW_INTEGER(6));
  for (i = 0; i < LENGTH(idx_i); ++i)
   INTEGER(idx_i)[i] = i + 1;

  PROTECT(e = lang4(install("["), install("SWX.RET"),
idx_i, idx_j));

or

  PROTECT(e = lang4(install("["), install("SWX.RET"),
R_MissingArg, idx_j));


While it would be straight-forward to use R_ParseVector to execute a  
string representing this command, presumably your real code will want  
to represent the subscripts (and object) as C variables and not  
hard-coded or user-supplied strings.


Martin


PROTECT(portData=R_tryEval(e,R_GlobalEnv,NULL));

PROTECT(e=lang2(install("print"),portData));
R_tryEval(e,R_GlobalEnv,NULL);


UNPROTECT(3);

Rf_endEmbeddedR(0);

return 0;
}

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Extending [ method to S4 class and drop argument (PR#9211)

2006-09-08 Thread mtmorgan
Or more in the spirit of method dispatch, and to get some insight into
why this design decision is not a bug:

> setClass("test",representation(x="character"))
[1] "test"
> 
> setMethod("[",
+   signature(x="test", drop="missing"),
+   function(x, i, j, ..., drop) {
+   cat("here\n")
+   callNextMethod(x, i, j, ..., drop=TRUE)
+   })
[1] "["
> 
> setMethod("[","test",function(x, i, j, ..., drop) {
+ cat("there\n")
+ print(drop)
+ })
[1] "["
> 
> a = new("test",x="fred")
> a[1]
here
there
[1] TRUE

'drop' is 'missing', and 'missing' satisfies the implicit 'ANY'
condition in the signature of the original method. With this insight,
the design decision to 'ignore' the default argument seems appropriate
to me -- developers implementing the generic might well want to
dispatch on it, since after all it is in the generic signature. This
could lead to an explosion of methods if many arguments have 'default'
values, but such an explosion probably points to a nice distinction
between arguments-for-dispatch and arguments-for-evaluation.

To tell the truth, I hadn't really understood why the error originally
reported occurred, until writing the method with drop="missing" just
now.

Martin

"Gabor Grothendieck" <[EMAIL PROTECTED]> writes:

> Try this:
>
>> setClass("test",representation(x="character"))
> [1] "test"
>> setMethod("[","test",function(x,i,j,...,drop) {
> +print(i)
> +if (missing(drop)) drop <- TRUE
> +print(drop)
> + })
> [1] "["
>> a = new("test",x="fred")
>> a[1]
> [1] 1
> [1] TRUE
>
>
> On 9/8/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>> Full_Name: John Verzani
>> Version: 2.4.0 alpha (2006-09-05 r39134)
>> OS: linux, gentoo 2.6.17
>> Submission from: (NULL) (163.238.43.26)
>>
>>
>> When extending the "[" method to a new S4 class, the default value for the 
>> drop
>> argument is not being found. Here is a small example:
>>
>> setClass("test",representation(x="character"))
>> setMethod("[","test",function(x,i,j,...,drop=TRUE) {print(i);print(drop)})
>> a = new("test",x="fred")
>> a[1]
>>
>> resulting in:
>>
>> [1] 1
>> Error in print(drop) : argument "drop" is missing, with no default
>>
>> I'm expecting TRUE for the value of drop. That's correct isn't?
>>
>> __
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin T. Morgan
Bioconductor / Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024
Seattle, WA 98109

http://bioconductor.org

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel