Re: [Rd] R CMD check does not recognize S4 functions inside other functions?

2009-08-28 Thread Sylvain Loiseau
It looks as if it is very related to the message I posted just some day  
ago ("Problem with standard generic methods in Matrix package").


Sylvain

On Fri, 28 Aug 2009 03:12:48 +0200, Giles Hooker  wrote:


I am developing a new R package and am now checking it for submission to
CRAN.

The some functions in the package make use of the sparse matrix routines
in the package 'Matrix'.

When these are loaded in R, they create no problems.

However, when running R CMD check, I run into the following error in  
executing the examples in a .rd file:


 >   DD = Matrix(diag(1,200),sparse=TRUE)
 >   tDD = t(DD)
 >
 > fres = FitMatchOpt(coefs=coefs,which=2,pars=pars,proc)
Error in t.default(DD) : argument is not a matrix
Calls: FitMatchOpt -> t -> t.default
Execution halted

The first two lines of the function FitMatchOpt are

   DD = Matrix(diag(1,200),sparse=TRUE)
   tDD = t(DD)

These were fine when given in the examples section of the .rd file  
directly. However, when defined within a function in the package, the  
lines cause an error.


Sparse matrices improve the computational efficiency of the routines I  
am developing and I would like to use them. But I'm not sure how I can  
get around this error.


Many thanks,

Giles Hooker

__
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] R CMD check does not recognize S4 functions inside other functions?

2009-08-28 Thread Martin Maechler
> "GH" == Giles Hooker 
> on Thu, 27 Aug 2009 21:12:48 -0400 writes:

GH> I am developing a new R package and am now checking it for submission to
GH> CRAN.

GH> The some functions in the package make use of the sparse matrix routines
GH> in the package 'Matrix'.

GH> When these are loaded in R, they create no problems.

GH> However, when running R CMD check, I run into the following error in 
GH> executing the examples in a .rd file:

>> DD = Matrix(diag(1,200),sparse=TRUE)
>> tDD = t(DD)

{{well, the transpose of a diagonal matrix, really ??}}

Just a remark:   For larger values of n=200,
it would be more efficient (and elegant) to directly use

   DD <- Diagonal(200)

See   ?Diagonal,  and e.g.,

  > Diagonal(4)
  4 x 4 diagonal matrix of class "ddiMatrix"
   [,1] [,2] [,3] [,4]
  [1,]1...
  [2,].1..
  [3,]..1.
  [4,]...1
  > .symDiagonal(4)
  4 x 4 sparse Matrix of class "dsCMatrix"

  [1,] 1 . . .
  [2,] . 1 . .
  [3,] . . 1 .
  [4,] . . . 1





>> 
>> fres = FitMatchOpt(coefs=coefs,which=2,pars=pars,proc)
GH> Error in t.default(DD) : argument is not a matrix
GH> Calls: FitMatchOpt -> t -> t.default
GH> Execution halted

GH> The first two lines of the function FitMatchOpt are

GH> DD = Matrix(diag(1,200),sparse=TRUE)
GH> tDD = t(DD)

GH> These were fine when given in the examples section of the .rd file 
GH> directly. However, when defined within a function in the package, the 
GH> lines cause an error.

How does your package "get Matrix"?

I'm sure there's some problem in requiring Matrix, in either
your DESCRIPTION or your NAMESPACE  (or a .First.library or ...)

Your DESCRIPTION should have

Depends: , Matrix
Imports: , Matrix

and your NAMESPACE  

either
   importFrom("Matrix", .)# the things you need

or (if you use many things from the Matrix package, and/or
in some way just *extend* it ...)

   import("Matrix")



Does that help?

Best regards,
Martin Maechler


GH> Sparse matrices improve the computational efficiency of the routines I 
GH> am developing and I would like to use them. But I'm not sure how I can 
GH> get around this error.

GH> Many thanks,

GH> Giles Hooker

GH> __
GH> R-devel@r-project.org mailing list
GH> 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] Problem with standard generic methods in Matrix package

2009-08-28 Thread Martin Maechler

> "SL" == Sylvain Loiseau 
> on Thu, 27 Aug 2009 18:06:50 +0200 writes:

SL> Hello,
>> I think it would still be most efficient,
>> if you  do  'R CMD build '
>> and make the   .tar.gz  file available.
>> 
>> Ideally for every reader of this list, from a "public" URL;
>> alternatively, send it per e-mail as attachment;
>> if you use correct MIME type, i.e., one of

SL> Ok, here is the minimal package producing the error :

SL> http://panini.u-paris10.fr/~sloiseau/test_1.0.tar.gz

SL> I have :

SL> library(test)
>> getAdjacentAsSparseMatrix()
SL> [1] "dgCMatrix"
SL> attr(,"package")
SL> [1] "Matrix"
SL> Erreur dans y[1L + 0L:(m - 1L) * (n + 1L)] <- x :
SL> types (de S4 a double) incompatibles dans l'ajustement d'affectation de
SL> type

Yes, Sylvain, you were right (in the other thread
   "R CMD check does not recognize S4 functions ...")
that your problem is very similar to Giles Hooker's one.

You both need to learn more about namespaces.

As help for you (and other readers),
I append a "fixed up" version (1.0-1) of your test package.

I hope this helps,
Martin Maechler



test_1.0-1.tar.gz
Description: application/compressed-tar
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Clarifications please.

2009-08-28 Thread Abhijit Bera
Hi Martin

Here's the code. I'm stuck at one point. I cannot figure out how to print
the dimnames. I've commented it in my code:

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

SEXP e,t1,t2,val;
int errorOccurred,nx,ny,i,j;
double *v;
char x[1024],y[1024];

Rf_initEmbeddedR(argc, argv);

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


PROTECT(e = lang2(install("as.matrix"),install("SWX.RET")));
PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));

v=REAL(t1);

PROTECT(t2=getAttrib(t1,R_DimSymbol));

nx=INTEGER(t2)[0];
ny=INTEGER(t2)[1];

PROTECT(t2=getAttrib(t1,R_DimNamesSymbol));

// I'm getting stuck here.
   // I want to print out the dimnames
   // so that I can get the dates for the timeseries object.
strcpy(x,(CHAR(VECTOR_ELT(t2,0))[0]));
strcpy(y,(CHAR(VECTOR_ELT(t2,1))[0]));

printf("%d * %d\n  %s %s \n Matrix:\n",nx,ny,x,y);

// The matrix is stored in column major order so
// we print it in this manner.
// my previous code was incorrect.
for(i=0;i wrote:

> Hi Abhijit --
>
> Abhijit Bera wrote:
> > Hi Martin
> >
> > Thanks. I think I got it! Read the R extensions documentation again. I
> > don't even need to convert to a list. This is what I did (just a demo):
> >
> > #include 
> > #include 
> > #include 
> > #include 
> >
> > int main (int argc, char** argv)  {
> >
> > SEXP e,t1,t2,val;
> > int errorOccurred,nx,ny,i,j;
> > double *v;
> >
> > Rf_initEmbeddedR(argc, argv);
> >
> > PROTECT(e = lang2(install("library"), mkString("fPortfolio")));
> > R_tryEval(e, R_GlobalEnv, NULL);
> > UNPROTECT(1);
> >
> > /* We try to evaluate the R expression:
> > *  round(cov(100 * SWX.RET), digits = 4)
> > *  we shall split it as:
> > *  t1<-100*SWX.RET
> > *  t2<-cov(t1)
> > *  val<-round(t2,4)
> > */
> >
> > PROTECT(e = lang3(install("*"),ScalarInteger(100),
> install("SWX.RET")));
> > PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));
>
> For what it's worth, and realizing that this is sloppiness in my
> original code, ScalarInteger(100) (and mkString("fPortfolio")) returns
> an unprotected SEXP, so it could in principle be garbage collected while
> lang3 is being evaluated...
>
> >
> > PROTECT(e = lang2(install("cov"),t1));
> > PROTECT(t2 = (R_tryEval(e, NULL, &errorOccurred)));
> >
> > PROTECT(e = lang3(install("round"),t2, ScalarInteger(4)));
> > PROTECT(val = (R_tryEval(e, NULL, &errorOccurred)));
> >
> > Rf_PrintValue(val);
> >
> >/* This isn't required, is extraneous.
> > PROTECT(e = lang2(install("as.list"),val));
> > PROTECT(t2 = (R_tryEval(e, NULL, &errorOccurred)));
> >
> > Rf_PrintValue(t2);*/
>
> the reason I recommended using as.list (for example) was to respect the
> implied abstraction between the object (of class 'timeSeries') and it's
> representation. Apparently there is a method as.list.timeSeries, and a
> list is something that I am allowed to know about. Your code below
> works, but doesn't respect the (R-level) abstraction the class author
> wants. I don't know whether this is regular practice in the R community,
> but it seems like the right thing to do.
>
> Martin
>
> >
> > v=REAL(val);
> >
> > PROTECT(t2=getAttrib(val,R_DimSymbol));
> >
> > nx=INTEGER(t2)[0];
> > ny=INTEGER(t2)[1];
> >
> > /* Just printing out the matrix
> >*  To understand how I can convert
> >*  data types b/w R and C
> >*/
> >
> > printf("Matrix:\n");
> >
> > for(i=0,j=0;i<(nx*ny);i++,j++) {
> >
> > printf("%.4f ",v[i]);
> >
> > if(j==ny-1) {
> > printf("\n");
> > j=0;
> > }
> >
> > }
> >
> > UNPROTECT(6);
> >
> > return 0;
> >
> > }
> >
> > Regards
> >
> > Abhijit Bera
> >
> >
> > On Wed, Aug 26, 2009 at 12:37 PM, Abhijit Bera  > > wrote:
> >
> > Hi Martin
> >
> > Thanks. I think I got the hang of it. I will try it out and post any
> > more queries I have regarding handling data types onto the mailing
> list.
> >
> > Regards
> >
> > Abhijit Bera
> >
> >
> > On Tue, Aug 25, 2009 at 7:15 PM, Martin Morgan  > > wrote:
> >
> > Abhijit Bera mailto:abhib...@gmail.com>>
> > writes:
> >
> > > Hi
> > >
> > > I think I have asked these questions earlier, but I been able
> > to find
> > > answers from the documentation (which I found poorly written
> > in several
> > > places). Will someone be kind enough to give me answers and
> > enlighten me?
> > > (as in explain with CODE?)
> > >
> > > I want to embed R in my application and use the fPortfolio
> > package for
> > > carrying out risk management computations. Right now I'm
> > reading the
> > > Rmetrics Ebook and tryin

Re: [Rd] Wishlist: specify the border color of boxes in legend() (PR#13913)

2009-08-28 Thread maechler

> "FS" == Frederic Schutz 
> on Thu, 27 Aug 2009 13:25:16 +0200 (CEST) writes:

FS> I could not find a way to specify the border color of the boxes drawn in
FS> a legend, so that the legend can match exactly the colors of the actual
FS> plot (e.g. in the case of two superimposed histograms which have
FS> different shading and different borders).

FS> Indeed, the legend function seems to hard code the color "black" for the
FS> borders in this call:

FS> rect2(left = xt, top = yt + ybox/2, dx = xbox, dy = ybox,
FS> col = fill, density = density, angle = angle,
FS> border = "black")

FS> I worked around this by adding a "border" argument to the function
FS> (which defaults to "black"), and changing the call above. It works for
FS> me; I have looked quickly at the rest of the function to see if there
FS> would be a side effect and did not see any (but I haven't checked
FS> carefully).

FS> This could be a useful improvement of the legend() function; in case the
FS> change described above looks usable, I attach two simple diff files (for
FS> the function itself and the manual page).

FS> Cheers,
FS> Frédéric

Thank you, Frédéric,
indeed, the changes are so minimal that I will apply the two
patches and they should be in "R-devel" (for R 2.10.0) soon.

Best regards,
Martin Maechler, ETH Zurich


FS> --040007030503070307040509
FS> Content-Type: text/x-patch;
FS> name="legend.R.diff"
FS> Content-Transfer-Encoding: 7bit
FS> Content-Disposition: inline;
FS> filename="legend.R.diff"

FS> --- legend.R~   2009-03-20 00:05:18.0 +0100
FS> +++ legend.R2009-08-27 11:52:02.0 +0200
FS> @@ -15,8 +15,8 @@
FS> #  http://www.r-project.org/Licenses/
 
FS> legend <-
FS> -function(x, y = NULL, legend, fill=NULL, col = par("col"), lty, lwd, 
pch,
FS> -angle = 45, density = NULL, bty = "o", bg = par("bg"),
FS> +function(x, y = NULL, legend, fill=NULL, col = par("col"), 
border="black",
FS> + lty, lwd, pch, angle = 45, density = NULL, bty = "o", bg = 
par("bg"),
FS> box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),
FS> pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,
FS> xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1, adj = c(0, 0.5),
FS> @@ -217,7 +217,7 @@
FS> fill <- rep(fill, length.out = n.leg)
FS> rect2(left = xt, top=yt+ybox/2, dx = xbox, dy = ybox,
FS> col = fill,
FS> - density = density, angle = angle, border = "black")
FS> + density = density, angle = angle, border = border)
FS> }
FS> xt <- xt + dx.fill
FS> }

FS> --040007030503070307040509
FS> Content-Type: text/x-patch;
FS> name="legend.Rd.diff"
FS> Content-Transfer-Encoding: 7bit
FS> Content-Disposition: inline;
FS> filename="legend.Rd.diff"

FS> --- legend.Rd~  2009-03-20 00:05:18.0 +0100
FS> +++ legend.Rd   2009-08-27 11:57:28.0 +0200
FS> @@ -8,7 +8,7 @@
FS> \title{Add Legends to Plots}
FS> \usage{
FS> legend(x, y = NULL, legend, fill = NULL, col = par("col"),
FS> -   lty, lwd, pch,
FS> +   border="black", lty, lwd, pch,
FS> angle = 45, density = NULL, bty = "o", bg = par("bg"),
FS> box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),
FS> pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,
FS> @@ -34,6 +34,8 @@
FS> with the specified colors (or shaded in the specified colors)
FS> to appear beside the legend text.}
FS> \item{col}{the color of points or lines appearing in the legend.}
FS> +  \item{border}{the border color for the boxes (used only if
FS> +\code{fill} is specified).}
FS> \item{lty, lwd}{the line types and widths for lines appearing in the
FS> legend.  One of these two \emph{must} be specified for line drawing.}
FS> \item{pch}{the plotting symbols appearing in the legend, either as

FS> --040007030503070307040509--

FS> __
FS> R-devel@r-project.org mailing list
FS> 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] R CMD check does not recognize S4 functions inside other functions?

2009-08-28 Thread Henric (Nilsson) Winell

On 2009-08-28 09:58, Martin Maechler wrote:


"GH" == Giles Hooker 
on Thu, 27 Aug 2009 21:12:48 -0400 writes:


GH> I am developing a new R package and am now checking it for submission to
GH> CRAN.

GH> The some functions in the package make use of the sparse matrix routines
GH> in the package 'Matrix'.

GH> When these are loaded in R, they create no problems.

GH> However, when running R CMD check, I run into the following error in 
GH> executing the examples in a .rd file:


>> DD = Matrix(diag(1,200),sparse=TRUE)
>> tDD = t(DD)

{{well, the transpose of a diagonal matrix, really ??}}

Just a remark:   For larger values of n=200,
it would be more efficient (and elegant) to directly use

   DD <- Diagonal(200)

See   ?Diagonal,  and e.g.,

  > Diagonal(4)
  4 x 4 diagonal matrix of class "ddiMatrix"
   [,1] [,2] [,3] [,4]
  [1,]1...
  [2,].1..
  [3,]..1.
  [4,]...1
  > .symDiagonal(4)
  4 x 4 sparse Matrix of class "dsCMatrix"

  [1,] 1 . . .
  [2,] . 1 . .
  [3,] . . 1 .
  [4,] . . . 1





>> 
>> fres = FitMatchOpt(coefs=coefs,which=2,pars=pars,proc)

GH> Error in t.default(DD) : argument is not a matrix
GH> Calls: FitMatchOpt -> t -> t.default
GH> Execution halted

GH> The first two lines of the function FitMatchOpt are

GH> DD = Matrix(diag(1,200),sparse=TRUE)
GH> tDD = t(DD)

GH> These were fine when given in the examples section of the .rd file 
GH> directly. However, when defined within a function in the package, the 
GH> lines cause an error.


How does your package "get Matrix"?

I'm sure there's some problem in requiring Matrix, in either
your DESCRIPTION or your NAMESPACE  (or a .First.library or ...)

Your DESCRIPTION should have

Depends: , Matrix
Imports: , Matrix


Can you please clarify the simultaneous use of `Depends:' and `Imports'. 
The Writing R Extensions manual explicitly says that:


"Packages declared in the 'Depends' field should not also be in the 
'Imports' field."



Thanks,
Henric





and your NAMESPACE  


either
   importFrom("Matrix", .)# the things you need

or (if you use many things from the Matrix package, and/or
in some way just *extend* it ...)

   import("Matrix")



Does that help?

Best regards,
Martin Maechler


GH> Sparse matrices improve the computational efficiency of the routines I 
GH> am developing and I would like to use them. But I'm not sure how I can 
GH> get around this error.


GH> Many thanks,

GH> Giles Hooker

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

__
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] Clarifications please.

2009-08-28 Thread Simon Urbanek


On Aug 28, 2009, at 7:41 AM, Abhijit Bera wrote:


Hi Martin

Here's the code. I'm stuck at one point. I cannot figure out how to  
print

the dimnames. I've commented it in my code:

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

   SEXP e,t1,t2,val;
   int errorOccurred,nx,ny,i,j;
   double *v;
   char x[1024],y[1024];

   Rf_initEmbeddedR(argc, argv);

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


   PROTECT(e = lang2(install("as.matrix"),install("SWX.RET")));
   PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));

   v=REAL(t1);

   PROTECT(t2=getAttrib(t1,R_DimSymbol));



FWIW no need to protect it - t1 is already protected and even more so  
you are blowing t2 away two lines later anyway ...




   nx=INTEGER(t2)[0];
   ny=INTEGER(t2)[1];

   PROTECT(t2=getAttrib(t1,R_DimNamesSymbol));



Again, no need to protect - you have protected t1 already ...



   // I'm getting stuck here.
  // I want to print out the dimnames
  // so that I can get the dates for the timeseries object.
   strcpy(x,(CHAR(VECTOR_ELT(t2,0))[0]));


Please read docs about character vectors - what you probably meant is  
something like

const char *x = CHAR(STRING_ELT(VECTOR_ELT(t2,0), 0));
printf("dim 1 = (%s, ...)\n", x);

Cheers,
Simon




   strcpy(y,(CHAR(VECTOR_ELT(t2,1))[0]));

   printf("%d * %d\n  %s %s \n Matrix:\n",nx,ny,x,y);

   // The matrix is stored in column major order so
   // we print it in this manner.
   // my previous code was incorrect.
   for(i=0;iOn Wed, Aug 26, 2009 at 10:25 PM, Martin Morgan   
wrote:



Hi Abhijit --

Abhijit Bera wrote:

Hi Martin

Thanks. I think I got it! Read the R extensions documentation  
again. I
don't even need to convert to a list. This is what I did (just a  
demo):


#include 
#include 
#include 
#include 

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

   SEXP e,t1,t2,val;
   int errorOccurred,nx,ny,i,j;
   double *v;

   Rf_initEmbeddedR(argc, argv);

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

   /* We try to evaluate the R expression:
   *  round(cov(100 * SWX.RET), digits = 4)
   *  we shall split it as:
   *  t1<-100*SWX.RET
   *  t2<-cov(t1)
   *  val<-round(t2,4)
   */

   PROTECT(e = lang3(install("*"),ScalarInteger(100),

install("SWX.RET")));

   PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));


For what it's worth, and realizing that this is sloppiness in my
original code, ScalarInteger(100) (and mkString("fPortfolio"))  
returns
an unprotected SEXP, so it could in principle be garbage collected  
while

lang3 is being evaluated...



   PROTECT(e = lang2(install("cov"),t1));
   PROTECT(t2 = (R_tryEval(e, NULL, &errorOccurred)));

   PROTECT(e = lang3(install("round"),t2, ScalarInteger(4)));
   PROTECT(val = (R_tryEval(e, NULL, &errorOccurred)));

   Rf_PrintValue(val);

  /* This isn't required, is extraneous.
   PROTECT(e = lang2(install("as.list"),val));
   PROTECT(t2 = (R_tryEval(e, NULL, &errorOccurred)));

   Rf_PrintValue(t2);*/


the reason I recommended using as.list (for example) was to respect  
the
implied abstraction between the object (of class 'timeSeries') and  
it's
representation. Apparently there is a method as.list.timeSeries,  
and a

list is something that I am allowed to know about. Your code below
works, but doesn't respect the (R-level) abstraction the class author
wants. I don't know whether this is regular practice in the R  
community,

but it seems like the right thing to do.

Martin



   v=REAL(val);

   PROTECT(t2=getAttrib(val,R_DimSymbol));

   nx=INTEGER(t2)[0];
   ny=INTEGER(t2)[1];

   /* Just printing out the matrix
  *  To understand how I can convert
  *  data types b/w R and C
  */

   printf("Matrix:\n");

   for(i=0,j=0;i<(nx*ny);i++,j++) {

   printf("%.4f ",v[i]);

   if(j==ny-1) {
   printf("\n");
   j=0;
   }

   }

   UNPROTECT(6);

   return 0;

}

Regards

Abhijit Bera


On Wed, Aug 26, 2009 at 12:37 PM, Abhijit Bera mailto:abhib...@gmail.com>> wrote:

   Hi Martin

   Thanks. I think I got the hang of it. I will try it out and  
post any
   more queries I have regarding handling data types onto the  
mailing

list.


   Regards

   Abhijit Bera


   On Tue, Aug 25, 2009 at 7:15 PM, Martin Morgan  

   > wrote:

   Abhijit Bera mailto:abhib...@gmail.com>>
   writes:


Hi

I think I have asked these questions earlier, but I been able

   to find

answers from the documentation (which I found poorly written

   in several

places). Will someone be kind enough to give me answers and

   enlighten me?

(as in explain with CODE?)

I want to embed R in my application and use the fPortfolio

   package for

carrying out risk management computations. Right now I'm

   reading the

Rmetrics Ebook and trying to convert the various examples into

   embedded C

code.

Coming from a strictly C backgrou

Re: [Rd] Clarifications please.

2009-08-28 Thread Abhijit Bera
Hi Simon

Thanks. All the confusion between CHAR and STRING. First I read this :

http://www1.cuni.cz/~obo/r_surprises.html

In the meantime I got your reply. Then I read the documentation and it made
sense!

Regards

Abhijit Bera

On Fri, Aug 28, 2009 at 6:49 PM, Simon Urbanek
wrote:

>
> On Aug 28, 2009, at 7:41 AM, Abhijit Bera wrote:
>
>  Hi Martin
>>
>> Here's the code. I'm stuck at one point. I cannot figure out how to print
>> the dimnames. I've commented it in my code:
>>
>> int main (int argc, char** argv) {
>>
>>   SEXP e,t1,t2,val;
>>   int errorOccurred,nx,ny,i,j;
>>   double *v;
>>   char x[1024],y[1024];
>>
>>   Rf_initEmbeddedR(argc, argv);
>>
>>   PROTECT(e = lang2(install("library"), mkString("fPortfolio")));
>>   R_tryEval(e, R_GlobalEnv, NULL);
>>   UNPROTECT(1);
>>
>>
>>   PROTECT(e = lang2(install("as.matrix"),install("SWX.RET")));
>>   PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));
>>
>>   v=REAL(t1);
>>
>>   PROTECT(t2=getAttrib(t1,R_DimSymbol));
>>
>>
> FWIW no need to protect it - t1 is already protected and even more so you
> are blowing t2 away two lines later anyway ...
>
>
>nx=INTEGER(t2)[0];
>>   ny=INTEGER(t2)[1];
>>
>>   PROTECT(t2=getAttrib(t1,R_DimNamesSymbol));
>>
>>
> Again, no need to protect - you have protected t1 already ...
>
>
>// I'm getting stuck here.
>>  // I want to print out the dimnames
>>  // so that I can get the dates for the timeseries object.
>>   strcpy(x,(CHAR(VECTOR_ELT(t2,0))[0]));
>>
>
> Please read docs about character vectors - what you probably meant is
> something like
> const char *x = CHAR(STRING_ELT(VECTOR_ELT(t2,0), 0));
> printf("dim 1 = (%s, ...)\n", x);
>
> Cheers,
> Simon
>
>
>
>strcpy(y,(CHAR(VECTOR_ELT(t2,1))[0]));
>>
>>   printf("%d * %d\n  %s %s \n Matrix:\n",nx,ny,x,y);
>>
>>   // The matrix is stored in column major order so
>>   // we print it in this manner.
>>   // my previous code was incorrect.
>>   for(i=0;i>
>>   for(j=0;j>printf("%f ",v[i+(j*ny)]);
>>
>>   printf("\n");
>>
>>   }
>>
>>   //UNPROTECT(3);
>>
>>   return 0;
>>
>> }
>>
>> On Wed, Aug 26, 2009 at 10:25 PM, Martin Morgan 
>> wrote:
>>
>>  Hi Abhijit --
>>>
>>> Abhijit Bera wrote:
>>>
 Hi Martin

 Thanks. I think I got it! Read the R extensions documentation again. I
 don't even need to convert to a list. This is what I did (just a demo):

 #include 
 #include 
 #include 
 #include 

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

   SEXP e,t1,t2,val;
   int errorOccurred,nx,ny,i,j;
   double *v;

   Rf_initEmbeddedR(argc, argv);

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

   /* We try to evaluate the R expression:
   *  round(cov(100 * SWX.RET), digits = 4)
   *  we shall split it as:
   *  t1<-100*SWX.RET
   *  t2<-cov(t1)
   *  val<-round(t2,4)
   */

   PROTECT(e = lang3(install("*"),ScalarInteger(100),

>>> install("SWX.RET")));
>>>
   PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));

>>>
>>> For what it's worth, and realizing that this is sloppiness in my
>>> original code, ScalarInteger(100) (and mkString("fPortfolio")) returns
>>> an unprotected SEXP, so it could in principle be garbage collected while
>>> lang3 is being evaluated...
>>>
>>>
   PROTECT(e = lang2(install("cov"),t1));
   PROTECT(t2 = (R_tryEval(e, NULL, &errorOccurred)));

   PROTECT(e = lang3(install("round"),t2, ScalarInteger(4)));
   PROTECT(val = (R_tryEval(e, NULL, &errorOccurred)));

   Rf_PrintValue(val);

  /* This isn't required, is extraneous.
   PROTECT(e = lang2(install("as.list"),val));
   PROTECT(t2 = (R_tryEval(e, NULL, &errorOccurred)));

   Rf_PrintValue(t2);*/

>>>
>>> the reason I recommended using as.list (for example) was to respect the
>>> implied abstraction between the object (of class 'timeSeries') and it's
>>> representation. Apparently there is a method as.list.timeSeries, and a
>>> list is something that I am allowed to know about. Your code below
>>> works, but doesn't respect the (R-level) abstraction the class author
>>> wants. I don't know whether this is regular practice in the R community,
>>> but it seems like the right thing to do.
>>>
>>> Martin
>>>
>>>
   v=REAL(val);

   PROTECT(t2=getAttrib(val,R_DimSymbol));

   nx=INTEGER(t2)[0];
   ny=INTEGER(t2)[1];

   /* Just printing out the matrix
  *  To understand how I can convert
  *  data types b/w R and C
  */

   printf("Matrix:\n");

   for(i=0,j=0;i<(nx*ny);i++,j++) {

   printf("%.4f ",v[i]);

   if(j==ny-1) {
   printf("\n");
   j=0;
   }

   }

   UNPROTECT(6);

   return 0;

 }

 Regards

 Abhijit Bera


 On Wed, Aug 26, 2009 at 12:37

Re: [Rd] Clarifications please.

2009-08-28 Thread Abhijit Bera
Hi Simon/Martin

I have updated the code. I think I may be doing mistakes with PROTECT and
UNPROTECT but the code works fine, no errors or warnings so far. Please
suggest any improvements/changes.

#include 
#include 
#include 
#include 

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

SEXP e,t1,t2,val;
int errorOccurred,nx,ny,i,j;
double *v;
const char *x,*y;

Rf_initEmbeddedR(argc, argv);

// loading fPortfolio library

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

// This is a demo to print out the SWX.RET built-in object

// Converting SWX.RET into a matrix

PROTECT(e = lang2(install("as.matrix"),install("SWX.RET")));
PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));

v=REAL(t1);

// Finding the dimensions of the matrix
PROTECT(t2=getAttrib(t1,R_DimSymbol));

nx=INTEGER(t2)[0];
ny=INTEGER(t2)[1];

// Obtaining a pointer to the dimnames list of the matrix
PROTECT(t2=getAttrib(t1,R_DimNamesSymbol));


// The matrix is stored in column major order so
// we print it in this manner.

// Printing column headings
// This contains the index names of the SWX.RET object
for(j=0;j wrote:

> Hi Simon
>
> Thanks. All the confusion between CHAR and STRING. First I read this :
>
> http://www1.cuni.cz/~obo/r_surprises.html
>
> In the meantime I got your reply. Then I read the documentation and it made
> sense!
>
> Regards
>
> Abhijit Bera
>
>
> On Fri, Aug 28, 2009 at 6:49 PM, Simon Urbanek <
> simon.urba...@r-project.org> wrote:
>
>>
>> On Aug 28, 2009, at 7:41 AM, Abhijit Bera wrote:
>>
>>  Hi Martin
>>>
>>> Here's the code. I'm stuck at one point. I cannot figure out how to print
>>> the dimnames. I've commented it in my code:
>>>
>>> int main (int argc, char** argv) {
>>>
>>>   SEXP e,t1,t2,val;
>>>   int errorOccurred,nx,ny,i,j;
>>>   double *v;
>>>   char x[1024],y[1024];
>>>
>>>   Rf_initEmbeddedR(argc, argv);
>>>
>>>   PROTECT(e = lang2(install("library"), mkString("fPortfolio")));
>>>   R_tryEval(e, R_GlobalEnv, NULL);
>>>   UNPROTECT(1);
>>>
>>>
>>>   PROTECT(e = lang2(install("as.matrix"),install("SWX.RET")));
>>>   PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));
>>>
>>>   v=REAL(t1);
>>>
>>>   PROTECT(t2=getAttrib(t1,R_DimSymbol));
>>>
>>>
>> FWIW no need to protect it - t1 is already protected and even more so you
>> are blowing t2 away two lines later anyway ...
>>
>>
>>nx=INTEGER(t2)[0];
>>>   ny=INTEGER(t2)[1];
>>>
>>>   PROTECT(t2=getAttrib(t1,R_DimNamesSymbol));
>>>
>>>
>> Again, no need to protect - you have protected t1 already ...
>>
>>
>>// I'm getting stuck here.
>>>  // I want to print out the dimnames
>>>  // so that I can get the dates for the timeseries object.
>>>   strcpy(x,(CHAR(VECTOR_ELT(t2,0))[0]));
>>>
>>
>> Please read docs about character vectors - what you probably meant is
>> something like
>> const char *x = CHAR(STRING_ELT(VECTOR_ELT(t2,0), 0));
>> printf("dim 1 = (%s, ...)\n", x);
>>
>> Cheers,
>> Simon
>>
>>
>>
>>strcpy(y,(CHAR(VECTOR_ELT(t2,1))[0]));
>>>
>>>   printf("%d * %d\n  %s %s \n Matrix:\n",nx,ny,x,y);
>>>
>>>   // The matrix is stored in column major order so
>>>   // we print it in this manner.
>>>   // my previous code was incorrect.
>>>   for(i=0;i>>
>>>   for(j=0;j>>printf("%f ",v[i+(j*ny)]);
>>>
>>>   printf("\n");
>>>
>>>   }
>>>
>>>   //UNPROTECT(3);
>>>
>>>   return 0;
>>>
>>> }
>>>
>>> On Wed, Aug 26, 2009 at 10:25 PM, Martin Morgan 
>>> wrote:
>>>
>>>  Hi Abhijit --

 Abhijit Bera wrote:

> Hi Martin
>
> Thanks. I think I got it! Read the R extensions documentation again. I
> don't even need to convert to a list. This is what I did (just a demo):
>
> #include 
> #include 
> #include 
> #include 
>
> int main (int argc, char** argv)  {
>
>   SEXP e,t1,t2,val;
>   int errorOccurred,nx,ny,i,j;
>   double *v;
>
>   Rf_initEmbeddedR(argc, argv);
>
>   PROTECT(e = lang2(install("library"), mkString("fPortfolio")));
>   R_tryEval(e, R_GlobalEnv, NULL);
>   UNPROTECT(1);
>
>   /* We try to evaluate the R expression:
>   *  round(cov(100 * SWX.RET), digits = 4)
>   *  we shall split it as:
>   *  t1<-100*SWX.RET
>   *  t2<-cov(t1)
>   *  val<-round(t2,4)
>   */
>
>   PROTECT(e = lang3(install("*"),ScalarInteger(100),
>
 install("SWX.RET")));

>   PROTECT(t1 = (R_tryEval(e, NULL, &errorOccurred)));
>

 For what it's worth, and realizing that this is sloppiness in my
 original code, ScalarInteger(100) (and mkString("fPortfolio")) returns
 an unprotected SEXP, so it could in principle be garbage collected while
 lang3 is being evaluated...


>   PROTECT(e = lang2(install("cov"),t1));
>   PROTECT(t2 = (R_tryEval(e, NULL, &errorOccurred)));
>
>  

[Rd] S4: names stripped during instantiation of grandchildren of numeric class

2009-08-28 Thread Vitalie S.


Dear All,

A small inconsistency (it's probably not even a buglet):



setClass("A", contains="numeric")

[1] "A"

names(new("A", c(a=23)))

[1] "a"


setClass("B", contains="A")

[1] "B"

names(new("B", c(a=23)))

NULL




This is exactly that kind of behavior S4 was invented for, you expect  
names to be there, but they are not :(.


If that behavior is left to be, one would have to implement @names  
manually for children of basic pseudo-classes, which is quite an overhead  
given the functionality is already in base R.


Thanks,
Vitalie.

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


Re: [Rd] Clarifications please.

2009-08-28 Thread Simon Urbanek


On Aug 28, 2009, at 9:42 AM, Abhijit Bera wrote:


Hi Simon

Thanks. All the confusion between CHAR and STRING. First I read this :

http://www1.cuni.cz/~obo/r_surprises.html



Uh, oh, that doc confuses things even more and is not really correct  
(well, the author admits that he has no idea, but that doesn't help).  
Ok, so a quick clarification of the world of strings and character  
vectors.


In R speak string vector is a vector containing 0 or more character  
strings (e.g. c("foo","bar")). It is sometimes also called character  
vector, because it is of type and mode "character". In the R C-level  
API string vectors are represented by the type STRSXP and you can use  
LENGTH(), STRING_ELT(x,i) and SET_STRING_ELT(x,i,a) macros on it to  
get the length and get/set elements. The elements are character  
strings which are represented by CHARSXP (this is not a vector in R  
land) and are always read-only.  They can be created using mkChar*()  
functions and the C-level const char* content (\0-terminated for now)  
can be retrieved using CHAR(x).


The author of the above page is confused about mkString - which  
creates a string vector (STRSXP) and mkChar which creates a character  
string (CHARSXP). Essentially mkString(x) is just a shortcut for  
v=allocVector(STRSXP, 1); SET_STRING_ELT(v, 0, mkChar(x)); x; Users in  
R never see CHARSXP directly - it is never used when interfacing  
another function since there are conceptually no scalars in the R  
language.


I hope this helps to sort out the confusion.

Cheers,
Simon


In the meantime I got your reply. Then I read the documentation and  
it made sense!


Regards

Abhijit Bera

[..snip, previously posed code omitted ..]


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


Re: [Rd] S4 Generics and NAMESPACE : justified warning ?

2009-08-28 Thread Yohan Chalabi
 "MM" == Martin Morgan 
 on Tue, 18 Aug 2009 06:15:50 -0700

Hi Martin,

Thanks for your response.

   MM> Commenting as a user, there's no guarantee that the 'plot'
   MM> generic
   MM> defined in pkgA is derived from graphics::plot via setGeneric;
   MM> pkgA
   MM> could define it's own generic, and one would want to be informed
   MM> of the
   MM> collision.

This shouldn't be a problem because generics keep track of the function
and package used.

setGeneric(plot)
str(getGeneric(plot))

   MM>
   MM> Maintenance of packages that have used simple 'import' to pull
   MM> in all
   MM> dependencies is tedious, but using 'import' in some ways
   MM> undermines
   MM> benefits of name spaces (restricting the symbol lookup table
   MM> to reduce
   MM> the number of symbols and the possibility of name collisions,
   MM> and to
   MM> more carefully isolate code inside the package name space to
   MM> changes in
   MM> imported packages or induced by the user). So I think a 'better
   MM> practice' is to explicitly import just those functions,
   MM> classes, etc
   MM> that are required by the package. Maintenance of such selective
   MM> imports
   MM> is much less tedious, even with complicated package
   MM> dependencies. There
   MM> is an unreleased Bioconductor package to identify specific
   MM> imports,
   MM> available for R-2.9.* at


I agree with you that 'importFrom' should be the preferred approach. I
have been using it for some time and I have even wrote my own
functions to automatically generate the NAMESPACE. 


But the drawback is that it makes the dependency with other packages
version specific and can become tricky when one has several such
dependencies.

To make the maintenance of packages easier, I would like to use
the suboptimal 'import' so that I do not need to care about this
NAMESPACE/package version issue. But I cannot do that because of an,
IMO, unjustified warning.

regards,
Yohan

-- 
PhD student
Swiss Federal Institute of Technology
Zurich

www.ethz.ch

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


Re: [Rd] ARM v7/Linux Port/cross-compile?

2009-08-28 Thread Andrew Piskorski
On Thu, Aug 27, 2009 at 06:36:38PM -0400, Simon Urbanek wrote:

> It was fairly straight-forward to build R (like any other cross- 
> compilation). The tricky part is to install packages (if you are truly  
> cross-compiling on another architecture) which I solved by using multi- 
> arch R (which contains both arm and the host architecture) and cross- 
> compiling only the binaries (i.e. only packages with native code).  

Simon, could you provide any links to more detailed info on how you
did that, please?  E.g., how did you detect whether a package contains
binaries (native code) or not?  What exactly is a multi-arch R and how
did you build it?

I often need to build R packages for both x86 32 and 64 bit.
Currently, I build both R and all the packages twice in two entirely
separate directory trees, which is both annoying, and overkill for all
the R-code-only packages.  It sounds like you know a better way...

-- 
Andrew Piskorski 
http://www.piskorski.com/

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


[Rd] Simpler example of embedding R in C++ (Was: Clarifications please.)

2009-08-28 Thread Dirk Eddelbuettel

Abhijit,

I think you are making your life harder than you need to.  Consider this
simple C++ program implementing your example (of loading fPortfolio,
converting the data in SWX.RET to a matrix M, exporting M to C++ and showing
its content):


  e...@ron:~/svn/rinside/pkg/inst/examples> cat rinside_test2.cpp
  // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; -*-
  //
  // Simple example for the repeated r-devel mails by Abhijit Bera
  //
  // Copyright (C) 2009 Dirk Eddelbuettel and GPL'ed 
  
  #include "RInside.h"  // for the embedded R via RInside
  #include "Rcpp.h" // for the R / Cpp interface used for 
transfer
  
  int main(int argc, char *argv[]) {
  
  RInside R(argc, argv);// create an embedded R instance 
  SEXP ans;
  
  std::string txt = "suppressMessages(library(fPortfolio))";
  R.parseEvalQ(txt);// load library, no return valueassign 
matrix M to SEXP variable ans
  
  txt = "M <- as.matrix(SWX.RET); print(head(M)); M";
  R.parseEval(txt, ans);// assign matrix M to SEXP variable ans
  RcppMatrix M(ans);// convert SEXP variable to an 
RcppMatrix
  
  std::cout << "M has " << M.getDim1() << " rows and " << M.getDim2() << " 
cols" << std::endl;
  
  R.parseEval("colnames(M)", ans);  // assign columns names of M to ans
  RcppStringVector cnames(ans); // and into string vector cnames
  
  for (int i=0; i ./rinside_test2
  Package 'sn', 0.4-12 (2009-03-21). Type 'help(SN)' for summary information
  Using the GLPK callable library version 4.37
SBI   SPISII  LP25  LP40  LP60
  2000-01-04 -0.0020881 -0.034390  1.367e-05 -0.011994 -0.018013 -0.026155
  2000-01-05 -0.0001045 -0.010408 -4.955e-03 -0.003657 -0.005837 -0.009011
  2000-01-06 -0.0013598  0.012119  3.813e-03 -0.001324 -0.001645 -0.002396
  2000-01-07  0.0004186  0.022462 -6.162e-04  0.007715  0.011660  0.017063
  2000-01-10  0.000  0.002108  2.381e-03  0.003029  0.004566  0.006948
  2000-01-11 -0.0010468 -0.002774 -2.938e-04 -0.002423 -0.003143 -0.004183
  M has 1916 rows and 6 cols
  Column SBI in row 42 has -0.000848356
  Column SPI in row 42 has 0.0129016
  Column SII in row 42 has -0.000141168
  Column LP25 in row 42 has 0.00261833
  Column LP40 in row 42 has 0.00443192
  Column LP60 in row 42 has 0.00694762
  e...@ron:~/svn/rinside/pkg/inst/examples> 


This took literally a few minutes to put together, given the infrastructure
provided by Rcpp and RInside.

Comments welcome.  I know I still need to enhance the installation
instructions for RInside -- it works out of the box for Debian / Ubuntu on 32
and 64 bit, but another user on FC 64bit alerted me to issues he experienced
which I need to fix.

Hth,  Dirk

-- 
Three out of two people have difficulties with fractions.

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


Re: [Rd] ARM v7/Linux Port/cross-compile?

2009-08-28 Thread Simon Urbanek

On Aug 28, 2009, at 11:18 , Andrew Piskorski wrote:


On Thu, Aug 27, 2009 at 06:36:38PM -0400, Simon Urbanek wrote:


It was fairly straight-forward to build R (like any other cross-
compilation). The tricky part is to install packages (if you are  
truly
cross-compiling on another architecture) which I solved by using  
multi-

arch R (which contains both arm and the host architecture) and cross-
compiling only the binaries (i.e. only packages with native code).


Simon, could you provide any links to more detailed info on how you  
did that, please?  E.g., how did you detect whether a package  
contains binaries (native code) or not?  What exactly is a multi- 
arch R and how did you build it?




See "R Installation and Administration" (R-admin) section 2.5 Sub- 
architectures.


You can build R that has binaries for both 32-bit and 64-bit simply by  
setting r_arch (we use that for quite a while on OS X to build for  
PPC, Intel - both 32 and 64-bit). Subsequently all packages will be  
build for all architectures by default, so there is nothing you have  
to do (except if the packages uses configure script in which case you  
have to install it subsequently with --libs-only for additional  
architectures - see R-admin 2.5 again. This is easy to spot since the  
installed package will have only one directory in "libs").



I often need to build R packages for both x86 32 and 64 bit.  
Currently, I build both R and all the packages twice in two entirely  
separate directory trees, which is both annoying, and overkill for  
all the R-code-only packages.  It sounds like you know a better way...




Yes, the above :).

Cheers,
Simon

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


[Rd] names<- in data.frame (PR#13916)

2009-08-28 Thread vitosmail
Full_Name: Spinu Vitalie
Version: 2.9.0
OS: Windows
Submission from: (NULL) (130.115.113.15)


In assignment of "zero length" names to data.frame:

> tdf <- data.frame(rbind(c(1, 2), c(1, 2)))
> names(tdf) <- c("", "")
> tdf
  structure(c("1", "1"), class = "AsIs") structure(c("2", "2"), class = "AsIs")
1  1  2
2  1  2

For vectors it works nicely:

> tv <- c(1, 2)
> names(tv) <- c("", "")
> tv

1 2 




> sessionInfo()
R version 2.9.0 (2009-04-17) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics  uti

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