[R] Can you turn a string into a (working) symbol?

jim holtman jholtman at gmail.com
Sat Nov 3 23:02:25 CET 2012


for the second part use 'assign'

> assign(paste0('a', 'a'), 3)
> aa
[1] 3
>



On Sat, Nov 3, 2012 at 5:31 PM, andrewH <ahoerner at rprogress.org> wrote:
> Dear folks--
>
> Suppose I have an expression that evaluates to a string, and that that
> string, were it not a character vector, would be a symbol.  I would like a
> function, call it doppel(), that will take that expression as an argument
> and produce something that functions exactly like the symbol would have if I
> typed it in the place of the function of the expression.  It should go as
> far along the path to evaluation as the symbol would have, and then stop,
> and be available for subsequent manipulation.  For example, if
>
> aa <- 3.1416
> bb  <- function(x) {x^2}
> r <- 2
> xx <- c("aa", "bb")
>
> out <- doppel(xx[1])*doppel(xx[2])(r)
>
> Then out should be 13.3664
>
> Or similarly, after
> doppel(paste("a",  "a",  sep=''))  <-  3
> aa
>
> typing aa should return 3.
>
> Is there such a function? Can there be?
>
> I thought as.symbol would do this, but it does not.
>> as.symbol (xx[1])*as.symbol (xx[2])(r)
> Error: attempt to apply non-function
>
> Looking forward to hearing from y'all.    --andrewH
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Can-you-turn-a-string-into-a-working-symbol-tp4648343.html
> Sent from the R help mailing list archive at Nabble.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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list