The gtrendsR package

The gtrendsR package provides a way to access Google Trends from R. It is useful to make searches reproducible, but do not make many calls in a short period of time because Google will block you. Always save the data as soon as you got it.

Installing the package can be done like other packages, but since the Google Trends API changes all the time, it is better to install the latest github version with devtools:

# instead of install.packages("gtrendsR")
install.packages("devtools")  # pay attention at possible software you might have to install for devtools
devtools::install_github("PMassicotte/gtrendsR")

And loading it as well:

library(gtrendsR)

Its main fuction is call gtrends, which allows you to query Google Trends automatically. Take a look to the documentation of the function with this command:

?gtrends

Among its parameters, four are important for us:

For example, we can search for the terms “2013” and “2015” from all over the world including low search volume regions and on the year 2014:

result <- gtrends(keyword = c("2013","2015"), geo = "", time="2014-01-01 2014-12-31", low_search_volume = T)

The result is an object with various data frames. For example, interest_over_time contains the time series with the volume on its column “hits”:

head(result$interest_over_time)
##         date hits keyword   geo                  time gprop category
## 1 2013-12-29   78    2013 world 2014-01-01 2014-12-31   web        0
## 2 2014-01-05   53    2013 world 2014-01-01 2014-12-31   web        0
## 3 2014-01-12   48    2013 world 2014-01-01 2014-12-31   web        0
## 4 2014-01-19   42    2013 world 2014-01-01 2014-12-31   web        0
## 5 2014-01-26   40    2013 world 2014-01-01 2014-12-31   web        0
## 6 2014-02-02   38    2013 world 2014-01-01 2014-12-31   web        0

And interest_by_country contains the volume across countries:

head(result$interest_by_country)
##          location hits keyword   geo gprop
## 1         Algeria  100    2013 world   web
## 2         Moldova   92    2013 world   web
## 3         Armenia   88    2013 world   web
## 4        Pakistan   85    2013 world   web
## 5 Wallis & Futuna   74    2013 world   web
## 6      Kazakhstan   69    2013 world   web