Ignore error when importing JSON files in R

若如初见. 提交于 2021-01-29 11:39:22

问题


I have this for loop that download a json file from a solr search server. It loops over a vector that contain keywords (100, in this case):

library(jsonlite)
for (i in 1:100) {
  docs <- fromJSON(paste("http://myurl.com/solr/select?df=topic&fq=",keywords[i],"&indent=on&q=*:*&rows=1&wt=json",sep=""))
  numFound <- docs$response$numFound
  print(numFound)
}

It works fine, until it reaches a certain keyword that is not found on the solr, and returns this error:

Error in open.connection(con, "rb") : HTTP error 400.

And then the loop stops.

Is there a way to ignore the error and proceed the loop?

I've read something using tryCatch but still couldn't figure it out.


回答1:


Simpler than tryCatch, you can use the function try inside your keyword loop. This will attempt to load the URL, but if an error is encountered will print the error but continue to the next keyword.

library(jsonlite)
for (i in 1:100) {
  try({
    docs <- fromJSON(paste("http://myurl.com/solr/select?df=topic&fq=",keywords[i],"&indent=on&q=*:*&rows=1&wt=json",sep=""))
    numFound <- docs$response$numFound
    print(numFound)
  })
}

If you also don't want to have the errors printed, specify silent = TRUE:

library(jsonlite)
for (i in 1:100) {
  try({
    docs <- fromJSON(paste("http://myurl.com/solr/select?df=topic&fq=",keywords[i],"&indent=on&q=*:*&rows=1&wt=json",sep=""))
    numFound <- docs$response$numFound
    print(numFound)
  }, silent = TRUE)
}



回答2:


I'm partial to purrr's safely for this kind of task, which works well in purrr's map functions. You can test it by getting JSONs from GitHub's API:

keywords <- c("hadley", "gershomtripp", "lsjdflkaj")
url <- "https://api.github.com/users/{.}/repos"

Now we can get the JSONs and extract the repo IDs

library(jsonlite)
library(purrr)
library(glue)

json_list <- map(keywords, safely(~ fromJSON(glue(url)) %>% .$id))

This will return a list of elements containing result and error. If there was an error it will be saved in error, otherwise the results will be save in result.

[[1]]
[[1]]$result
 [1]  40423928  40544418  14984909  12241750   5154874   9324319  20228011     82348    888200   3116998
[11]   8296284 137344416 133734429   2788278  28724058   9470424 116708612  34325557     41144     41157
[21]  78543290  66588778  35225488  14507273  15718805  18562209     12522 115742443 119107571    201908

[[1]]$error
NULL


[[2]]
[[2]]$result
 [1] 150995700 141743224 127107806 130802586 185857872 131488780 148619375 165221804 135417803 127116088
[11] 181662388 173351888 127131146 136896011

[[2]]$error
NULL


[[3]]
[[3]]$result
NULL

[[3]]$error
<simpleError in open.connection(con, "rb"): HTTP error 404.>


来源:https://stackoverflow.com/questions/56892518/ignore-error-when-importing-json-files-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!