From ripley at stats.ox.ac.uk  Thu Jun  7 17:59:39 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu, 7 Jun 2007 16:59:39 +0100 (BST)
Subject: [Rd] missing IntegerFromString()
In-Reply-To: <40e66e0b0706070833k3d8e2afk7dc1a09088351d2a@mail.gmail.com>
References: <m2tztlpv77.fsf@ziti.local>
	<6BA3828721B5434DAAE1E65C25715A6E1ABBC8@dph002.hpi-mcw.lan>
	<40e66e0b0706070833k3d8e2afk7dc1a09088351d2a@mail.gmail.com>
Message-ID: <Pine.LNX.4.64.0706071651590.21987@gannet.stats.ox.ac.uk>

On Thu, 7 Jun 2007, 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.  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: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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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, UK                Fax:  +44 1865 272595



[Rd] missing IntegerFromString()
Prof Brian Ripley