[R] paste an unevaluated expression
Lorenzo Cattarino
l.cattarino at uq.edu.au
Mon Oct 18 09:12:32 CEST 2010
Thanks for your reply. However, I have only told you half of the story.
My intention was to create a function that outputs 10 different .RData
file. These output files need to contain similar command lines (one
"apply" and one "save" command) but with slightly different arguments.
This is what I wrote:
Start <- 1
End <- 120
for (i in 1:10)
{
object.result <- paste('Result', i, sep="")
section <- paste('apply', '(', 'exp.des[', Start, ':', End, ',], 1,
one.row, parms=parameters)')
Apply <- paste(object.result, section, sep=" <- ")
Save <- format(substitute(save(result1,
file='/home/uqlcatta/pbs/result1.RData')))
path <- '//atlas2/Research/Lorenzo Cattarino/PhD/myR/R_laboratory/'
output <- paste(path, object.result, sep = "")
write(paste(Apply,Save), paste(output, 'R', sep = "."))
Start <- Start + 120
End <- End + 120
}
As you can see, apart from an extremely repetitive use of "paste", the
function works. However there are several problems:
1. I could not find the way to change the arguments of the Save object
(e.g. result1 and .../result1.RData for first output file; result2 and
.../result2.RData for second output, and so forth)
2. I did not know how to put together the Apply and Save objects in the
final command, in a way that resemble two executable lines (each on a
different line, and not one behind each other on the same line, like
they are now)
3. I was also wondering about a more elegant way to write the function
(which looks sort of inefficient to me???).
Thanks very much for your help
Lorenzo
-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Sent: Monday, 18 October 2010 1:38 PM
To: Lorenzo Cattarino
Cc: r-help at r-project.org
Subject: Re: [R] paste an unevaluated expression
On Sun, Oct 17, 2010 at 9:14 PM, Lorenzo Cattarino
<l.cattarino at uq.edu.au> wrote:
> Hi R-users,
>
>
>
> I would like to create an expression without evaluating it. Then paste
> that expression to an object. Example:
>
>
>
> Result <- paste('Result', 1, sep="")
>
> paste(Result, substitute(apply(exp.des[1:10,], 1, one.row,
> parms=parameters)), sep="<-")
>
>
>
> However this pastes EACH element of the unevaluated expression.
Instead
> I just would like the expression to be a character string, with just
ONE
> element.
Try this:
s <- substitute(...whatever...)
paste(Result, format(s), sep = "<-")
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list