Search code examples
reval

How do you add an environment option with an evaluated name in R?


I created a google token (R6 object of classes Token2.0, Token) to so I can interact with the YouTube Analytics API as follows:

google_token <- httr::oauth2.0_token(httr::oauth_endpoints("google"),
                                     httr::oauth_app("google", appId, appSecret),
                                     scope = c("https://www.googleapis.com/auth/yt-analytics.readonly"))

I also have a variable Account <- "myChannel". Similar to a previous question I posted here I would like to evaluate Account and assign the value to be the content of google_token. For example, options(myChannel, google_token) works, and running getOption("myChannel") shows the token, but because I will generate various tokens and want to create various options, I want to use Account. I would hope something like the following works:

options(eval(Account) = google_token

# OR

do.call(options, as.list(setNames(google_token, Account)

Neither of which work. Any suggestions?


Solution

  • I was able to figure this out by assigning the option value to a list, naming the list whatever I wanted it to be in the option, and then assigning the option, as follows:

    tokenOption <- list(google_token)
    names(tokenOption) <- Account
    options(tokenOption)
    
    getOption("myChannel")
    > #Gives me the results of my google_token