[R] How to output name of object
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Jun 18 16:15:25 CEST 2014
On 18/06/2014 15:01, Bert Gunter wrote:
> Would
>
> f <- function (...)
> {
> sapply(substitute(list(...)),deparse)[-1]
> }
>
>> f(a1=log(1:4), m1=1:4, c1=pi)
> a1 m1 c1
> "log(1:4)" "1:4" "pi"
>
> be equivalent?
> I find the substitute(...()) construction completely mysterious, while
> substitute(list(...)) makes sense to me.
It would give the same result. If starting from scratch I would
probably have used
f <- function(...) as.character(match.call()[-1])
but that does not give names (if wanted) whereas
f <- function(...) sapply(match.call(), deparse)[-1]
does
My main point was that a for() loop (and extending a logical vector
initialized to NA and then coerced to character) seems rather unnatural.
And seq_along would be clearer if you really must use a loop.
>
> Cheers,
> Bert
>
>
>
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
> (650) 467-7374
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> Clifford Stoll
>
>
>
>
> On Wed, Jun 18, 2014 at 5:41 AM, Prof Brian Ripley
> <ripley at stats.ox.ac.uk> wrote:
>> On 18/06/2014 12:54, Jim Lemon wrote:
>>>
>>> On Wed, 18 Jun 2014 04:24:58 PM yzh lin wrote:
>>>>
>>>> Hi, every R user,
>>>>
>>>> I wanna output names of objects, I googled codes from website,
>>>
>>> but it
>>>>
>>>> was not what I wanted.
>>>>
>>>> the codes are as following:
>>>>
>>>> f <- function (...)
>>>> {
>>>> unevaluatedArgs <- substitute(...())
>>>> evaluatedArgs <- list(...)
>>>> stopifnot(length(unevaluatedArgs) == length(evaluatedArgs))
>>>> tags <- vapply(unevaluatedArgs, FUN=function(x) deparse(x)[1],
>>>> FUN.VALUE=character(1))
>>>> if (!is.null(tmp <- names(evaluatedArgs))) {
>>>> # if argument is tagged, tag=expr, use the tag
>>>> i <- !is.na(tmp) & tmp != ""
>>>> tags[i] <- tmp[i]
>>>> }
>>>> tags
>>>> }
>>>>
>>>> f(a1=log(1:4), m1=1:4, c1=pi)
>>>>
>>>> results:
>>>>>
>>>>> f(a1=log(1:4), m1=1:4, c1=pi)
>>>>
>>>>
>>>> a1 m1 c1
>>>> "a1" "m1" "c1"
>>>>
>>>>
>>>> what I wanted is outputting "log(1:4) " "1:4", "pi" , while not "a1" "m1"
>>>> "c1" .
>>>>
>>>> Coulde someone tell me how to get the results I wanted?
>>>>
>>> Hi Yuanzhen,
>>> This may be of some help:
>>>
>>> f<-function (...) {
>>> unevaluatedArgs <- unlist(substitute(...()))
>>> tags<-NA
>>> for(arg in 1:length(unevaluatedArgs))
>>> tags[arg]<-deparse(unevaluatedArgs[[arg]])
>>> tags
>>> }
>>>
>>> f(a1=log(1:4), m1=1:4, c1=pi)
>>> [1] "log(1:4)" "1:4" "pi"
>>
>>
>> Or simply
>>
>> f <- function(...) sapply(subsitute(...()), deparse)
>>
>> which even keeps the names.
>>
>> --
>> Brian D. Ripley, ripley at stats.ox.ac.uk
>> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford, Tel: +44 1865 272861 (self)
>> 1 South Parks Road, +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK Fax: +44 1865 272595
>>
>> ______________________________________________
>> 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.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list