R/getData.R

#' Get Google trends data and format
#'
#' Get Google trends data and format
#'
#' @param input_keywords A string with keywords comma separated
#' @param date_range Vector of length 2 with the start and end date
#'
#' @return Dataframe
#'
#' @examples
#' \dontrun{
#' test <- getData(
#'               input_keywords = 'brexit'
#'               date_range = c("2018-01-01", "2019-01-01")
#'         )
#'
#'
#' }
#'
#' @export

getData <- function(input_keywords, date_range = c("2015-01-01", Sys.Date())){

  # Format inputs
  date_range = paste0(date_range[[1]], " ", date_range[[2]])
  input_keywords = strsplit(input_keywords, ",")

  input_keywords = lapply(input_keywords, stringr::str_trim)[[1]]

  # Pull data from Gtrends
  res = gtrends(input_keywords,
                geo = c("GB"),
                gprop = "web",
                time = date_range)[[1]]

  # Change column classes
  res$hits <- as.numeric(res$hits)
  res$date <- as.Date(res$date)

  return(res)
}
duncstod/newRPackage documentation built on May 9, 2019, 2:22 a.m.