[R] Converting a string

Bert Gunter gunter.berton at gene.com
Mon Oct 29 23:31:04 CET 2007


...

Of course if arg1 and arg2 are expressions (as characters strings) that
themselves contain commas, then this won't work. So the poster does, indeed,
seem to have achieved near obfuscation optimality.

Of course this is yet another case where Lumley's principle (at least I
think it's his) holds: if you have to use eval(parse(...)) rethink --
there's a better way.


Bert Gunter
Genentech Nonclinical Statistics


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Tim Calkins
Sent: Monday, October 29, 2007 3:16 PM
To: Gang Chen
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Converting a string

Hi -

you can split the string using strsplit().

if your function uses Argument1 as a string, then you're all set.  If
that's not the case then you can get() the object.

>myfunc <- function(arg1, arg2) {
+ arg1 <- get(arg1)
+ arg2 <- get(arg2)
...
}

>args <- "argument1,argument2" # easier with no " " after the ","

>myfunc(strsplit(args)[[1]][1],strsplit(args)[[1]][2])


This seems needlessly complicated: if obfuscation is your goal, you're
well on your way to success.  Of course, that may be a feature of my
solution -- obfuscation is often MY goal.

HTH.

tim

On 10/30/07, Gang Chen <gangchen at mail.nih.gov> wrote:
> Thanks for the help.
>
> One case is like this: With function MyFunc(Argument1,
> Argument2, ...) I have the first two arguments defined as one
> variable "tempstr", a string of characters, like
>
> tempstr <- "Argument1, Argument2"
>
> The question is how I can feed tempstr into MyFunc to make it
> executable?
>
> Gang
>
> On Oct 29, 2007, at 5:42 PM, jim holtman wrote:
>
> > Can you provide an example of your input and what you expect the
> > output to be.  You can always use 'as.numeric'.
> >
> > On 10/29/07, Gang Chen <gangchen at mail.nih.gov> wrote:
> >> This must be very simple, but I'm stuck. I have a command line in R
> >> defined as a variable of a string of characters. How can I convert
> >> the variable so that I can execute it in R?
> >>
> >> Really appreciate any help,
> >> Gang
>
> ______________________________________________
> 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.
>


-- 
Tim Calkins
0406 753 997

______________________________________________
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.



More information about the R-help mailing list