Look carefully at your output (and I don't think that you are showing
us the output of what you actually ran in the order that you ran it).
After running `x %=% 1` you should see that x has the value `1`, but
your output shows `2`, this is the result of the next command `y$a %=%
2`, see the `<<-` assignment will always assign to `x`, not to the
variable named in the local variable x.

An operator like %=% should be used to somehow combine its 2 arguments
and return the result, not ever change a variable value.  The job of
assigning the result should go to the user/caller.  You could possibly
create a new class and an assignment method for that class that would
work like the assignment.

If you can give us more of an idea of what your ultimate goal is then
we may be able to help you find a better approach.

On Tue, Oct 22, 2013 at 2:04 AM, Taiyun Wei <weitai...@gmail.com> wrote:
> Many thanks:)
>
> I have a list(), eg. named opt, I want to check and assign values to it:
>
> if(is.null(opt$a)) opt$a = 'x'
> if(is.null(opt$b)) opt$a = 'y'
> ...
>
> I need to do a lot of these jobs, so I write a function to simplify it:
>
> '%=%' = function(x, y){
>     if(is.null(x)) {
>         x <<- y
>     }
> }
>
> x=NULL
> y=list()
>
>> is.null(x)
> [1] TRUE
>> is.null(y$a)
> [1] TRUE
>
>> x %=% 1;
>> x
> [1] 2
>> y$a %=% 2; ## Can't assign 2 to y$a, still NULL
>> y$a
> NULL
>
>
>
>
>
>
> On Mon, Oct 21, 2013 at 11:38 PM, Greg Snow <538...@gmail.com> wrote:
>>
>> See fortune(174).
>>
>> It is best to avoid using '<<-', if you tell us what you are trying to
>> accomplish then we may be able to provide a better means to accomplish
>> it.
>>
>> On Sat, Oct 19, 2013 at 10:28 PM, Taiyun Wei <weitai...@gmail.com> wrote:
>> > Dear All,
>> >
>> >> opt = list()
>> >> opt$aa <<- TRUE
>> > Error in opt$aa <<- TRUE : object 'opt' not found
>> >
>> > Why?
>> >
>> > --
>> > Regards,
>> > Taiyun
>> > --
>> > Taiyun Wei <weitai...@gmail.com>
>> > Homepage: http://blog.cos.name/taiyun/
>> > Phone: +86-15201142716
>> > Renmin University of China
>> >
>> > ______________________________________________
>> > R-help@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.
>>
>>
>>
>> --
>> Gregory (Greg) L. Snow Ph.D.
>> 538...@gmail.com
>
>
>
>
> --
> Regards,
> Taiyun
> --
> Taiyun Wei <weitai...@gmail.com>
> Homepage: http://blog.cos.name/taiyun/
> Phone: +86-15201142716
> Renmin University of China



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

______________________________________________
R-help@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.

Reply via email to