From hpages at fredhutch.org  Sat Nov  1 11:42:52 2014
From: hpages at fredhutch.org (=?windows-1252?Q?Herv=E9_Pag=E8s?=)
Date: Sat, 01 Nov 2014 03:42:52 -0700
Subject: [Rd] ScalarLogical and setAttrib
In-Reply-To: <CABFfbXvzf4QV6Tb5qi53ddJh_MEhJEDeb3aFwL=s-Nnnpe2okA@mail.gmail.com>
References: <CABFfbXvzf4QV6Tb5qi53ddJh_MEhJEDeb3aFwL=s-Nnnpe2okA@mail.gmail.com>
Message-ID: <5454B92C.7010501@fredhutch.org>

Hi,

The problem is better illustrated with:

   library(inline)
   test2 <- cfunction(body = '
     SEXP success = PROTECT(ScalarLogical(0));
     setAttrib(success, install("foo"), mkString("bar"));
     UNPROTECT(1);
     return success;
   ')
   test3 <- cfunction(body = '
     SEXP success = PROTECT(ScalarLogical(0));
     LOGICAL(success)[0] = 1;
     UNPROTECT(1);
     return success;
   ')

Then:

   > test2()
   [1] FALSE
   attr(,"foo")
   [1] "bar"

   > test3()
   [1] TRUE
   attr(,"foo")
   [1] "bar"

Looks like someone assumed that the SEXP returned by ScalarLogical()
would never be touched before being returned to R (which is probably
true 99% of the time but not 100%).

Cheers,
H.


On 10/31/2014 03:58 PM, Jeroen Ooms wrote:
> Is it expected that attributes set on a LGLSXP created by
> ScalarLogical will apply to all future objects created by
> ScalarLogical as well? For example: the 'test1' function below returns
> FALSE and 'test2' returns FALSE with an attribute:
>
>    library(inline)
>    test1 <- cfunction(body = 'return ScalarLogical(0);')
>    test2 <- cfunction(body = '
>      SEXP success = PROTECT(ScalarLogical(0));
>      setAttrib(success, install("foo"), mkString("bar"));
>      UNPROTECT(1);
>      return success;
>    ')
>
> However after running test2(), then test1() will also return the attribute:
>
>    > test1()
>    [1] FALSE
>    > test2()
>    [1] FALSE
>    attr(,"foo")
>    [1] "bar"
>    > test1()
>    [1] FALSE
>    attr(,"foo")
>    [1] "bar"
>
> It seems like ScalarLogical returns a singleton object, which is not
> the case for ScalarInteger or ScalarReal. I am currently working
> around this using duplicate(ScalarLogical(0)), but was quite surprised
> by this behavior of ScalarLogical.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Herv? Pag?s

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fredhutch.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



[Rd] ScalarLogical and setAttrib
=?windows-1252?Q?Herv=E9_Pag=E8s?=