我正在尝试创建一个函数,该函数将基于输入:test_doi查询上述API(baseurl)。如果该网址返回200,该代码就可以正常工作,但是一旦该网址返回404,我就会收到以下错误消息。
is.url(url)中的错误:length(url)== 1不正确
我的代码:
library(jsonlite)
library(httr)
call_dim <- function(x) {
baseurl <- "https://metrics-api.dimensions.ai/doi/"
url <- paste0(baseurl, x)
resp <- GET(url)
if(status_code(resp) == 404){
stop("NA", call. = FALSE)
} else {
pages_t <- fromJSON(paste0(baseurl,x))
lapply(pages_t, function(z) { z[ lengths(z) == 0 ] <- NA; z; })
}
}
test_doi <- c("10.3390/w10111643" ,"10.1371/journal.pone.0007108")
view <- call_dim(test_doi)
谢谢!
答案 0 :(得分:0)
编写您的函数的目的是一次获取一个URL。您需要使用Apply系列功能之一来解决这个问题。对于您而言,我修改了函数以返回请求的内容,以便更轻松地调试正在发生的事情并使用sapply
。
library(jsonlite)
library(httr)
call_dim <- function(x) {
baseurl <- "https://metrics-api.dimensions.ai/doi/"
url <- paste0(baseurl, x)
resp <- GET(url)
if(status_code(resp) == 404){
return(content(resp))
} else {
pages_t <- fromJSON(paste0(baseurl,x))
lapply(pages_t, function(z) { z[ lengths(z) == 0 ] <- NA; z; })
}
}
test_doi <- c("10.3390/w10111643" ,"10.1371/journal.pone.0007108")
view <- sapply(test_doi, call_dim)
> str(view)
List of 2
$ 10.3390/w10111643 : chr "404: Not Found"
$ 10.1371/journal.pone.0007108:List of 9
..$ doi : chr "10.1371/journal.pone.0007108"
..$ times_cited : int 17
..$ recent_citations : int 1
..$ highly_cited_1 : logi FALSE
..$ highly_cited_5 : logi FALSE
..$ highly_cited_10 : logi FALSE
..$ relative_citation_ratio: num 0.63
..$ field_citation_ratio : num 2.39
..$ license : chr "This data has been sourced via the Dimensions Metrics API, use of which is subject to the terms at https://dime"| __truncated__