[R] expressions (nesting, substitution, 2-stage evaluation)

Bert Gunter gunter.berton at gene.com
Fri Feb 28 15:53:49 CET 2014


David:

Oh yes! That is certainly the "right" way. Much better than what I suggested.

I actually first thought about doing this, but didn't get the form
right (I wanted to substitute within the expression, rather than
substituting the whole expression).Of course,one can always use
bquote() instead of substitute for a slight streamlining:

for(ii in vectorA)  {
    plot(0:1,0:1)
    title(main = bquote(asdfsadf*~~.(ii) ))
}

## Note that the quotes are unnecessary.

Cheers,
Bert

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Thu, Feb 27, 2014 at 10:20 PM, David Winsemius
<dwinsemius at comcast.net> wrote:
>
> On Feb 27, 2014, at 9:55 PM, Bert Gunter wrote:
>
>> 1. I don't think this is the right way to go about this. I would think
>> about making pieces of your title arguments and assembling them in
>> your call.
>>
>> ... But be that as it may...
>>
>> 2. The problem is that in your loop, ii is already an expression -- a
>> language object. Pasting to it is meaningless.  So you need to deparse
>> it first to a character string and paste to that. Then parse the
>> result:
>>
>> vectorA <- c( quote(TNF-*alpha), quote(IFN-*gamma) )
>>
>> for(ii in vectorA)  {
>>   plot(0:1,0:1)
>>  ex <- paste("abcd*~~",deparse(ii),sep="")
>>   title(main = (parse(text=ex)))
>> }
>>
>>
>> 3. There may well be more elegant ways to do this. But discovering
>> them exceeds my capabilities.
>>
>
> What about just using `substitute`?
>
> vectorA = c( bquote("TNF-"*alpha), bquote("IFN-"*gamma) )
>
> for(ii in vectorA)  {
>    plot(0:1,0:1)
>    title(main = substitute("asdfsadf"*x, list(x=ii)) )
> }
>
> --
> David.
>
>>
>> Cheers,
>> Bert
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>> (650) 467-7374
>>
>> "Data is not information. Information is not knowledge. And knowledge
>> is certainly not wisdom."
>> H. Gilbert Welch
>>
>>
>>
>>
>> On Thu, Feb 27, 2014 at 5:37 PM, Daryl Morris <darylm at uw.edu> wrote:
>>> Hi,
>>>
>>> Both your code and my code work when I don't combine things.  The problem is
>>> when I want to combine an expression (or a bquote in your example) with
>>> something else
>>>
>>> e.g. this doesn't work:
>>>
>>> vectorA = c( bquote("TNF-"*alpha), bquote("IFN-"*gamma) )
>>>
>>> for(ii in vectorA)  {
>>>   plot(0:1,0:1)
>>>   title(main = paste("asdfsadf",ii))
>>> }
>>>
>>> because as soon as I've made an expression, I can no longer append it to
>>> something else.  While in this example I could have had the "asdfsadf" in
>>> the original bquote, there are reasons I need to build the ultimate label at
>>> a separate point than I define the labels (I mix and match things multiple
>>> ways inside the code).
>>>
>>> So, the thing I'm really trying to do is a 2-stage evaluation of an
>>> expression, aka a nested expression evaluation, or a substition of
>>> expressions.  I've tried things like deparse, but so far haven't found the
>>> magic.
>>>
>>> thanks, Daryl
>>>
>>>
>>> On 2/27/14 5:17 PM, David Winsemius wrote:
>>>
>>> On Feb 27, 2014, at 3:17 PM, Bert Gunter wrote:
>>>
>>>> ?plotmath
>>>>
>>>> -- Bert
>>>
>>> Daryl;;
>>>
>>> I think what Bert was hoping you would do was read the plotmath page and
>>> figure it out on your own but that can be a bit tricky when working with
>>> expression object vectors. Here is (perhaps) a step forward:
>>>
>>> vectorA = c( bquote("TNF-"*alpha), bquote("IFN-"*gamma) )
>>>
>>> for(ii in vectorA)  {
>>>  plot(0:1,0:1)
>>>  title(main = ii)
>>> }
>>>
>>> Now as Jim Holtman is fond of saying... what problem were you (really)
>>> trying to solve?
>>>
>>> --
>>> David.
>>>
>>>>
>>>> Bert Gunter
>>>> Genentech Nonclinical Biostatistics
>>>> (650) 467-7374
>>>>
>>>> "Data is not information. Information is not knowledge. And knowledge
>>>> is certainly not wisdom."
>>>> H. Gilbert Welch
>>>>
>>>>
>>>>
>>>>
>>>> On Thu, Feb 27, 2014 at 2:58 PM, Daryl Morris <darylm at uw.edu> wrote:
>>>
>>>>> Hi,
>>>>>
>>>>> I have a function which generates many plots.  To keep it simple, let's
>>>>> say
>>>>> I want to set the main title based on where we are in nested loops.
>>>>>
>>>>> So, something like
>>>>>
>>>>> vectorA = c("a","b","c")
>>>>> vectorB = c("a","b","c")
>>>>>
>>>>> for(ii in vectorA) { for(jj in vectorB) {
>>>>> plot(0:1,0:1)
>>>>> title(main = paste(ii,jj))
>>>>> }
>>>>>
>>>>> that part is easy!   The question is what if I wanted vectorA to be an
>>>>> expression?
>>>>>
>>>>> I'd like to be able to set vectorA =
>>>>> c(expression(paste("TNF-",alpha)),expression(paste("IFN-",gamma))), and
>>>>> have
>>>>> the plot title show the greek letters.
>>>>>
>>>>> Obviously, in the for-loop I could build the expression all at once, but
>>>>> there are lots of programmatic reasons I'd like to be able to have this
>>>>> program structure.  Is there a solution which modifies either/both (1)
>>>>> the
>>>>> setting of main in the loop (2) how I define the vector outside of the
>>>>> loop?
>>>>>
>>>>>
>>>>> thanks, Daryl
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>
>>>>
>>>> ______________________________________________
>>>> 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.
>>>
>>> David Winsemius
>>> Alameda, CA, USA
>>>
>>>
>
> David Winsemius
> Alameda, CA, USA
>




More information about the R-help mailing list