[R] boolean SEXP interpretation upon function return

Duncan Murdoch murdoch.duncan at gmail.com
Fri Aug 5 14:35:27 CEST 2011


On 11-08-05 7:19 AM, Prof Brian Ripley wrote:
> On Fri, 5 Aug 2011, Duncan Murdoch wrote:
>
>> On 11-08-05 12:09 AM, Alexandre Aguiar wrote:
>>> Hi,
>>>
>>> When a function returns a SEXP of type LGLSXP (logical) to signal whether
>>> it succeeded or failed, how is it intrepreted? Is it like C where SUCCESS
>>> = 0 or other value?
>>
>> Usually TRUE is used to signal success.  TRUE is non-zero.
>
> Strictly, TRUE is not numeric: it is coerced to 1 when coerced to a
> numeric value.

Another point I should have made:  using logical values to signal 
success is fairly rare among R functions.  More common is to trigger an 
error or return NULL or a zero length vector on failure.  Most R 
functions return something other than TRUE on success, even if they are 
called for their side effects (e.g. setwd() returns the old directory, 
plot.new() returns NULL on success).

Duncan Murdoch

>
> If you are looking at C level at the SEXP: don't as the internal
> representation is just that: 'internal and subject to change'.
>
> There is no C convention to use 0 for success: that is a Unix
> convention for status values as returned by exit(), and even there the
> man page will advise you to use the symbol EXIT_SUCCESS.  Other OSes
> do differ.
>
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>



More information about the R-help mailing list