[R] Converting a string

Gang Chen gangchen at mail.nih.gov
Tue Oct 30 15:30:29 CET 2007


Thanks a lot for all who've provided help!

Yes the accusation of obfuscation is well-justified. Sorry I was not  
specific enough in the original post. But I'm lost at this point.  
What I want to do is this:

 >  contrast(ModelFit, list(Trust="T", Sex=levels(Model$Sex),  
Freq=levels(Model$Freq)), list(Trust="U", Sex=levels(Model$Sex),  
Freq=levels(Model$Freq)), type="average")

However the 2nd and 3rd arguments (the two list's) are pre-defined as  
one string of characters contr

 > contr
[1] "list(Trust=\"T\",Sex=levels(Model$Sex),Freq=levels(Model 
$Freq)),list(Trust=\"U\",Sex=levels(Model$Sex),Freq=levels(Model$Freq))"

If I define

 > args <- strsplit(contr, 'list')
 >  args
[[1]]
[1] ""
[2] "(Trust=\"T\",Sex=levels(Model$Sex),Freq=levels(Model$Freq)),"
[3] "(Trust=\"U\",Sex=levels(Model$Sex),Freq=levels(Model$Freq))"

The following still doesn't work as Bert Gunter has pointed out:

 > contrast(fit.lme, eval(parse(text=args[[1]][2])), eval(parse 
(text=args[[1]][3])), type="average")
Error in parse(text = args[[1]][2]) : unexpected ',' in "(Trust="T","

I also tried to use parse and eval for one inner layer further, but  
it didn't work either. So any better approach to dealing with the pre- 
defined string? Or giving up pre-defining the string?

Thanks,
Gang


On Oct 29, 2007, at 6:31 PM, Bert Gunter wrote:

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