Re: [Rd] missing IntegerFromString()

From: Simon Urbanek <simon.urbanek_at_r-project.org>
Date: Thu, 07 Jun 2007 11:57:24 -0400

On Jun 7, 2007, at 11:33 AM, Douglas Bates wrote:

> On 6/6/07, Aniko Szabo <aszabo_at_hpi.mcw.edu> 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:sfalcon_at_fhcrc.org]
>> Sent: Tuesday, June 05, 2007 11:24 PM
>> To: Aniko Szabo
>> Cc: r-devel_at_r-project.org
>> Subject: Re: [Rd] missing IntegerFromString()
>>
>> Hi Aniko,
>>
>> "Aniko Szabo" <aszabo_at_hpi.mcw.edu> 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_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 07 Jun 2007 - 16:02:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Jun 2007 - 03:34:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.