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.
To capture (and process) errors there are functions
try
andtryCatch
. 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.– Rui Barradas
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.– RxT