[R] How to coerce an object name to character vector?

Greg Snow 538280 at gmail.com
Tue Dec 17 22:22:53 CET 2013


Bert,

Save some room in the stocks for me, If arguing against the use of
'assign' is worthy of being sent to the stocks then fortune(236) is
probably enough evidence to put me next to you.

To the original author, can you tell us more of what you are trying to
accomplish?
Replacement functions are one option for doing what your function
above does without the mess of using 'assign' and some of the other
steps.  For example:

> `do<-` <- function(x,value) value
>
> test <- NULL
> do(test) <- mean
>
> test( 1:10 )
[1] 5.5





On Mon, Dec 9, 2013 at 5:55 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> Don:
>
> I defer to your judgment as to whether this was what the OP wanted,
> but I think you would agree that the idiom of assign()ing to the
> global workspace from within a function is almost always a bad idea in
> R. Unfortunately, a better alternative, which frequently involves
> building up a list structure of some sort, depends on context and, in
> particular, on what further is to be done with the assigned objects,
> which usually we (and sometimes the poster) don't know.
>
> While assign() and friends certainly exist and allow script-like
> programming if that is how one wishes to proceed, my understanding is
> that it circumvents the functional-style programming paradigm that R
> naturally supports. So I would urge those who wish to partake of the
> "zen" of R to expunge get() and assign() from their R programming
> vocabulary and perhaps read up a bit on functional programming, which
> is really kinda cool.
>
> Contrary opinions most definitely welcome! The stock awaits me in the
> public squaRe.
>
> Best,
> Bert
>
> On Mon, Dec 9, 2013 at 3:34 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:
>> Not tested, but I think you may want this:
>>
>> do <- function(x,fun, ...) {
>>   fun <- match.fun(fun)
>>   obj.name <- deparse(substitute(x))
>>   assign(obj.name,fun(x, ...))
>> }
>>
>>
>>
>> -Don
>>
>>
>>
>>
>> --
>> Don MacQueen
>>
>> Lawrence Livermore National Laboratory
>> 7000 East Ave., L-627
>> Livermore, CA 94550
>> 925-423-1062
>>
>>
>>
>>
>>
>> On 12/9/13 1:14 PM, "Антон Морковин" <a-morkovin at yandex.ru> wrote:
>>
>>>
>>>   For example, I have a numeric vector named "d" (without any
>>>attributes) and
>>>   I want to coerce it to character vector "d". Is there any such
>>>functions? I
>>>   need  it  to make a function which applies other functions to objects,
>>>   something like this:
>>>
>>>   do<-function(x,fun, ...) {
>>>   fun<-match.fun(fun)
>>>   assign(as.character(quote(x)),fun(x, ...))
>>>   }
>>>
>>>   But, of course, quote(x) always return just "x", not the name of
>>>object.
>>>
>>>   Thanks for help!
>>>
>>>   ______
>>>
>>>   цЁ ц∙ц≈ц│ц√ц┘ц▌ц┴ц┘ц█,
>>>   ц║.ц║. ц╜ц▐ц▓ц▀ц▐ц≈ц┴ц▌
>>>______________________________________________
>>>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.
>>
>> ______________________________________________
>> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> (650) 467-7374
>
> ______________________________________________
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list