[R] assigment operator question

Henrik Bengtsson hb at biostat.ucsf.edu
Sat Apr 7 23:41:14 CEST 2012


On Sat, Apr 7, 2012 at 2:01 PM, Mark Heckmann <mark.heckmann at gmx.de> wrote:
> Thanks! I'll try to stick to that advice!
> Maybe there is a better way... Here is what I want:
>
> I want to save some default settings for a package.
> The user can change these using a function similar to par().
> I do not want to use options() here as it will be quite a lot of parameters.
> I was thinking about an invisible object in the global environment and using <<- or assign() to change the settings...
>
> What other roads are there to go?

It sounds like you have a use case where using a global variable is
acceptable, however options() is indeed a good alternative to "host"
your settings.  I'm not sure why you don't want to leverage the
options() functionality that is already in place?  Is it because you
don't want to clutter up the options() output?  You can always keep
all of your package's setting in a single entry, e.g.
options(myPkgOpts=myPkgOpts) where myPkgOpts is a list/tree (e.g. your
'l' below).  Then you can have your own wrapper functions for settings
and getting these options.  The advantage with this approach is that
the settings will sustain any attempts of cleaning up the global
environment, e.g. rm(list=ls(all.names=TRUE)).

FYI, this updated topic may be better suited for the R-devel list.

/Henrik

>
> TIA
> --Mark
>
>
> Am 07.04.2012 um 22:51 schrieb Henrik Bengtsson:
>
>> On Sat, Apr 7, 2012 at 1:30 PM, Mark Heckmann <mark.heckmann at gmx.de> wrote:
>>> Hello,
>>>
>>> using the <<- assignment operator I do not understand why the following does not work.
>>>
>>> l <<- list()
>>> l
>>> list()
>>> l$arg1 <<- "test"
>>> error in l$arg1 <<- "test" : Objekt 'l' not found
>>>
>>> ?"<<-" says:  "The operators <<- and ->> cause a search to made through the environment for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment. "
>>>
>>> Still I do noch understand why the above does not work. The object l is in the global environment. Can someone explain it to me?
>>
>> Yes, the object 'l' is in the global environment, but 'l$arg1' is not,
>> cf. exists("l$arg1") and exists("l").  Instead, this works:
>>
>> l <<- list();
>> l$args1 <- "test";
>>
>> but much much much much much much much much much much much much much
>> much much much much much much much much much much much much much much
>> much much much much much much much much much much much better, do NOT
>> use "<<-" (or assign()/get()) unless you fully understand what you're
>> doing and have a very good reason for doing it, and that reason should
>> be able pass the embarrassment-test on public R mailing lists.  Got
>> the point?  Don't use it - there is another way to do what you want to
>> achieve - you just have to ask/find out how.
>>
>> /H
>>
>>>
>>> Thanks
>>> --Mark
>>> 末末末末末末末末末末末末末末末末末末
>>> Mark Heckmann
>>> Blog: www.markheckmann.de
>>> R-Blog: http://ryouready.wordpress.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.
>
> 末末末末末末末末末末末末末末末末末末
> Mark Heckmann
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
>
>
>
>
>
>
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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