deparseDots to get names of all arguments?

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

deparseDots to get names of all arguments?

Spencer Graves-4
Hi, All:


       How can I get the names of all the arguments in dots(...)?


       I'm able to get the name of the first argument but not the second:



deparseDots <- function(...){
   deparse(substitute(...))
}
a <- 1
b <- 2
deparseDots(a, b)
[1] "a"


       I'd like to get c('a', 'b').


       Thanks,
       Spencer Graves


 > sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

Matrix products: default
BLAS:
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK:
/Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base

loaded via a namespace (and not attached):
[1] compiler_3.4.3 tools_3.4.3    yaml_2.1.16

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: deparseDots to get names of all arguments?

Rolf Turner
On 21/02/18 11:36, Spencer Graves wrote:

> Hi, All:
>
>
>        How can I get the names of all the arguments in dots(...)?
>
>
>        I'm able to get the name of the first argument but not the second:
>
>
>
> deparseDots <- function(...){
>    deparse(substitute(...))
> }
> a <- 1
> b <- 2
> deparseDots(a, b)
> [1] "a"
>
>  >        I'd like to get c('a', 'b').

Does

     names(list(...))

do what you want?

cheers,

Rolf

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: deparseDots to get names of all arguments?

Duncan Murdoch-2
On 20/02/2018 5:47 PM, Rolf Turner wrote:

> On 21/02/18 11:36, Spencer Graves wrote:
>> Hi, All:
>>
>>
>>         How can I get the names of all the arguments in dots(...)?
>>
>>
>>         I'm able to get the name of the first argument but not the second:
>>
>>
>>
>> deparseDots <- function(...){
>>     deparse(substitute(...))
>> }
>> a <- 1
>> b <- 2
>> deparseDots(a, b)
>> [1] "a"
>>
>>   >        I'd like to get c('a', 'b').
>
> Does
>
>       names(list(...))
>
> do what you want?

No, that does what he asked for, not what he wants :-).  Spencer, you
want to deparse all of the expressions in ..., not their names.

I think base R doesn't have a way to do this (but I may be wrong).  You
can do it using some the rlang package.  For example, this seems to work:

deparseDots <- function(...) {
   unname(sapply(rlang::exprs(...), deparse))
}

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: deparseDots to get names of all arguments?

Bert Gunter-2
In reply to this post by Rolf Turner
If you want to avoid the inefficiency and memory overhead of first
constructing the list and work directly on the language, then I think
?match.call is the tool you want. e.g.


> f <- function(...){
    z <- as.list(match.call())[-1]
    vapply(z,deparse,"a")
 }

> a <- 1
> b <- 2

> f(a,b)
[1] "a" "b"

I am sure that there are packages that may do this more elegantly and
perhaps reliably, though.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Tue, Feb 20, 2018 at 2:47 PM, Rolf Turner <[hidden email]>
wrote:

> On 21/02/18 11:36, Spencer Graves wrote:
>
>> Hi, All:
>>
>>
>>        How can I get the names of all the arguments in dots(...)?
>>
>>
>>        I'm able to get the name of the first argument but not the second:
>>
>>
>>
>> deparseDots <- function(...){
>>    deparse(substitute(...))
>> }
>> a <- 1
>> b <- 2
>> deparseDots(a, b)
>> [1] "a"
>>
>>  >        I'd like to get c('a', 'b').
>>
>
> Does
>
>     names(list(...))
>
> do what you want?
>
> cheers,
>
> Rolf
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: deparseDots to get names of all arguments?

Bert Gunter-2
In reply to this post by Duncan Murdoch-2
Duncan et.al:

Referring to my previous suggestion for f:

> f(log(x),x^2)
[1] "log(x)" "x^2"

Is this not what you want?

Cheers,
Bert


On Tue, Feb 20, 2018 at 4:00 PM, Duncan Murdoch <[hidden email]>
wrote:

> On 20/02/2018 5:47 PM, Rolf Turner wrote:
>
>> On 21/02/18 11:36, Spencer Graves wrote:
>>
>>> Hi, All:
>>>
>>>
>>>         How can I get the names of all the arguments in dots(...)?
>>>
>>>
>>>         I'm able to get the name of the first argument but not the
>>> second:
>>>
>>>
>>>
>>> deparseDots <- function(...){
>>>     deparse(substitute(...))
>>> }
>>> a <- 1
>>> b <- 2
>>> deparseDots(a, b)
>>> [1] "a"
>>>
>>>   >        I'd like to get c('a', 'b').
>>>
>>
>> Does
>>
>>       names(list(...))
>>
>> do what you want?
>>
>
> No, that does what he asked for, not what he wants :-).  Spencer, you want
> to deparse all of the expressions in ..., not their names.
>
> I think base R doesn't have a way to do this (but I may be wrong).  You
> can do it using some the rlang package.  For example, this seems to work:
>
> deparseDots <- function(...) {
>   unname(sapply(rlang::exprs(...), deparse))
> }
>
> Duncan Murdoch
>
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: deparseDots to get names of all arguments?

R help mailing list-2
In reply to this post by Spencer Graves-4
Does substitute(...()) do what you want?

> myFunc <- function(x, ...) substitute(...())
> myFunc(y=1/(1:10), x=sin(3:1), z=stop("Oops"), "untagged arg")
$y
1/(1:10)

$z
stop("Oops")

[[3]]
[1] "untagged arg"

> names(.Last.value)
[1] "y" "z" ""


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Feb 20, 2018 at 2:36 PM, Spencer Graves <
[hidden email]> wrote:

> Hi, All:
>
>
>       How can I get the names of all the arguments in dots(...)?
>
>
>       I'm able to get the name of the first argument but not the second:
>
>
>
> deparseDots <- function(...){
>   deparse(substitute(...))
> }
> a <- 1
> b <- 2
> deparseDots(a, b)
> [1] "a"
>
>
>       I'd like to get c('a', 'b').
>
>
>       Thanks,
>       Spencer Graves
>
>
> > sessionInfo()
> R version 3.4.3 (2017-11-30)
> Platform: x86_64-apple-darwin15.6.0 (64-bit)
> Running under: macOS High Sierra 10.13.3
>
> Matrix products: default
> BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/
> Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
> LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/
> libRlapack.dylib
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods base
>
> loaded via a namespace (and not attached):
> [1] compiler_3.4.3 tools_3.4.3    yaml_2.1.16
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: deparseDots to get names of all arguments?

Spencer Graves-4


On 2018-02-20 20:52, William Dunlap wrote:
> Does substitute(...()) do what you want?


       That's the key.  Thanks very much.
       Spencer Graves

>
> > myFunc <- function(x, ...) substitute(...())
> > myFunc(y=1/(1:10), x=sin(3:1), z=stop("Oops"), "untagged arg")
> $y
> 1/(1:10)
>
> $z
> stop("Oops")
>
> [[3]]
> [1] "untagged arg"
>
> > names(.Last.value)
> [1] "y" "z" ""
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
>
> On Tue, Feb 20, 2018 at 2:36 PM, Spencer Graves
> <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi, All:
>
>
>           How can I get the names of all the arguments in dots(...)?
>
>
>           I'm able to get the name of the first argument but not the
>     second:
>
>
>
>     deparseDots <- function(...){
>       deparse(substitute(...))
>     }
>     a <- 1
>     b <- 2
>     deparseDots(a, b)
>     [1] "a"
>
>
>           I'd like to get c('a', 'b').
>
>
>           Thanks,
>           Spencer Graves
>
>
>     > sessionInfo()
>     R version 3.4.3 (2017-11-30)
>     Platform: x86_64-apple-darwin15.6.0 (64-bit)
>     Running under: macOS High Sierra 10.13.3
>
>     Matrix products: default
>     BLAS:
>     /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
>     LAPACK:
>     /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
>
>     locale:
>     [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
>     attached base packages:
>     [1] stats     graphics  grDevices utils     datasets methods base
>
>     loaded via a namespace (and not attached):
>     [1] compiler_3.4.3 tools_3.4.3    yaml_2.1.16
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list --
>     To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>     PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>     and provide commented, minimal, self-contained, reproducible code.
>
>


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.