[R] Converting a string

Tim Calkins tim.calkins at gmail.com
Mon Oct 29 23:15:37 CET 2007


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



More information about the R-help mailing list