0

We can assign a value to a single object using that object's name - assign("x", 1) - and we can efficiently assign different values to multiple object thanks to the zeallot package - c(x, y) %<-% c(1, 2) - but can we do both? I basically just want to do c("x", "y") %<-% c(1, 2) and I can only think to do it in this lovely way:

invisible(mapply(function(i, j) assign(i, j, envir = .GlobalEnv), i = c("x", "y"), j = c(1, 2)))

Is there any better way?

DHW
  • 1,157
  • 1
  • 9
  • 24
  • 5
    just do `list2env(setNames(as.list(c(1,2)),c("x","y")),.GlobalEnv)` or even `list2env(as.list(setNames(c(1,2),c("x","y"))),.GlobalEnv)` – Onyambu Aug 17 '18 at 00:31
  • 4
    "Is there a better way?" - Can I ask why you're creating multiple free-floating objects in your global environment? I've seen many people here go down that route, and it almost always ends up coming back to making a list instead as `assign/get` becomes a nightmare eventually. – thelatemail Aug 17 '18 at 00:56
  • @Onyambu Thanks, that's pretty much the best I could have hoped for; I see that `list2env`'s help even says it can be used to "'multi-assign' from x into a pre-existing environment." Make that an answer and I'll accept it. @thelatemail I agree, though I suspect that some people are constrained to do it this way for various reasons, e.g. I'm looking to improving a colleague's code, they have these free-floating objects, and I just want to change one thing at a time here. Also, I mean, there IS a whole package just to create a multi-assign operator. And, Chollet uses it in Deep Learning with R. – DHW Aug 17 '18 at 03:07
  • I agree with @thelatemail that this is a bad practice, `put all the free-floating objects in a list` is more environment friendly and are recommended by FDA. – TC Zhang Aug 17 '18 at 03:26
  • 'I see that list2env's help even says it can be used to "'multi-assign' from x into a pre-existing environment."' Note that this function was never intended for assignment into the global environment. That's a misuse. – Roland Aug 17 '18 at 06:09
  • @Roland You say to note that like the help file says that. I see nothing at all to suggest that this isn't one of the intended uses. – DHW Aug 17 '18 at 11:18
  • @DHW I remember R-core's Martin Mächler, the author himself, expressing this. Unfortunately, I can't find it right now. – Roland Aug 17 '18 at 12:02
  • @Roland Good enough for me :) Thanks – DHW Aug 17 '18 at 12:11
  • @DHW: Looks like you have your answer, but I still don't understand the question. Could you clarify the purpose of this exercise? It's very abstract in its present form, and it would be helpful to know how you plan on applying it. – Nettle Aug 18 '18 at 22:46
  • @Nettle I don't know how to better phrase the question; my title said what I was looking for, and it's what 'list2env' does, which I now know. By "better way," I meant a more concise, and presumably quicker, way, compared to the best 'apply'-based method I could write, which I provided. I don't want to get too deep into why I'm storing objects in the global environment here, as that's very much a separate question and I don't want to conflate the two. – DHW Aug 19 '18 at 13:11
  • @DHW: The question was clear. I'm just trying to figure out what it's for. Apparently others know, though, so I'll delete these comments to avoid distracting. – Nettle Aug 19 '18 at 15:24

1 Answers1

1

You can list named elements of a list to an environment:

list2env(setNames(as.list(c(1,2)),c("x","y")),.GlobalEnv)
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • I've marked this as the right answer, as it's probably still the best way and seems to work great, but note the above comments - it's apparently an unintended usage of `list2env`. – DHW Aug 17 '18 at 12:13