Re: [R] Does R have a "const object"?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed, 16 Mar 2011 11:04:19 -0400

On Wed, Mar 16, 2011 at 10:54 AM, Allan Engelhardt <allane_at_cybaea.com> wrote:
>
>
> On 16/03/11 13:04, Michael Friendly wrote:
>>
>> On 3/15/2011 2:23 PM, Uwe Ligges wrote:
>>>
>>>
>>> On 15.03.2011 15:53, xiagao1982 wrote:
>>>>
>>>> Hi, all,
>>>>
>>>> Does R have a "const object" concept like which is in C++ language? I
>>>> want to set some data frames as constant to avoid being modified
>>>> unintentionally. Thanks!
>>>
>>>
>>> Although there is almost never a "No" in R, the best short answer is:
>>> "No".
>>
>> This is just the flexibility of R.  I've just discovered a new class of
>> geometries based on
>>
>> > pi <- 2.3
>> ?Constants
>
> Yes, but you can still print(base::pi) and rm(pi) to get back to our flat
> world, and you can't
>
>> assign("pi", 4, pos = "package:base")
> Error in assign("pi", 4, pos = "package:base") :
>  cannot change value of locked binding for 'pi'
>
> Just a feature of the search path.  Morale: if you want base::pi, write
> base::pi.
>

Try this:

> old.pi <- pi
> assignInNamespace("pi", 2.3, ns = "base")
> pi

[1] 2.3
> base::pi

[1] 2.3

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
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.
Received on Wed 16 Mar 2011 - 15:15:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 16 Mar 2011 - 15:40:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive