将列表中的所有项目写入来自 GET httr 请求的字符向量

Write all items from list to a character vector from GET httr request

我正在尝试从 GET 请求中提取数据并按如下方式解析它:

# Function to get details from all experiments
getData<- function()
{
  server_url <- "http://127.0.0.1:5000/experiments"
  r <- GET(url = server_url, verbose())
  return(r)
}

# Get all experiments
x <- getData()

# Retrieve content and parse for processing
y <- content(x, "parsed")

输出如下,即包含所有 17 个实验的单个列表。

> y
$experiments
$experiments[[1]]
$experiments[[1]]$end
NULL

$experiments[[1]]$id
[1] 1

$experiments[[1]]$name
[1] "reprehenderit"

$experiments[[1]]$start
[1] "Mon, 30 Mar 2015 17:29:13 GMT"


$experiments[[2]]
$experiments[[2]]$end
NULL

$experiments[[2]]$id
[1] 2

$experiments[[2]]$name
[1] "explicabo"

$experiments[[2]]$start
[1] "Mon, 30 Mar 2015 17:29:44 GMT"

.......

$experiments[[17]]
$experiments[[17]]$end
NULL

$experiments[[17]]$id
[1] 17

$experiments[[17]]$name
[1] "dsagfsdzg"

$experiments[[17]]$start
[1] "Wed, 01 Apr 2015 01:45:01 GMT"

我想从此元素列表中提取 17 "name" 个元素到一个字符向量中(已为前 2 个元素手动完成)。

z <- c(unlist(y[[1]][1])["name"], unlist(y[[1]][2])["name"])
z

           name            name 
"reprehenderit"     "explicabo" 

谁能帮我把上面的自动化?有没有一种简单的方法可以从 GET 请求中提取它?

根据上面的代码,您可以在 y[[1]] 上使用 sapply,它是一个列表向量:

> names <- sapply(y[[1]], function(x) x["name"])
> names
[1] "reprehenderit" "explicabo" ... "dsagfsdzg"