[英]Retrieving google scholar ID within a for function
I've got a list of scholars for parse their available Google scholar ID's.我有一个学者列表,用于解析他们可用的 Google 学者 ID。 Somehow I obtain the error of "subscript out of bounds", but I can't relate to other responses provided with this issue.
不知何故,我得到了“下标越界”的错误,但我无法与此问题提供的其他响应相关联。 Code as follows.
代码如下。 Thanks::
谢谢::
library(scholar)
for (i in 1:200){
scholars<-get_scholar_id(last_name = list$Last.Name[i],
first_name = list$First.Name[i],
affiliation = "ABC University")
}
This results in:这导致:
No Scholar ID found.
No Scholar ID found.
Error in tables[[1]] : subscript out of bound
However, if I do put:但是,如果我确实提出:
scholars_B<-get_scholar_id(last_name = list$Last.Name[3],
first_name = list$First.Name[3],
affiliation = "ABC University")
I obtain the Google scholar ID without any issues.我获得了 Google 学者 ID,没有任何问题。
Reproducible example:可重现的例子:
# list from top h-index researchers (just for the purpose of this sample)
Last.Name <- c("Colditz", "Lander", "Akira", "Langer", "Karin")
First.Name <- c("Graham", "Eric", "Shizuo", "Robert", "Michael")
df <- data.frame(Last.Name, First.Name)
print (df)
library(scholar)
#First try:
for(i in 1:3){
scholars<-get_scholar_id(last_name = df$Last.Name[i],
first_name = df$First.Name[i])
}
#Error: Error in tables[[1]] : subscript out of bounds
#Suggestion by @akrun
for(i in seq_along(df$Last.Name)){
scholars<-get_scholar_id(last_name = df$Last.Name[i],
first_name = df$First.Name[i])
}
#Error: Error in tables[[1]] : subscript out of bounds
#This way works, but not with the for function:
scholars<-get_scholar_id(last_name = df$Last.Name[3],
first_name = df$First.Name[3])
print(scholars)
Some elements doesn't have the scholar id and it is returning with an error.某些元素没有学者 ID,并且返回错误。 An option is to bypass those cases with a
tryCatch
or possibly
(from purrr
)一种选择是使用
tryCatch
或possibly
(来自purrr
)绕过这些情况
library(purrr)
library(scholar)
p_get_scholar_id <- possibly(get_scholar_id, otherwise = NA_character_)
scholars <- character(nrow(df))
for(i in seq_along(scholars)) {
scholars[i] <- p_get_scholar_id(last_name = df$Last.Name[i],
first_name = df$First.Name[i])
}
-output -输出
scholars
#[1] NA "LXVfPc8AAAAJ" "0TG2laoAAAAJ" NA "xVvyb1gAAAAJ"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.