Alvaro.Antonio.Novo@bportugal.pt
2003-Sep-12 16:55 UTC
[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)) ## OKVar1 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 correctformat> expand.grid(paste(rep("c(0,1)",2),collapse=',')) ## does not get me thereVar1 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.
Have you considered eval(parse(text="expand.grid(c(1,0),c(1,0))")) There are other ways to get what you want, but this should work. hope this helps. spencer graves Alvaro.Antonio.Novo at bportugal.pt wrote:> 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
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 > >