Re: [Rd] missing IntegerFromString()

2007-06-07 Thread Aniko Szabo
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.

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 mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] missing IntegerFromString()

2007-06-07 Thread Douglas Bates
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.  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.

>
> 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 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] missing IntegerFromString()

2007-06-07 Thread Simon Urbanek

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:

 > .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).

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 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
>
>

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


Re: [Rd] missing IntegerFromString()

2007-06-07 Thread Prof Brian Ripley
On Thu, 7 Jun 2007, 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.  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.

I have to say I am puzzled.  IntegerFromString works on a CHARSXP, and 
furthermore does not check its argument (which is one reason why it is no 
longer even exposed).  AFAICS, coerceVector(x,INTSXP) will not accept a 
CHARSXP, but it will accept a STRSXP.  (The same for asInteger.)

So two ways to do this for vector 'x' are

asInteger(x)
INTEGER(coerceVector(x,INTSXP))[0]

provided in the second case you know 'x' has length at least 1.  But the 
first is both safer and more efficient.

If you really have a CHARSXP, then you are not really supposed to work 
with 'bare' CHARSXPs unless you know what you are doing (in which case you 
don't need to ask ...).

>
>>
>> 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 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
>

-- 
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


Re: [Rd] missing IntegerFromString()

2007-06-07 Thread Prof Brian Ripley
On Thu, 7 Jun 2007, Prof Brian Ripley wrote:

> On Thu, 7 Jun 2007, 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.  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.
>
> I have to say I am puzzled.  IntegerFromString works on a CHARSXP, and
> furthermore does not check its argument (which is one reason why it is no
> longer even exposed).  AFAICS, coerceVector(x,INTSXP) will not accept a
> CHARSXP, but it will accept a STRSXP.  (The same for asInteger.)

I should have added 'The same for asInteger in R-devel'.  I forgot to 
check R 2.5.0 (and suspect others did the same).

> So two ways to do this for vector 'x' are
>
> asInteger(x)
> INTEGER(coerceVector(x,INTSXP))[0]
>
> provided in the second case you know 'x' has length at least 1.  But the
> first is both safer and more efficient.
>
> If you really have a CHARSXP, then you are not really supposed to work
> with 'bare' CHARSXPs unless you know what you are doing (in which case you
> don't need to ask ...).
>
>>
>>>
>>> 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 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
>>
>
>

-- 
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


Re: [Rd] C function with unknown output length

2007-06-07 Thread Vincent Goulet
Le 07-06-06 à 15:20, Herve Pages a écrit :

> Vincent Goulet wrote:
>> Hi all,
>>
>> Could anyone point me to one or more examples in the R sources of a C
>> function that is called without knowing in advance what will be the
>> length (say) of the output vector?
>>
>> To make myself clearer, we have a C function that computes
>> probabilities until their sum gets "close enough" to 1. Hence, the
>> number of probabilities is not known in advance.
>>
>
> Hi Vincent,
>
> Let's say you want to write a function get_matches(const char *  
> pattern, const char * x)
> that will find all the occurrences of string 'pattern' in string  
> 'x' and "return"
> their positions in the form of an array of integers.
> Of course you don't know in advance how many occurrences you're  
> going to find.
>
> One possible strategy is to:
>
>   - Add an extra arg to 'get_matches' for storing the positions and  
> make
> 'get_matches' return the number of matches (i.e. the length of  
> *pos):
>
>   int get_matches(int **pos_ptr, const char * pattern, const  
> char * x)
>
> Note that pos_ptr is a pointer to an int pointer.
>
>   - In get_matches(): use a local array of ints and start with an  
> arbitrary
> initial size for it:
>
>   int get_matches(...)
>   {
> int *tmp_pos, tmp_size, npos = 0;
>
> tmp_size = some initial guess of the number of matches
> tmp_pos = (int *) S_alloc((long) tmp_size, sizeof(int));
> ...
>
> Then start searching for matches and every time you find one,  
> store its
> position in tmp_pos[npos] and increase npos.
> When tmp_pos is full (npos == tmp_size), realloc with:
>
> ...
> old_size = tmp_size;
> tmp_size = 2 * old_size; /* there are many different  
> strategies for this */
> tmp_pos = (int *) S_realloc((char *) tmp_pos, (long) tmp_size,
> (long) old_tmp_size, sizeof(int));
> ...
>
> Note that there is no need to check that the call to S_alloc()  
> or S_realloc()
> were successful because these functions will raise an error and  
> end the call
> to .Call if they fail. In this case they will free the memory  
> currently allocated
> (and so will do on any error or user interrupt).
>
> When you are done, just return with:
>
> ...
> *pos_ptr = tmp_pos;
> return npos;
>   }
>
>   - Call get_matches with:
>
>   int *pos, npos;
>
>   npos = get_matches(&pos, pattern, x);
>
> Note that memory allocation took place in 'get_matches' but now  
> you need
> to decide how and when the memory pointed by 'pos' will be freed.
> In the R environment, this can be addressed by using  
> exclusively transient
> storage allocation (http://cran.r-project.org/doc/manuals/R- 
> exts.html#Transient)
> as we did in get_matches() so the allocated memory will be  
> automatically
> reclaimed at the end of the call to .C or .Call.
> Of course, the integers stored in pos have to be moved to a  
> "safe" place
> before .Call returns. Typically this will be done with  
> something like:
>
>   SEXP Call_get_matches(...)
>   {
> ...
> npos = get_matches(&pos, pattern, x);
> PROTECT(pos_sxp = NEW_INTEGER(npos));
> memcpy(INTEGER(pos_sxp), pos, npos * sizeof(int));
> UNPROTECT(1);
> return pos_sxp; /* end of call to .Call */
>   }
>
> There are many variations around this. One of them is to "share"  
> pos and npos between
> get_matches and its caller by making them global variables (in this  
> case it is
> recommended to use 'static' in their declarations but this requires  
> that get_matches
> and its caller are in the same .c file).
>
> Hope this helps.

It did, thanks Herve. And thanks also to Dirk and Bill for their  
useful suggestions.

We (actually, my student, but in pure academia style I'll take part  
of the credit ;-) had done something very similar to Herve's  
suggestion, including the "double the size when it's full" strategy,  
but in one function only instead of two. Now I got confirmation it  
was a good way to go. I'm satisfied.

Best,Vincent

>
> H.
>
>> I would like to have an idea what is the best way to handle this
>> situation in R.
>>
>> Thanks in advance!
>>
>> ---
>>Vincent Goulet, Associate Professor
>>École d'actuariat
>>Université Laval, Québec
>>[EMAIL PROTECTED]   http://vgoulet.act.ulaval.ca
>>
>> __
>> 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] missing IntegerFromString()

2007-06-07 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 
>> 
>> 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

Re: [Rd] missing IntegerFromString()

2007-06-07 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-07 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