[R] converting R objects to C types in .Call

2005-01-25 Thread Edzer J. Pebesma
Is there a specific reason why, instead of
   CHAR(STRING_ELT(chstr, 0));
The S-Plus compatible
  CHARACTER_POINTER(chstr)[0]
does not work in R?
--
Edzer
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] converting R objects to C types in .Call

2005-01-25 Thread Prof Brian Ripley
On Tue, 25 Jan 2005, Edzer J. Pebesma wrote:
Is there a specific reason why, instead of
  CHAR(STRING_ELT(chstr, 0));
The S-Plus compatible
 CHARACTER_POINTER(chstr)[0]
does not work in R?
Yes.
This is really a question for R-devel, but briefly, R and S-PLUS store 
character vectors in very different ways.  This worked prior to 1.2.0, but 
was changed as part of the price of the new (then) garbage collector.

--
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-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] converting R objects to C types in .Call

2005-01-24 Thread Faheem Mitha
Dear People,
I'm trying to write an R wrapper for a C++ library, using .Call. I've 
never used .Call before. I'm currently having some difficulties converting 
a R character string to a C one.

Here is a little test program.
#include R.h
#include Rinternals.h
#include stdio.h
SEXP testfn(SEXP chstr)
{
  char * charptr = CHAR(chstr);
  printf(%s, charptr);
}
This compiles without problems, but when I try to run this as
.Call(testfn, foo)
I get a segmentation fault.
I am sure I am making an obvious mistake, but can someone help me to sort 
it out? Thanks in advance. Please cc me, I'm not subscribed.

 Faheem.
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] converting R objects to C types in .Call

2005-01-24 Thread Barry Rowlingson
Faheem Mitha wrote:
SEXP testfn(SEXP chstr)
{
  char * charptr = CHAR(chstr);
  printf(%s, charptr);
}

I am sure I am making an obvious mistake, but can someone help me to 
sort it out? Thanks in advance. Please cc me, I'm not subscribed.

Firstly, R is expecting an SEXP as a return value! And secondly, your 
SEXP chstr is still a vector - so you need to get an element from it. If 
there's only one element, then that'll be the zeroth element. Here's my 
code:

#include R.h
#include Rinternals.h
#include stdio.h
SEXP testfn(SEXP chstr)
{
  char * charptr = CHAR(VECTOR_ELT(chstr,0));
  printf(%s, charptr);
  return(chstr);
}
 - I'm just returning the same object back in the return() statement, 
and using VECTOR_ELT(chstr,0) to get to the 0'th element.

 SO in R:
  foo = .Call(testfn,fnord)
should print 'fnord' and return foo as fnord.
  foo = .Call(testfn,c(bar,baz))
will print 'bar' and return foo as c(bar,baz)
Baz
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] converting R objects to C types in .Call

2005-01-24 Thread Prof Brian Ripley
On Mon, 24 Jan 2005, Faheem Mitha wrote:
I'm trying to write an R wrapper for a C++ library, using .Call. I've never 
used .Call before. I'm currently having some difficulties converting a R 
character string to a C one.

Here is a little test program.
#include R.h
#include Rinternals.h
#include stdio.h
SEXP testfn(SEXP chstr)
{
 char * charptr = CHAR(chstr);
CHAR(STRING_ELT(chstr, 0));
First you select the first element of the character vector, then select 
its contents as a C-level character array.

 printf(%s, charptr);
}
This compiles without problems, but when I try to run this as
.Call(testfn, foo)
I get a segmentation fault.
I am sure I am making an obvious mistake, but can someone help me to sort it 
out? Thanks in advance. Please cc me, I'm not subscribed.
Please read `Writing R Extensions' for more examples.
--
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-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] converting R objects to C types in .Call

2005-01-24 Thread Faheem Mitha
Hi Barry,
Thanks for your reply.
On Mon, 24 Jan 2005, Barry Rowlingson wrote:
Firstly, R is expecting an SEXP as a return value!
Ouch...
I haven't found anything that says this explicitly, but it looks like 
.Call expects a SEXP to be returned to R. At any rate, trying to use void 
instead gives a segfault.

And secondly, your SEXP chstr is still a vector - so you need to get an 
element from it. If there's only one element, then that'll be the zeroth 
element. Here's my code:

#include R.h
#include Rinternals.h
#include stdio.h
SEXP testfn(SEXP chstr)
{
 char * charptr = CHAR(VECTOR_ELT(chstr,0));
 printf(%s, charptr);
 return(chstr);
}

- I'm just returning the same object back in the return() statement, and 
using VECTOR_ELT(chstr,0) to get to the 0'th element.

SO in R:
 foo = .Call(testfn,fnord)
should print 'fnord' and return foo as fnord.
 foo = .Call(testfn,c(bar,baz))
will print 'bar' and return foo as c(bar,baz)
Thanks for your help. That works fine.   Faheem.
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html