[R] Converting character to function argument

Henrik Bengtsson hb at maths.lth.se
Fri Sep 12 19:32:13 CEST 2003


eval() and parse() are you friends here. Possibly also substitute().
Example:

 eval(parse(text="expand.grid(c(1,0),c(1,0))"))

However, I would like to reply with a question to you. Why do you end up
with a string that you need to convert in the first place? Maybe you are
more interested in a solution like the following one:

N <- 2
l <- lapply(1:N, FUN=function(x) c(0,1))
expand.grid(l)

Henrik Bengtsson
Lund University

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Alvaro.Antonio.Novo at bportugal.pt
> Sent: den 12 september 2003 18:55
> To: r-help at stat.math.ethz.ch
> Subject: [R] Converting character to function argument
> 
> 
> How can one transform a character string into an argument of 
> a function (which is not or I don't want it to be a character string)?
> 
> Example:
> 
> > expand.grid(c(1,0),c(1,0)) ## OK
>   Var1 Var2
> 1    1    1
> 2    0    1
> 3    1    0
> 4    0    0
> 
> 
> > paste(rep("c(0,1)",2),collapse=',') ## to be used below
> [1] "c(0,1),c(0,1)"
> > ## string is the input I want, but it needs to be coerced to the 
> > correct
> format
> 
> > expand.grid(paste(rep("c(0,1)",2),collapse=',')) ## does not get me 
> > there
>            Var1
> 1 c(0,1),c(0,1)
> 
> **************************************************************
> *************
>  AVISO DE CONFIDENCIALIDADE: Esta mensagem, assim como os 
> ficheiros eventualmente anexos, é confidencial e reservada 
> apenas ao conhecimento
> da(s) pessoa(s) nela indicada(s) como destinatária(s). Se não 
> é o seu destinatário, solicitamos que não faça qualquer uso 
> do respectivo conteúdo e proceda à sua destruição, 
> notificando o remetente.  LIMITAÇÃO DE RESPONSABILIDADE: A 
> segurança da transmissão de informação por via electrónica 
> não pode ser garantida pelo remetente, o qual, em 
> consequência, não se responsabiliza por qualquer facto 
> susceptível de afectar a sua integridade.
> 
>  CONFIDENTIALITY NOTICE: This message, as well as existing 
> attached files, is confidential and intended exclusively for 
> the individual(s) named as addressees. If you are not the 
> intended recipient, you are kindly requested not to make any 
> use whatsoever of its contents and to proceed to the 
> destruction of the message, thereby notifying the sender.
>  DISCLAIMER: The sender of this message can not ensure the 
> security of its electronical transmission and consequently 
> does not accept liability for any fact which may interfere 
> with the integrity of its content.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
> 
>




More information about the R-help mailing list