Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

globalsByName("..2") does not find argument '..2' #88

Closed
HenrikBengtsson opened this issue May 19, 2023 · 1 comment
Closed

globalsByName("..2") does not find argument '..2' #88

HenrikBengtsson opened this issue May 19, 2023 · 1 comment
Labels
Milestone

Comments

@HenrikBengtsson
Copy link
Owner

Issue

Consider:

> f <- function(...) { ..2 }
> f(a = 1, b = 2)
[1] 2

If we attempt to get ..2 using globalsByName(), it does not find that argument;

> f <- function(...) { globals::globalsByName("..2") }

> f(a = 1, b = 2)
$..2
[1] NA
attr(,"class")
[1] "DotDotDotList" "logical"      

attr(,"where")
attr(,"where")$..2
NULL

attr(,"class")
[1] "Globals" "list"   

Troubleshooting

The reason is that the code assumes exists("..2") is TRUE, but that's not the case, e.g.

> f <- function(...) { exists("..2") }
> f(a = 1, b = 2)
[1] FALSE

Solution

The solution is to work with .... Gist:

> f <- function(...) { 
  globals <- globals::globalsByName("...")
  if (is.list(globals[["..."]])) {
    if (length(globals[["..."]]) < 2) stop("the ... list contains fewer than 2 elements")
    args<- globals[["..."]]
    args <- args[2L]
    class(args) <- class(globals[["..."]])
    globals[["..."]] <- args
    globals
  }
}
> f(a = 1, b = 2)
$...
$b
[1] 2

attr(,"class")
[1] "DotDotDotList" "list"         

attr(,"where")
attr(,"where")$...
<environment: R_EmptyEnv>

attr(,"class")
[1] "Globals" "list"   
@HenrikBengtsson HenrikBengtsson added this to the Next release milestone May 19, 2023
@HenrikBengtsson HenrikBengtsson changed the title globalsByName("..2") does find argument '..2' globalsByName("..2") does not find argument '..2' Jun 9, 2023
HenrikBengtsson added a commit that referenced this issue Mar 6, 2024
@HenrikBengtsson
Copy link
Owner Author

Implemented;

> f <- function(...) { globals::globalsByName("..2") }
g <- f(a = 1, b = 2, c = 3)
str(g)
List of 1
 $ ..2:List of 1
  ..$ : num 2
  ..- attr(*, "class")= chr [1:2] "DotDotDotList" "list"
 - attr(*, "where")=List of 1
  ..$ ..2:<environment: 0x557dae0e3d50> 
 - attr(*, "class")= chr [1:2] "Globals" "list"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant