Search code examples
reval

How do you evaluate variable as option name in R?


I have two variables, as follows:

tokenName = "myToken"
tokenResult = "myResult"

I'd like to set options() where I evaluate the variable tokenName and set that option as myResult.

Essentially I want the equivalent of options(myToken = tokenResult), but using the variable tokenName.

I tried options(eval(tokenName) = tokenResult) but to no avail. Does anyone have any ideas?


Solution

  • We can use setNames

    do.call(options, as.list(setNames(tokenResult, tokenName)))
    

    -checking

    options()[[tokenName]]
    #[1] "myResult"