我正在尝试从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”元素到一个字符向量中(为前两个元素手动完成)。
z <- c(unlist(y[[1]][1])["name"], unlist(y[[1]][2])["name"])
z
name name
"reprehenderit" "explicabo"
有人可以帮我自动化上述内容吗?有没有一种简单的方法可以从GET请求中提取它?
答案 0 :(得分:0)
根据上面的代码,您可以sapply
使用y[[1]]
,这是列表的向量:
> names <- sapply(y[[1]], function(x) x["name"])
> names
[1] "reprehenderit" "explicabo" ... "dsagfsdzg"