R receives object names from object list


I try to get an object's name from the list containing this object. I searched through similar questions and find some suggestions about using the deparse(substitute(object)) formula:

> my.list <- list(model.product, model.i, model.add)
> lapply(my.list, function(model) deparse(substitute(model)))

and the result is:

[1] "X[[1L]]"

[1] "X[[2L]]"

[1] "X[[3L]]"

whereas I want to obtain:

[1] "model.product", "model.i", "model.add"

Thank you in advance for being of some help!

You can write your own list() function so it behaves like data.frame(), i.e., uses the un-evaluated arg names as entry names:

List <- function(...) {
  names <- as.list(substitute(list(...)))[-1L]
  setNames(list(...), names)

my.list <- List(model.product, model.i, model.add)

Then you can just access the names via: