Giovanni Colitti
Giovanni Colitti

Reputation: 2344

How to pass a named argument stored in a string to list() in R

If I have a string like:

s <- "x = 1"

how do I pass this to list() and end up with a list with one element named "x" and equal to 1. Like this:

list(x = 1)

I tried:


list(eval(str2lang(s)))

I know one way to do this:


s2 <- strsplit(s, " = ")[[1]]

stats::setNames(list(s2[2]), s2[1])

I don't like the above solution, however, because it produces double and singles quotes if the value being assigned inside the list is already quoted:

s <- "x = 'a'"
s2 <- strsplit(s, " = ")[[1]]

lst1 <- stats::setNames(list(s2[2]), s2[1])

lst2 <- list(x = 'a')

all.equal(lst1, lst2)

Update

A number of solutions have been offered, but none seem to do quite what I had in mind when I say "how do I pass [a string] to list()".

This is what I mean:

list(x = 'z', y = 1, some_function("x = 'a'"))

where some_function can take a string, parse the expression inside, and then have it evaluated as if it were just another named object being passed to list().

Upvotes: 1

Views: 76

Answers (2)

Onyambu
Onyambu

Reputation: 79238

f <- function(string){
  a <- str2lang(string)
  as.list(setNames(a[[3]], as.character(a[[2]])))
}

s1 <- "x = 'a'"
s <- "x = 1"

f(s)
#> $x
#> [1] 1
f(s1)
#> $x
#> [1] "a"

Created on 2023-02-09 with reprex v2.0.2

Using eval, you could do:

 eval(str2lang(sprintf('list(%s)',s)))

Upvotes: 2

Ric
Ric

Reputation: 5722

s = "x = 1"

env <- new.env(parent = emptyenv())
env[["list"]] <- base::list

eval(str2lang(paste0("list(", s, ")")), env)
#> $x
#> [1] 1

The empty env environment is in order to prevent running code other than "list".

Upvotes: 1

Related Questions