0

That's probably a duplicate, but I couldn't find the answer anywhere...

Anyways, here's my simple problem. I am loading a yaml file in R with users metadata. After parsing the yaml with yaml::yaml.load_file, the list looks like this:

$users
$users$`1`
$users$`1`$user
[1] "Alice"


$users$`2`
$users$`2`$user
[1] "Bob"

I can get the user with id = 1 by:

user_list$users$`1`$user

That returns:

[1] "Alice"

My question is: how can I transform a numeric 1 into this "quoted" version, so I can pass the id as a function argument that retrieves the user I want?

  • 2
    You can't use `$` with variables-in-function-arguments regardless of whether they are numeric or not. Use `[[` to do extraction with variables. `x = 2`, then `user_list$users[[as.character(x)]]$user` – Gregor Thomas Jul 10 '19 at 13:29

1 Answers1

0

Instead of using $, use [[

id <- 1
user_list$users[[as.character(id)]]$user
akrun
  • 874,273
  • 37
  • 540
  • 662