Skip to content

Instantly share code, notes, and snippets.

@jsonbecker
Created October 12, 2015 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsonbecker/e53f1ffe535cae379650 to your computer and use it in GitHub Desktop.
Save jsonbecker/e53f1ffe535cae379650 to your computer and use it in GitHub Desktop.
## Current pattern
function(myList){
do_stuff_with(mylist$attribute1, myList$attribute2)
do_stuff_with(mylist$attribute3, myList$attribute4)
}
## Desired pattern
function(myList){
do_stuff_with(attribute1, attribute2)
do_stuff_with(attribute4, attribute4)
}
@jsonbecker
Copy link
Author

Maybe it's something like:

myFunc <- function(...) {}
myFunc(unlist(myList)){}

@sckott
Copy link

sckott commented Oct 12, 2015

This:

assign_vars <- function(x) {
  for (i in seq_along(x)) {
    assign(names(x)[[i]], x[[i]], envir = parent.frame())
  }
}

foo_bar <- function(x) {
  assign_vars(x)
  print(a)
  print(b)
}

a_list <- list(a = "hello", b = "world")
foo_bar(a_list)

#> [1] "hello"
#> [1] "world"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment