I am interested in creating a list of objects in which list elements are automatically named by the name of the input objects. I have two approaches below, but is there a way to convert this to a function for a single step solution for any number of inputs? These approaches can be tedious for many objects.
x <- 1
y <- "A"
# approach 1 ----
z <- list(x = x, y = y)
z
#> $x
#> [1] 1
#>
#> $y
#> [1] "A"
# approach 2 ----
# step 1 - create list
z <- list(x, y)
z
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] "A"
# step 2 - add names to list
z <- setNames(z, c("x", "y"))
z
#> $x
#> [1] 1
#>
#> $y
#> [1] "A"
Maybe you can use mget, which is the opposite of what you asked for. You give it the names of the objects, as characters, and it returns a named list of the objects.