1

Say I want to write a function that returns the name of the object passed into it.

The below works fine, returning the string "testlist":

testlist<-c("a","b","c")

H<-function(x){  
  name<-deparse(substitute(x)) 
  return(name)
}

H(testlist)

> H(testlist)
[1] "testlist"

However, when using map (or lapply) with the list input:

map(testlist,H)
> map(testlist,H)
[[1]]
[1] ".x[[i]]"

[[2]]
[1] ".x[[i]]"

[[3]]
[1] ".x[[i]]"

What I was hoping to see was:

map(testlist,H)
> map(testlist,H)
[[1]]
[1] "testlist"

[[2]]
[1] "testlist"

[[3]]
[1] "testlist"
rmgriffin
  • 11
  • 1
  • 2
    You may want to pass a named vector with `imap` i.e. `imap(setNames(testlist, rep('testlist', length(testlist))), ~ .y)` – akrun Nov 12 '20 at 22:26
  • 1
    You were expecting to see `testlist` three times? But you are passing different objects to each function call when you use `map` (a subset of `testlist` each time). Functions that depend on variable names being a certain way are very fragile when using functional programming. If you need a label, it's better to make it an optional second parameters that can default to the first variable name, but still makes it possible to change when using the function in more advanced ways. – MrFlick Nov 12 '20 at 22:37
  • @akrun Thanks! I'd like to use the name of the object in the function H, as the actual function is more complicated than the example. – rmgriffin Nov 12 '20 at 22:43
  • @MrFlick Yeah, I can definitely see why it wouldn't work, but have not been able to identify an approach to address the issue. Do you have a quick example of how I might implement your approach? – rmgriffin Nov 12 '20 at 22:50
  • 1
    this basic strategy of using names is described here: https://stackoverflow.com/questions/24309910/how-to-get-name-of-variable-in-r-substitute. Define your function as `H <- function(x, name=deparse(substitute(x))) return(name)` and then just call `map(testlist,H, name="testlist")` – MrFlick Nov 12 '20 at 22:52
  • 1
    Basically the answer is you cannot. The object sent to the functions inside map and `lapply` do not have names. The names are restored after the results have been gathered. So if you want the names (of the components) to be handled by the function you need to pass the names rather than the values and then pull from the list from inside the function. – IRTFM Nov 12 '20 at 22:52
  • @IRTFM thanks for that, guess I'll have to figure out another way to address this. – rmgriffin Nov 12 '20 at 23:02
  • If you have a list of variables and if you need the names of them, you can just convert it to data.frame and take the column names. a = 1 b = 5 c = 4 ll = list(a,b,c) as.data.frame(ll) The result is a b c 1 1 5 4 – ibilgen Nov 12 '20 at 23:02
  • @rmgriffin: You could write a helper function that would store the list name for later access by the main function. – IRTFM Nov 12 '20 at 23:04
  • I don't feel that the redirected question answers this question. Instead I think the answer is "you can't do that but here's an alternative" would have been helpful for future users searching for a similar approach. – Rich Nov 13 '20 at 03:07

0 Answers0