Abhijit Bera
2009-Sep-16 07:02 UTC
[Rd] I want to get a reference to this time series object
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 <stdio.h> #include <R.h> #include <Rinternals.h> #include <Rdefines.h> #include <Rembedded.h> 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"))); PROTECT(c=R_tryEval(e,R_GlobalEnv,NULL)); PROTECT(e=lang3(install("["),install("SWX.RET"),c)); 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]]
Romain Francois
2009-Sep-16 07:18 UTC
[Rd] I want to get a reference to this time series object
Hi, Luckily, R has its own parser, so you don't have to reimplement it. Just parse your string 'SWX.RET[1:6,c("SBI,"SPI","SII")]' and eval the parsed expression(s). The R_ParseVector function will help you. Romain On 09/16/2009 09:02 AM, Abhijit Bera wrote:> > 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<stdio.h> > #include<R.h> > #include<Rinternals.h> > #include<Rdefines.h> > #include<Rembedded.h> > > 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"))); > PROTECT(c=R_tryEval(e,R_GlobalEnv,NULL)); > > PROTECT(e=lang3(install("["),install("SWX.RET"),c)); > 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; > }-- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/yw8E : New R package : sos |- http://tr.im/y8y0 : search the graph gallery from R `- http://tr.im/y8wY : new R package : ant
mtmorgan at fhcrc.org
2009-Sep-16 12:30 UTC
[Rd] I want to get a reference to this time series object
Quoting Abhijit Bera <abhibera at gmail.com>:> 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 <stdio.h> > #include <R.h> > #include <Rinternals.h> > #include <Rdefines.h> > #include <Rembedded.h> > > 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 at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >