0

I am trying to load a few data with a nested-loop using pageviews. You already helped me get this result:

library("pageviews")

lang = c("it.wikipedia")
bm = c("ECB","Christine Lagarde")

x <- list(
    list(),
    list(),
    list(),
    list(),
    list()
    ) # store results

for (i in seq_along(lang)) {
  for (j in seq_along(bm)) {
        
x[[i]][[j]] = article_pageviews(project = lang[i], article = bm[j], platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily")
  
  }
}

The last step I need to do, however, involves reading some article for which project doesn't exist. Find an example below:

lang = c("it.wikipedia")
bm = c("Philip Lane")

    
x = article_pageviews(project = lang, article = bm, platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily")
 
# Error in FUN(X[[i]], ...) : 
#  The date(s) you used are valid, but we either do not have data for those date(s), or the project you asked for is not loaded yet.  Please check https://wikimedia.org/api/rest_v1/?doc for more information.

I would like to add this to the loop. I tried a few solutions but I don't manage to make the loop skip over if there is an error. I post below one mistaken attempt:

lang = c("it.wikipedia")
bm = c("ECB", "Christine Lagarde", "Philip Lane")

for (i in seq_along(lang)) {
  for (j in seq_along(bm)) {
    
  skip_to_next <- FALSE
    
  tryCatch(x[[i]][[j]] = article_pageviews(project = lang[i], article = bm[j], platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily"), error = function(e) {skip_to_next <<- TRUE})
  
    if(skip_to_next) { next }     

  }
}


Can anyone help me run the loop and skip whenever it meets an error?

Thanks a lot!

1 Answer 1

1

You can use tryCatch as :

library(pageviews)
library(purrr)

lang = c("it.wikipedia")
bm = c("ECB", "Christine Lagarde", "Philip Lane")

map_df(lang, function(x) map_df(bm, function(y) 
  tryCatch(article_pageviews(project = x, article = y, platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily"), 
           error = function(e) {}))) -> result
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.