Is it possible to check an error of a function with an if()?

Asked

Viewed 53 times

2

I’m using the package gtrends to pull some data from google..

This package allows you to download 5 Keywords at a time. That’s why I’m using a for()

However, some Keywords I need to query are not in the period I seek.

When the function gtrends() cannot find the word, it cannot find the error. Examples of Keywords with error: DDD, MMM.

So I’d like to know: is there a way to create a if(), in case of error in the function, it allocate the keyword in a list and, if not, do the rest of the procedure?

library(readr)
library(gtrendsR)


kwlist = readLines("keywords.csv")


resultslist <- list() 

for (keywords in kwlist){
  
  country=c('IT')
  time=("2018-08-01 2018-08-27")
  channel='web'
  trends = gtrends(keywords, gprop =channel,geo=country, time = time )
  resultslist[[keywords]] <- trends$interest_over_time
  
}

The mistake is this:

Error in [<-.data.frame(*tmp*, , timevar, value = "Subject") means: Placement has 1 Row, data has 0

I was told that in older versions of the package, the error does not appear. But I was curious to know if() can be created for an error.

  • 1

    To capture (and process) errors there are functions try and tryCatch. The latter is more flexible. Can you give an example of words that work and others that fail? Enough to process to the end, if possible.

  • Hello! Thank you for the reply! keywords <- c("FLWS","TXG","RETC","YSGG","EFSH","ONEM","ISOL","FNRC","TWGL","TWOU","TDCH","MRJT","DPSM","THDS","DDDX","MMM","FTPM","CATV","FFNT","SSOF","AOS") . When it reaches the MMM of the error. Then, the next error is in AOS.

1 answer

3


The following code reads the keyword array and creates two lists, a list of results and a list of errors.

library(readr)
library(gtrendsR)

keywords <- c("FLWS","TXG","RETC","YSGG","EFSH","ONEM","ISOL",
              "FNRC","TWGL","TWOU","TDCH","MRJT","DPSM","THDS",
              "DDDX","MMM","FTPM","CATV","FFNT","SSOF","AOS")

resultslist <- vector("list", length = length(keywords))
errorslist <- vector("list", length = length(keywords))
names(resultslist) <- keywords
names(errorslist) <- keywords

country <- 'IT'
time <- "2018-08-01 2018-08-27"
channel <- 'web'

for(keyword in keywords){
  cat("Processando:", keyword, "\n")
  flush.console()
  trends <- tryCatch(gtrends(keyword, gprop = channel, geo = country, time = time),
                     warning = function(w) warning(w),
                     error = function(e) e
                    )
  if(inherits(trends, "error")){
    errorslist[[keyword]] <- trends
  } else {
    resultslist[[keyword]] <- trends$interest_over_time
  }
}

resultslist <- resultslist[!sapply(resultslist, is.null)]
errorslist <- errorslist[!sapply(errorslist, is.null)]

errorslist
#$MMM
#<simpleError in `[<-.data.frame`(`*tmp*`, , timevar, value = "subject"): replacement has 1 row, data has 0>
#
#$AOS
#<simpleError in `[<-.data.frame`(`*tmp*`, , timevar, value = "subject"): replacement has 1 row, data has 0>
  • Man, thanks! If I want to save a list of kwlist that gave error, would keyerrorslist[[KeyErrors]] <- keyword?

  • 1

    @Rxt See now. The names of the list members are assigned before the cycle and if you want you can do errorslist$MMM. Still, if you don’t want to see the "Processando: ..." just remove these two lines (cat and flush.console), have no importance for the rest.

  • Perfect! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.