transfer string to expression

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

transfer string to expression

Yilong Zhang
Dear all:

when I use parse() there is some problems. Below is an example:

b0<-1
b1<-1
x<-1
str2expr<-function(x){eval(parse(text=x))}
test1<-"b0+b1*sqrt(x)"
test2<-"b0+b1"
str2expr(test1)
str2expr(test2)

it can work well for test2 but not for test1.

Could you tell me how to fix this problem or is there other more stable
method to transfer an string to expression?

best regards

Elong

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: transfer string to expression

Duncan Murdoch-2
On 31/10/2010 3:22 PM, Yilong Zhang wrote:

> Dear all:
>
> when I use parse() there is some problems. Below is an example:
>
> b0<-1
> b1<-1
> x<-1
> str2expr<-function(x){eval(parse(text=x))}
> test1<-"b0+b1*sqrt(x)"
> test2<-"b0+b1"
> str2expr(test1)
> str2expr(test2)
>
> it can work well for test2 but not for test1.
>
> Could you tell me how to fix this problem or is there other more stable
> method to transfer an string to expression?

You are evaluating those expressions in the evaluation frame of your
function, where "x" is the expression.  Just specify the environment in
which you want to evaluate them, e.g.

str2expr<-function(x){eval(parse(text=x), envir=parent.frame() )}

Duncan Murdoch

>
> best regards
>
> Elong
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: transfer string to expression

David Winsemius
In reply to this post by Yilong Zhang

On Oct 31, 2010, at 3:22 PM, Yilong Zhang wrote:

> Dear all:
>
> when I use parse() there is some problems. Below is an example:
>
> b0<-1
> b1<-1
> x<-1
> str2expr<-function(x){eval(parse(text=x))}
> test1<-"b0+b1*sqrt(x)"
> test2<-"b0+b1"
> str2expr(test1)
> str2expr(test2)

I don't think the scoping rules are up to the task of keeping the x's  
distinct:

 > b0<-1
 > b1<-1
 > xx<-1
 > str2expr<-function(x){eval(parse(text=x))}
 > test1<-"b0+b1*sqrt(xx)"
 > test2<-"b0+b1"
 > str2expr(test1)
[1] 2
 > str2expr(test2)
[1] 2

>
> it can work well for test2 but not for test1.
>
> Could you tell me how to fix this problem or is there other more  
> stable
> method to transfer an string to expression?

I generally try to keep the variables distinct so my brain can handle  
the various levels. So I would probably not have used x in that  
manner. I think it made the interpreter attempt to take the sqrt of  
"b0+b1*sqrt(x)", which gave you the "Error in sqrt(x) : Non-numeric  
argument to mathematical function"

And apropos is this:
 > fortune("If the answer is parse()")

If the answer is parse() you should usually rethink the question.
    -- Thomas Lumley
       R-help (February 2005)

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: transfer string to expression

Wu Gong
In reply to this post by Duncan Murdoch-2
Hi Duncan:

I'm curious about the environment setting. ?eval says:

"If envir is not specified, then the default is parent.frame() (the environment where the call to eval was made). "

So what's the difference between set envir=parent.frame() or not?

Thank you.

Wu
Reply | Threaded
Open this post in threaded view
|

Re: transfer string to expression

Duncan Murdoch-2
On 31/10/2010 4:47 PM, Wu Gong wrote:
>
> Hi Duncan:
>
> I'm curious about the environment setting. ?eval says:
>
> "If envir is not specified, then the default is parent.frame() (the
> environment where the call to eval was made). "
>
> So what's the difference between set envir=parent.frame() or not?

If you pass parent.frame() as an argument, it will be one level up:
it's the parent of the function that calls eval.  If you leave it at the
default, it's the parent of eval, i.e. the function that calls it.

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: transfer string to expression

Bert Gunter
If it's not already clear, perhaps this slight variation might help
explain what's happening:

b0<-1
b1<-1
x<-1
str2expr<-function(y){eval(parse(text=y))}  ## Note change from "x" to "y" here
test1<-"b0+b1*sqrt(x)"
str2expr(test1)

Cheers,
Bert

On Sun, Oct 31, 2010 at 1:58 PM, Duncan Murdoch
<[hidden email]> wrote:

> On 31/10/2010 4:47 PM, Wu Gong wrote:
>>
>> Hi Duncan:
>>
>> I'm curious about the environment setting. ?eval says:
>>
>> "If envir is not specified, then the default is parent.frame() (the
>> environment where the call to eval was made). "
>>
>> So what's the difference between set envir=parent.frame() or not?
>
> If you pass parent.frame() as an argument, it will be one level up: it's the
> parent of the function that calls eval.  If you leave it at the default,
> it's the parent of eval, i.e. the function that calls it.
>
> Duncan Murdoch
>
> ______________________________________________
> [hidden email] 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.
>



--
Bert Gunter
Genentech Nonclinical Biostatistics

______________________________________________
[hidden email] 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.