From sfalcon at fhcrc.org  Mon May 15 17:23:32 2006
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Mon, 15 May 2006 08:23:32 -0700
Subject: [Rd] bindingIsLocked returns illogical logical
In-Reply-To: <Pine.LNX.4.64.0605150803070.8993@gannet.stats.ox.ac.uk> (Brian
	Ripley's message of "Mon, 15 May 2006 08:05:32 +0100 (BST)")
References: <6phody0ct9e.fsf@gopher3.fhcrc.org> <m2lkt3n64e.fsf@ziti.local>
	<Pine.LNX.4.64.0605150803070.8993@gannet.stats.ox.ac.uk>
Message-ID: <m2ves7ia6j.fsf@ziti.local>

Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

> On Sun, 14 May 2006, Seth Falcon wrote:
>> I wonder if ScalarLogical should force 0/1:
>
> Not a good idea: what about the third value, NA?

Indeed, I should have been sleeping (or perhaps I was already :-)

Is it enough to check for NA_LOGICAL or should all NA_* values be
checked for?  Is there an easier/better way to do this sort of check?

Index: Rinlinedfuns.h
===================================================================
--- Rinlinedfuns.h      (revision 38060)
+++ Rinlinedfuns.h      (working copy)
@@ -494,6 +494,8 @@
 INLINE_FUN SEXP ScalarLogical(int x)
 {
     SEXP ans = allocVector(LGLSXP, 1);
+    if ((x != NA_LOGICAL) && (x != 0))
+        x = 1;
     INTEGER(ans)[0] = x;
     return ans;
 }             

Perhaps for completeness, even though at present they are identical,
NA_INTEGER should be in the checking...

INLINE_FUN SEXP ScalarLogical(int x)
{
    SEXP ans = allocVector(LGLSXP, 1);
    if ((x == NA_LOGICAL) || (x == NA_INTEGER))
        x = NA_LOGICAL;
    else if (x != 0)
        x = 1;
     INTEGER(ans)[0] = x;
     return ans;
}

Or perhaps there is a better solution entirely.  

+ seth



>
>> Index: include/Rinlinedfuns.h
>> ===================================================================
>> --- include/Rinlinedfuns.h      (revision 38060)
>> +++ include/Rinlinedfuns.h      (working copy)
>> @@ -494,7 +494,7 @@
>> INLINE_FUN SEXP ScalarLogical(int x)
>> {
>>     SEXP ans = allocVector(LGLSXP, 1);
>> -    INTEGER(ans)[0] = x;
>> +    INTEGER(ans)[0] = (x == 0) ? 0 : 1;
>>     return ans;
>> }
>>
>> Otherwise, I think do_bndIsLocked needs to make a similar operation
>> before calling ScalarLogical.  But preventing these all purpose
>> logicals from escaping would seem to be a good argument for changing
>> ScalarLogical.
>
> I think it needs to handle NAs.
>
> -- 
> 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] bindingIsLocked returns illogical logical
Seth Falcon