Re: [Rd] missing IntegerFromString()

2007-06-08 Thread Aniko Szabo
I am sorry about the confusion, I was too hasty.
asInteger(coerceVector(x,INTSXP)) does not work after all. Here are more
details of what I am trying to accomplish: I have a matrix with column
names that are actually known to be integers (because I set them so
myself in the R code, say, colnames(mat) - 1:10. Of course, they become
converted to character strings.)

The relevant part of my code used to be:

SEXP MyFunction(SEXP mat);
int warn, minY 
SEXP rl, cl;
char *rn, *cn;
GetMatrixDimnames(mat, rl, cl, rn, cn);
minY = IntegerFromString(VECTOR_ELT(cl,0), warn);
if (warn  0) error(Names of popmatrix columns are not
integers);

Running some tests it appears that VECTOR_ELT(cl,0) is CHARSXP (which I
wound up using without even knowing it).
I tried replacing the IntegerFromString part with both
asInteger(VECTOR_ELT(cl,0)) and with
asInteger(coerceVector(VECTOR_ELT(cl,0),INTSXP)), but as you surmised,
since VECTOR_ELT(cl,0) is CHARSXP, it does not work.

So, how could I get the actual values in the column names?

Thanks for all your help,
Aniko



-Original Message-
From: Prof Brian Ripley [mailto:[EMAIL PROTECTED] 
Sent: Thursday, June 07, 2007 12:51 PM
To: Simon Urbanek
Cc: Douglas Bates; Aniko Szabo
Subject: Re: [Rd] missing IntegerFromString()

On Thu, 7 Jun 2007, Simon Urbanek wrote:


 On Jun 7, 2007, at 1:00 PM, Prof Brian Ripley wrote:

 On Thu, 7 Jun 2007, Simon Urbanek wrote:
 
 
 On Jun 7, 2007, at 11:33 AM, Douglas Bates wrote:
 
 On 6/6/07, Aniko Szabo [EMAIL PROTECTED] wrote:
 Thanks to everybody who responded to my question.
 asInteger(coerceVector(x,INTSXP)) indeed does what I need. I guess
 there
 is a lot I don't understand about type coercion, as I would not
have
 expected it to work.
 
 It is better to use
 
 asInteger(x)
 
 which will do the coercion if necessary.
 
 Unfortunately not if it gets a character vector:
 
 Yes, if it gets a character *vector*, no if it gets a CHARSXP.
 

 Indeed, I was starting with an assumption that the task at hand is to
get the 
 same result as as.integer in R code from a string - mea culpa. I had
the 
 impression that it was likely what Aniko wanted (because anything else
would 
 work with trivial asInteger which I assumed was not enough). Yes, I
made too 
 many unsafe assumptions ;).

You weren't the only one: I think we all are a little confused here 
The positive outcome is that I will make asInteger work on CHARSXPs as 
well.

Brian


 Sorry for the noise,
 Simon


 .Call(foo,1)
 Error: unimplemented type 'character' in 'asInteger'
 
 
 When you do the coercion
 yourself you should PROTECT the result then UNPROTECT it.  Calling
 asInteger directly avoids this overhead without the risk of losing
 data in a garbage collection.  asInteger can accomplish this
because
 only the first element of the SEXP x is converted to an integer.
 
 
 It could, but doesn't ;). That is what the original
IntegerFromString
 did, but now you either have to do that yourself or coerce the whole
 vector (not as efficient but easier to write :P).
 
 But does coerceVector really handle CHARSXPs?  There are not vectors
and I 
 don't see it in the code. Consider
 
 #include Rinternals.h
 
 SEXP foo(SEXP x)
 {
   return coerceVector(STRING_ELT(x, 0), INTSXP);
 }
 
 SEXP foo2(SEXP x)
 {
   Rprintf(%d\n, asInteger(STRING_ELT(x, 0)));
   return x;
 }
 
 .Call(foo,1)
 Error: cannot coerce type char to integer vector
 .Call(foo2,1)
 -2147483648
 [1] 1
 
 (and that is NA_INTEGER).
 
 As I said, if asInteger(coerceVector(x,INTSXP)) works, 'x' is not a 
 CHARSXP.  So I have little idea what the actual story here is.
 
 Brian
 
 
 Cheers,
 Simon
 
 
 
 Aniko
 
 -Original Message-
 From: Seth Falcon [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, June 05, 2007 11:24 PM
 To: Aniko Szabo
 Cc: r-devel@r-project.org
 Subject: Re: [Rd] missing IntegerFromString()
 
 Hi Aniko,
 
 Aniko Szabo [EMAIL PROTECTED] writes:
 
 I have created a DLL not so long ago using C code. My code used
the
 IntegerFromString() function that used to be exported in the
 Rinternals.h header file (and thus easily accessible). Recently I
 upgraded to R 2.5.0 and my DLL stopped working. I see that the
 IntegerFromString() function is not exported in any of the header
 files
 in the RHOME\include directory. Is it possible for me to use it
 without
 installing all R source files? I can see that the function is in
 coerce.c, however it #includes other stuff that I don't have and
 I am
 afraid to mess things about by doing things I don't understand.
Or
 perhaps there is another function that is intended to be used
 instead?
 
 I think you want asInteger (which calls IntegerFromString).  This
is
 in RHOME/include/Rinternals.h
 
 Best Wishes,
 
 + seth
 
 PS: Nice to see you again :-)
 
 
 --
 Seth Falcon | Computational Biology | Fred Hutchinson Cancer
Research
 Center
 http://bioconductor.org
 
 __
 R-devel@r-project.org 

Re: [Rd] missing IntegerFromString()

2007-06-08 Thread Seth Falcon
Aniko Szabo [EMAIL PROTECTED] writes:

 I am sorry about the confusion, I was too hasty.
 asInteger(coerceVector(x,INTSXP)) does not work after all. Here are more
 details of what I am trying to accomplish: I have a matrix with column
 names that are actually known to be integers (because I set them so
 myself in the R code, say, colnames(mat) - 1:10. Of course, they become
 converted to character strings.)

 The relevant part of my code used to be:

 SEXP MyFunction(SEXP mat);
   int warn, minY 
   SEXP rl, cl;
   char *rn, *cn;
   GetMatrixDimnames(mat, rl, cl, rn, cn);
   minY = IntegerFromString(VECTOR_ELT(cl,0), warn);
   if (warn  0) error(Names of popmatrix columns are not
 integers);

 Running some tests it appears that VECTOR_ELT(cl,0) is CHARSXP (which I
 wound up using without even knowing it).
 I tried replacing the IntegerFromString part with both
 asInteger(VECTOR_ELT(cl,0)) and with
 asInteger(coerceVector(VECTOR_ELT(cl,0),INTSXP)), but as you surmised,
 since VECTOR_ELT(cl,0) is CHARSXP, it does not work.

 So, how could I get the actual values in the column names?

How about:

  SEXP colnums;
  int *ivals;
  PROTECT(colnums = coerceVector(cl, INTSXP));
  ivals = INTEGER(colnums);

Here you convert the STRSXP cl into an INTSXP.  If you want the actual
integer values, use the ivals pointer.

+ seth

-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org

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


Re: [Rd] missing IntegerFromString()

2007-06-08 Thread Prof Brian Ripley
On Thu, 7 Jun 2007, Aniko Szabo wrote:

 I am sorry about the confusion, I was too hasty.
 asInteger(coerceVector(x,INTSXP)) does not work after all. Here are more
 details of what I am trying to accomplish: I have a matrix with column
 names that are actually known to be integers (because I set them so
 myself in the R code, say, colnames(mat) - 1:10. Of course, they become
 converted to character strings.)

 The relevant part of my code used to be:

 SEXP MyFunction(SEXP mat);
   int warn, minY
   SEXP rl, cl;
   char *rn, *cn;
   GetMatrixDimnames(mat, rl, cl, rn, cn);
   minY = IntegerFromString(VECTOR_ELT(cl,0), warn);
   if (warn  0) error(Names of popmatrix columns are not
 integers);

 Running some tests it appears that VECTOR_ELT(cl,0) is CHARSXP (which I
 wound up using without even knowing it).
 I tried replacing the IntegerFromString part with both
 asInteger(VECTOR_ELT(cl,0)) and with
 asInteger(coerceVector(VECTOR_ELT(cl,0),INTSXP)), but as you surmised,
 since VECTOR_ELT(cl,0) is CHARSXP, it does not work.

 So, how could I get the actual values in the column names?

if(length(cl)  0)
minY = INTEGER(coerceVector(cl, INTSXP))[0];
else minY = NA_INTEGER;

You are assuming that there is at least one dimname, and your code needs 
more careful checks.  cl could be NULL and it could be of length 0.  If 
not NULL it is (currently) guaranteed to be a STRSXP.

In future versions of R 'minY = asInteger(cl)' will be all that is needed.


[Private message posted to the list deleted: you are not allowed to do 
that, as the posting guide points out.  It is a breach of copyright law.]


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

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


[Rd] Question about Running C code from R

2007-06-08 Thread Marthews, T. R.
Dear R-devel,
 
Apologies for sending what is probably a very simple question to R-devel: I am 
definitely missing something very simple and can't work out what it is. I've 
been trying to find the problem here for about a month and need some help!
 
I am trying to work out how to run a C program from an R script. Rather than 
try the C program directly, I'm trying to get a much simpler piece of code to 
work as a proof-of-concept. Here's the C code (simplecall.c):
 
***
#include stdio.h
#include Rinternals.h
#include R_ext/Rdynload.h

int Rc(int a) {
 return (a+3);
}

SEXP Rc2(SEXP a) {
 return(18); //LINE A
// return(ScalarReal(REAL(a)[0]+12)); //LINE B
}

int main()
{
 int i,j;
 SEXP m,n;

 i=4;j=Rc(i);
 printf(\nUsing Rc: %i+3=%i - OK so far.\n,i,j);

 m=6;n=Rc2(m);
 printf(\nUsing Rc2: %i+12=%i - Doing well.\n,m,n);

 printf(\nPress ENTER ...\n);
 getchar();
 return 0;
}
***

and I've compiled this (using the only C compiler I have, which is a Borland 
C++ Builder which insists on calling the executable Project.exe). I managed 
to get it to compile correctly with the two R includes at the front and the two 
functions work OK when I run the C executable.
What I want to do is access these functions from R, however. Here's the R code 
I've written (simplecall.r), which I've put together from various websites and 
guesses:

***
dll=dyn.load(Project1.exe)
syms=getNativeSymbolInfo(c(Rc,Rc2),dll)

RcInR=function(x) {
 .Call(dll$Rc,as.numeric(x))
}

Rc2InR=function(x) {
 .Call(dll$Rc2,as.numeric(x))
}

i1=4;j1=Rc(i1)
cat(\nUsing Rc from R: ,i1,+3=,j1, - OK so far.\n)

m1=6;n1=Rc2(m1)
cat(\nUsing Rc2 from R: ,m1,+12=,n1, - Doing well.\n);

***

Basically, there are two problems (at least!). If I have LINE A in the C code 
instead of LINE B then it compiles alright, but if I try to run the R script it 
says:

Error in FUN(c(Rc, Rc2)[[1]], ...) : no such symbol Rc in package 
C:/Documents and Settings/Toby/Desktop/cfiles/Project1.exe

I've read a lot online about the R CMD INSTALL command and the ?INSTALL man 
page, but I can't use that command because I've got Windows-R rather than 
Linux-R (unfortunately). Somehow, R can't 'see' these C functions in the 
executable, even though I thought including Rinternals.h ensured that it could. 
Stuck.

Additionally, if I have LINE B instead of LINE A in the C code then it won't 
compile at all and gives the errors:

[Linker error] Unresolved external '_REAL' referenced from C:\DOCUMENTS AND 
SETTINGS\TOBY\DESKTOP\CFILES\SIMPLECALL.OBJ
[Linker error] Unresolved external '_Rf_ScalarReal' referenced from 
C:\DOCUMENTS AND SETTINGS\TOBY\DESKTOP\CFILES\SIMPLECALL.OBJ

which I find odd because these symbols are defined in the .h files included at 
the start.

I feel like I'm either a) trying to do this in completely the wrong way or b) 
suffering from having to use a Windows machine or c) both. I tried setting up a 
package and inserting the code in the /src directory, but because I can't use 
the R CMD INSTALL command I can't figure out how to make this idea work either.

Can anyone help me here? Does anyone have an actual example of something like 
this that works?

Toby Marthews

·..¸¸·´¯`..¸¸·´¯`··..¸¸·´¯`·.¸¸·´¯`··..¸¸·´¯`··..¸¸·´¯`·.¸

Institut für Umweltwissenschaften (IfU, www.uzh.ch/uwinst 
http://www.uzh.ch/uwinst ),
Universität Zürich, Winterthurerstr. 190, 8057 Zürich, CH.

Plant  Soil Science Department (www.abdn.ac.uk/biologicalsci/pss 
http://www.abdn.ac.uk/biologicalsci/pss ),
University of Aberdeen, 23 St Machar Dr., Aberdeen AB24 3UU, UK.

¯`··..¸¸·´¯`·.¸¸·´¯`··..¸¸·´¯`··..¸¸·´¯`·.¸ (((º ¸.·´¯`·..¸ (((º

[[alternative HTML version deleted]]

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


Re: [Rd] Question about Running C code from R

2007-06-08 Thread Prof Brian Ripley
Since you are using Borland C++, the examples in the online complement to 
'S Programming' are relevant, as is README.packages.


Basically, you have not managed to export any entry points, and to do so 
you will need to make a DLL and not a .exe.


*However* it is much easier to make use of the MinGW compiler used to make 
R.


You are trying too hard using getNativeSymbolInfo etc, and R CMD INSTALL 
does work under Windows.


I would study 'Writing R Extensions' and 'R Installation and 
Administration Manual', install MinGW and use


R CMD SHLIB

to make a DLL.  It really is a lot easier to follow a well-trodden path
than to create your own.  (In Roger Bivand's analogy: you need stepping 
stones across this morass/mire/bog.)



On Fri, 8 Jun 2007, Marthews, T. R. wrote:


Dear R-devel,

Apologies for sending what is probably a very simple question to 
R-devel: I am definitely missing something very simple and can't work 
out what it is. I've been trying to find the problem here for about a 
month and need some help!


I am trying to work out how to run a C program from an R script. Rather 
than try the C program directly, I'm trying to get a much simpler piece 
of code to work as a proof-of-concept. Here's the C code (simplecall.c):


***
#include stdio.h
#include Rinternals.h
#include R_ext/Rdynload.h

int Rc(int a) {
return (a+3);
}

SEXP Rc2(SEXP a) {
return(18); //LINE A
// return(ScalarReal(REAL(a)[0]+12)); //LINE B
}

int main()
{
int i,j;
SEXP m,n;

i=4;j=Rc(i);
printf(\nUsing Rc: %i+3=%i - OK so far.\n,i,j);

m=6;n=Rc2(m);
printf(\nUsing Rc2: %i+12=%i - Doing well.\n,m,n);

printf(\nPress ENTER ...\n);
getchar();
return 0;
}
***

and I've compiled this (using the only C compiler I have, which is a 
Borland C++ Builder which insists on calling the executable 
Project.exe). I managed to get it to compile correctly with the two R 
includes at the front and the two functions work OK when I run the C 
executable. What I want to do is access these functions from R, however. 
Here's the R code I've written (simplecall.r), which I've put together 
from various websites and guesses:


***
dll=dyn.load(Project1.exe)
syms=getNativeSymbolInfo(c(Rc,Rc2),dll)

RcInR=function(x) {
.Call(dll$Rc,as.numeric(x))
}

Rc2InR=function(x) {
.Call(dll$Rc2,as.numeric(x))
}

i1=4;j1=Rc(i1)
cat(\nUsing Rc from R: ,i1,+3=,j1, - OK so far.\n)

m1=6;n1=Rc2(m1)
cat(\nUsing Rc2 from R: ,m1,+12=,n1, - Doing well.\n);

***

Basically, there are two problems (at least!). If I have LINE A in the C 
code instead of LINE B then it compiles alright, but if I try to run the 
R script it says:


Error in FUN(c(Rc, Rc2)[[1]], ...) : no such symbol Rc in package 
C:/Documents and Settings/Toby/Desktop/cfiles/Project1.exe

I've read a lot online about the R CMD INSTALL command and the 
?INSTALL man page, but I can't use that command because I've got 
Windows-R rather than Linux-R (unfortunately). Somehow, R can't 'see' 
these C functions in the executable, even though I thought including 
Rinternals.h ensured that it could. Stuck.


Additionally, if I have LINE B instead of LINE A in the C code then it 
won't compile at all and gives the errors:


[Linker error] Unresolved external '_REAL' referenced from C:\DOCUMENTS AND 
SETTINGS\TOBY\DESKTOP\CFILES\SIMPLECALL.OBJ
[Linker error] Unresolved external '_Rf_ScalarReal' referenced from 
C:\DOCUMENTS AND SETTINGS\TOBY\DESKTOP\CFILES\SIMPLECALL.OBJ

which I find odd because these symbols are defined in the .h files included at 
the start.

I feel like I'm either a) trying to do this in completely the wrong way or b) 
suffering from having to use a Windows machine or c) both. I tried setting up a 
package and inserting the code in the /src directory, but because I can't use 
the R CMD INSTALL command I can't figure out how to make this idea work either.

Can anyone help me here? Does anyone have an actual example of something like 
this that works?

Toby Marthews

·..¸¸·´¯`..¸¸·´¯`··..¸¸·´¯`·.¸¸·´¯`··..¸¸·´¯`··..¸¸·´¯`·.¸

Institut für Umweltwissenschaften (IfU, www.uzh.ch/uwinst 
http://www.uzh.ch/uwinst ),
Universität Zürich, Winterthurerstr. 190, 8057 Zürich, CH.

Plant  Soil Science Department (www.abdn.ac.uk/biologicalsci/pss 
http://www.abdn.ac.uk/biologicalsci/pss ),
University of Aberdeen, 23 St Machar Dr., Aberdeen AB24 3UU, UK.

¯`··..¸¸·´¯`·.¸¸·´¯`··..¸¸·´¯`··..¸¸·´¯`·.¸ (((º ¸.·´¯`·..¸ (((º

[[alternative HTML version deleted]]




--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 

[Rd] Update help file (PR#9724)

2007-06-08 Thread software
Full_Name: Ian Gregory
Version: latest
OS: Widnows XP
Submission from: (NULL) (58.110.163.199)


fSeries is a great package keep it up.
Need to update the help page.

Type:help(garchOxFit)

The line: 
garchOxFit(formula.mean = ~arma(0,0), formula.var = ~garch(1,1))
Should be changed to:
garchOxFit(~garch(1,1),x)

--
eg.
The example in the help page reads:

Examples
## SOURCE(fSeries1.4D-GarchOxModelling)

## Not run: 
## garchOxFit -
   
   # Load Benchmark Data Set:
   data(dem2gbp)
   x = dem2gbp[, 1]
   
   # Fit GARCH(1,1):
   garchOxFit(formula.mean = ~arma(0,0), formula.var = ~garch(1,1))
## End(Not run)

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


[Rd] R CMD SHLIB error using OS X

2007-06-08 Thread Richard Zur
Hello,

I'm using R 2.5.0 with OS X 10.4.9 and Xcode 2.4.1 on a PowerPC.  My 
code used to compile fine with R 2.4.* and Xcode 2.2 (I think).

I'm compiling some C code (named mhroc_prop.c) that calls a fortran 
routine from mvndstpack.f  I get the error

/usr/bin/libtool: file: mvndstpack.o is not an object file (not allowed 
in a library)

after R outputs (sorry... not sure if this is useful information or not)

gcc-4.0 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -std=gnu99 
-dynamiclib -Wl,-macosx_version_min -Wl,10.3 -undefined dynamic_lookup 
-single_module -multiply_defined suppress -L/usr/local/lib -o 
mhroc_prop.so mhroc_prop.o mvndstpack.o 
-L/Library/Frameworks/R.framework/Resources/lib/ppc -lRlapack 
-L/Library/Frameworks/R.framework/Resources/lib/ppc -lRblas 
-L/usr/local/lib/gcc/powerpc-apple-darwin8/4.2.0 -lgfortran -lgcc_s.10.4 
-lSystemStubs -L/usr/local/lib/gcc/powerpc-apple-darwin8/4.2.0 
-lgfortran -lgcc_s.10.4 -lSystemStubs 
-F/Library/Frameworks/R.framework/.. -framework R

Any ideas?

Thank you,
Richard Zur

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


Re: [Rd] R CMD SHLIB error using OS X

2007-06-08 Thread Simon Urbanek
Richard,

On Jun 8, 2007, at 9:29 AM, Richard Zur wrote:

 I'm using R 2.5.0 with OS X 10.4.9 and Xcode 2.4.1 on a PowerPC.  My
 code used to compile fine with R 2.4.* and Xcode 2.2 (I think).

 I'm compiling some C code (named mhroc_prop.c) that calls a fortran
 routine from mvndstpack.f  I get the error

 /usr/bin/libtool: file: mvndstpack.o is not an object file (not  
 allowed
 in a library)


The problem is then apparently somewhere earlier - do you have a  
proper Fortran compiler installed? Apparently the fortran object file  
is not created correctly. Please send me the full output (and tell me  
which Fortran you are using and how you installed it).

Cheers,
Simon

PS: I'm moving this to R-SIG-Mac as this is Mac-specific.


 after R outputs (sorry... not sure if this is useful information or  
 not)

 gcc-4.0 -arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -std=gnu99
 -dynamiclib -Wl,-macosx_version_min -Wl,10.3 -undefined dynamic_lookup
 -single_module -multiply_defined suppress -L/usr/local/lib -o
 mhroc_prop.so mhroc_prop.o mvndstpack.o
 -L/Library/Frameworks/R.framework/Resources/lib/ppc -lRlapack
 -L/Library/Frameworks/R.framework/Resources/lib/ppc -lRblas
 -L/usr/local/lib/gcc/powerpc-apple-darwin8/4.2.0 -lgfortran -lgcc_s. 
 10.4
 -lSystemStubs -L/usr/local/lib/gcc/powerpc-apple-darwin8/4.2.0
 -lgfortran -lgcc_s.10.4 -lSystemStubs
 -F/Library/Frameworks/R.framework/.. -framework R

 Any ideas?

 Thank you,
 Richard Zur

 __
 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


[Rd] suggested package not found

2007-06-08 Thread Tim Bergsma
I normally work in Windows, but make my packages under Unix.  My package 
'b' suggests my package 'a'.  R CMD check fails for 'b', even though 'a' 
is installed: Packages required but not available: b.  What do I have 
to do to make 'a' available, besides R CMD INSTALL 'a'?  I'm not 
spotting the answer in Writing Extensions  Please point me in the 
right direction.

Thanks in advance,

Tim.

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