Re: [Rd] ScalarLogical and setAttrib

2014-11-01 Thread Hadley Wickham
I believe this is by design (and changed relatively recently). FALSE and
TRUE are singletons, like NULL.

Hadley.

On Friday, October 31, 2014, Jeroen Ooms jeroen.o...@stat.ucla.edu 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@r-project.org javascript:; mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel



-- 
http://had.co.nz/

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] ScalarLogical and setAttrib

2014-11-01 Thread Hervé Pagès

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@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: hpa...@fredhutch.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] ScalarLogical and setAttrib

2014-11-01 Thread Hervé Pagès

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@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: hpa...@fredhutch.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] ScalarLogical and setAttrib

2014-11-01 Thread Radford Neal
 From: Jeroen Ooms jeroen.o...@stat.ucla.edu

 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.

 From: Hadley Wickham h.wick...@gmail.com
 
 I believe this is by design (and changed relatively recently). FALSE and
 TRUE are singletons, like NULL.


No, TRUE and FALSE aren't like NULL.  There's only ever one NULL, but
there can be many TRUE and FALSE objects.  For example, see below,
done with R-3.1.2:

   .Internal(inspect(TRUE))
  @2d84488 10 LGLSXP g0c1 [NAM(2)] (len=1, tl=0) 1
   .Internal(inspect(TRUE|FALSE))
  @2d84308 10 LGLSXP g0c1 [] (len=1, tl=0) 1

The problem is a combination of not documenting exactly what
ScalarLogical is supposed to do, and then changing what it does.

I think it's pretty reasonable for people to have assumed they could
attach an attribute to the result of ScalarLogical, given the lack of
any explicit documentation.  Of course, paranoid users would have
checked NAMED on the result to see it they need to duplicate it, but
the documentation on that is less explicit than it might be as well.

This is why pqR still returns an unshared object for ScalarLogical.
Internally, pqR has a ScalarLogicalMaybeShared function that returns the
shared version, which is in read-only memory so that any attempt to change
it will cause an error.  (Similarly, there are ScalarRealMaybeShared, etc.)

   Radford Neal

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] ScalarLogical and setAttrib

2014-10-31 Thread Jeroen Ooms
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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel