Quantcast

Pasting with Quotes

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Pasting with Quotes

rockclimber112358
Hello useRs!

So, I have a random question.  I'm trying to build a character string, then
evaluate it.  I think an example would be the easiest way to explain:


    kern.vec = c("rbfdot","polydot")
    for( j in 1:length( kern.vec ) )
    {
      formula    = paste("ksvm( ind ~ . ,
data=d.temp[,c(ind_col,dep_cols)], kernel =",kern.vec[j],", prob.model=T )")
      svm    = eval( parse( text=formula ) )
     ...
    }


The problem I always seem to have is that in the formula, I need to have
quotes around "rbfdot" (for example).  But, when I paste the expression
together, it removes the quotes.  Is there a better way to do this (or at
least a way around this problem)?  My method seems a bit kludgy :)

Thanks for all your help!

Josh

        [[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
|  
Report Content as Inappropriate
star

Re: Pasting with Quotes

Michael Weylandt
What's the "big picture" of what you're trying to do? eval(parse(text
= )) is often a less than optimal idea.

Some guesses:

Are you trying to construct a formula object (in the strict sense of
something that you pass to a modeling function)?

Maybe lazy evaluation of the deparse(substitute(x)) flavor might help here?

Michael

On Sat, May 5, 2012 at 2:42 PM, Josh Browning
<[hidden email]> wrote:

> Hello useRs!
>
> So, I have a random question.  I'm trying to build a character string, then
> evaluate it.  I think an example would be the easiest way to explain:
>
>
>    kern.vec = c("rbfdot","polydot")
>    for( j in 1:length( kern.vec ) )
>    {
>      formula    = paste("ksvm( ind ~ . ,
> data=d.temp[,c(ind_col,dep_cols)], kernel =",kern.vec[j],", prob.model=T )")
>      svm    = eval( parse( text=formula ) )
>     ...
>    }
>
>
> The problem I always seem to have is that in the formula, I need to have
> quotes around "rbfdot" (for example).  But, when I paste the expression
> together, it removes the quotes.  Is there a better way to do this (or at
> least a way around this problem)?  My method seems a bit kludgy :)
>
> Thanks for all your help!
>
> Josh
>
>        [[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
|  
Report Content as Inappropriate
star

Re: Pasting with Quotes

David Winsemius
In reply to this post by rockclimber112358

On May 5, 2012, at 2:42 PM, Josh Browning wrote:

> Hello useRs!
>
> So, I have a random question.  I'm trying to build a character  
> string, then
> evaluate it.

Actually you are trying to build a language object , a call or an  
expression. You might have gotten further with:

do.call(ksvm, list(  ... named arguments ...)

>  I think an example would be the easiest way to explain:
>
>
>    kern.vec = c("rbfdot","polydot")
>    for( j in 1:length( kern.vec ) )
>    {
>      formula    = paste("ksvm( ind ~ . ,
> data=d.temp[,c(ind_col,dep_cols)], kernel =",kern.vec[j],",  
> prob.model=T )")
>      svm    = eval( parse( text=formula ) )
>     ...
>    }
>
>
> The problem I always seem to have is that in the formula, I need to  
> have
> quotes around "rbfdot" (for example).  But, when I paste the  
> expression
> together, it removes the quotes.  Is there a better way to do this  
> (or at
> least a way around this problem)?  My method seems a bit kludgy :)

Perhaps looking at either:

?substitute
?bquote

kern.vec = c("rbfdot","polydot")
    for( j in 1:length( kern.vec ) )
    {
      formula    = bquote(expression( ksvm( ind ~ . ,  
data=d.temp[,c(ind_col,dep_cols)], kernel =.(kern.vec[j] ) ,  
prob.model=T ) ))
      print(formula)
    }

expression(ksvm(ind ~ ., data = d.temp[, c(ind_col, dep_cols)],
     kernel = "rbfdot", prob.model = T))
expression(ksvm(ind ~ ., data = d.temp[, c(ind_col, dep_cols)],
     kernel = "polydot", prob.model = T))

Notice that the values for kern.vec are 'character' which is what you  
passed them (and what you seem to be requesting. If you wanted the  
values of those named objects you might try get(<name>). (I'm not a  
user of whatever package has `ksvm` in it, so I'm not aware of whether  
'rbfdot' is supposed to be a character value as a parameter or if  
those are named objects, and I'm not running out to identify the  
package and then to locate a working example to test the eval-result.  
Those are details you should have provided.)
>


--

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
|  
Report Content as Inappropriate
star

Re: Pasting with Quotes

rockclimber112358
Thank you both so much for your help!  I ended up using
bquote(expression(...)), and it's working perfectly!

On Sat, May 5, 2012 at 1:05 PM, David Winsemius <[hidden email]>wrote:

>
> On May 5, 2012, at 2:42 PM, Josh Browning wrote:
>
>  Hello useRs!
>>
>> So, I have a random question.  I'm trying to build a character string,
>> then
>> evaluate it.
>>
>
> Actually you are trying to build a language object , a call or an
> expression. You might have gotten further with:
>
> do.call(ksvm, list(  ... named arguments ...)
>
>
>   I think an example would be the easiest way to explain:
>>
>>
>>   kern.vec = c("rbfdot","polydot")
>>   for( j in 1:length( kern.vec ) )
>>   {
>>     formula    = paste("ksvm( ind ~ . ,
>> data=d.temp[,c(ind_col,dep_**cols)], kernel =",kern.vec[j],",
>> prob.model=T )")
>>     svm    = eval( parse( text=formula ) )
>>    ...
>>   }
>>
>>
>> The problem I always seem to have is that in the formula, I need to have
>> quotes around "rbfdot" (for example).  But, when I paste the expression
>> together, it removes the quotes.  Is there a better way to do this (or at
>> least a way around this problem)?  My method seems a bit kludgy :)
>>
>
> Perhaps looking at either:
>
> ?substitute
> ?bquote
>
>
> kern.vec = c("rbfdot","polydot")
>   for( j in 1:length( kern.vec ) )
>   {
>     formula    = bquote(expression( ksvm( ind ~ . ,
> data=d.temp[,c(ind_col,dep_**cols)], kernel =.(kern.vec[j] ) ,
> prob.model=T ) ))
>     print(formula)
>   }
>
> expression(ksvm(ind ~ ., data = d.temp[, c(ind_col, dep_cols)],
>    kernel = "rbfdot", prob.model = T))
> expression(ksvm(ind ~ ., data = d.temp[, c(ind_col, dep_cols)],
>    kernel = "polydot", prob.model = T))
>
> Notice that the values for kern.vec are 'character' which is what you
> passed them (and what you seem to be requesting. If you wanted the values
> of those named objects you might try get(<name>). (I'm not a user of
> whatever package has `ksvm` in it, so I'm not aware of whether 'rbfdot' is
> supposed to be a character value as a parameter or if those are named
> objects, and I'm not running out to identify the package and then to locate
> a working example to test the eval-result. Those are details you should
> have provided.)
>
>>
>>
>
> --
>
> David Winsemius, MD
> West Hartford, CT
>
>

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