## ----include = FALSE, eval = FALSE--------------------------------------------
# knitr::opts_chunk$set(
#   collapse = TRUE,
#   comment = "#>",
#   eval = FALSE,
#   purl = FALSE
# )

## ----setup, eval = FALSE------------------------------------------------------
# # load package -----------------------------------------------------------------
# library(globaltrends)

## ----eval = FALSE-------------------------------------------------------------
# # initialize_db ----------------------------------------------------------------
# setwd("your/globaltrends/folder")
# initialize_db()
# #> Database files created successfully under 'db/'.

## ----eval = FALSE-------------------------------------------------------------
# # start_db ---------------------------------------------------------------------
# setwd("your/globaltrends/folder")
# start_db()
# #> Successfully loaded database and exported table handles to gt.env.

## ----eval = FALSE-------------------------------------------------------------
# # disconnect_db ----------------------------------------------------------------
# disconnect_db()
# #> Successfully disconnected and persisted database to 'db/'.

## ----eval = FALSE-------------------------------------------------------------
# # initialize python ------------------------------------------------------------
# initialize_python(
#   api_key = Sys.getenv("GOOGLE_API_KEY"), # Google Trends API key
#   conda_env = Sys.getenv("CONDA_ENV"), # Location of conda environment OR
#   python_env = Sys.getenv("PYTHON_ENV") # Location of Python environment
# )

## ----eval = FALSE-------------------------------------------------------------
# batch_object <- read_rds("data/batch_object.rds")
# 
# i <- 1
# while (get_api_usage()[2] >= 100 & i <= max(batch_object)) {
#   download_object(object = i, control = 1)
#   download_object_global(object = i, control = 1)
#   i <- i + 1
# }

## ----eval = FALSE-------------------------------------------------------------
# # add_control_keyword ----------------------------------------------------------
# new_control <- add_control_keyword(
#   keyword = c("gmail", "maps", "translate", "wikipedia", "youtube"),
#   start_date = "2010-01",
#   end_date = "2020-12"
# )
# #> Successfully created new control batch 1 (gmail, maps, translate, wikipedia, youtube, 2010-01-2020-12).

## ----eval = FALSE-------------------------------------------------------------
# # keywords_control and dplyr interaction ---------------------------------------
# dplyr::filter(gt.env$keywords_control, keyword == "gmail")
# #> # A tibble: 1 x 2
# #>   batch keyword
# #>   <int> <chr>
# #> 1     1 gmail

## ----eval = FALSE-------------------------------------------------------------
# # download_control -------------------------------------------------------------
# download_control(control = new_control, locations = countries)
# #> Successfully downloaded control data | control: 1 | location: US [1/66]
# #> ...
# #> Successfully downloaded control data | control: 1 | location: DO [66/66]

## ----eval = FALSE-------------------------------------------------------------
# # download_control_global ------------------------------------------------------
# download_control_global(control = new_control)
# #> Successfully downloaded control data | control: 1 | location: world [1/1]

## ----eval = FALSE-------------------------------------------------------------
# # add_object_keyword -----------------------------------------------------------
# new_object <- add_object_keyword(
#   keyword = list(
#     c("coca cola", "facebook", "microsoft"),
#     c("alaska air group", "illinois tool works", "jm smucker")
#   ),
#   start_date = "2010-01",
#   end_date = "2020-12"
# )
# #> Successfully created new object batch 1 (coca cola, facebook, microsoft, 2010-01-2020-12).
# #> Successfully created new object batch 2 (alaska air group, illinois tool works, jm smucker, 2010-01-2020-12).

## ----eval = FALSE-------------------------------------------------------------
# # keywords_object and dplyr interaction ----------------------------------------
# dplyr::filter(gt.env$keywords_object, keyword == "coca cola")
# #> # A tibble: 1 x 2
# #>   batch keyword
# #>   <int> <chr>
# #> 1     1 coca cola

## ----eval = FALSE-------------------------------------------------------------
# # download_object --------------------------------------------------------------
# download_object(object = new_object, locations = countries)
# #> Successfully downloaded object data | object: 1 | location: US [1/66]
# #> ...
# #> Successfully downloaded object data | object: 2 | location: DO [66/66]

## ----eval = FALSE-------------------------------------------------------------
# # download_object_global -------------------------------------------------------
# download_object_global(object = new_object)
# #> Successfully downloaded object data | object: 1 | location: world [1/1]
# #> Successfully downloaded object data | object: 2 | location: world [1/1]

## ----eval = FALSE-------------------------------------------------------------
# # compute_score ----------------------------------------------------------------
# compute_score(object = new_object, control = new_control[[1]], locations = countries)
# #> Successfully computed search score | control: 1 | object: 1 | location: US [1/66]
# #> ...
# #> Successfully computed search score | control: 1 | object: 2 | location: DO [66/66]

## ----eval = FALSE-------------------------------------------------------------
# # compute_voi ------------------------------------------------------------------
# compute_voi(object = new_object, control = new_control[[1]])
# #> Successfully computed search score | control: 1 | object: 1 | location: world [1/1]
# #> Successfully computed search score | control: 1 | object: 2 | location: world [1/1]

## ----eval = FALSE-------------------------------------------------------------
# # compute_doi ------------------------------------------------------------------
# compute_doi(object = new_object, control = new_control[[1]], locations = "countries")
# #> Successfully computed DOI | control: 1 | object: 1 [1/2]
# #> Successfully computed DOI | control: 1 | object: 2 [2/2]

## ----eval = FALSE-------------------------------------------------------------
# # manual exports ---------------------------------------------------------------
# gt.env$dt_score[keyword == "coca cola"]
# #>       location   keyword  date     score batch_c batch_o
# #>    1:       US coca cola 14610   0.00362       1       1
# #>  ...
# #>   10:       US coca cola 14883   0.00347       1       1
# #> ---
# #> 8030 more rows

## ----eval = FALSE-------------------------------------------------------------
# # export_control ---------------------------------------------------------------
# export_control(control = 1)
# #> # A tibble: 39,600 x 5
# #>    location keyword date        hits control
# #>    <chr>    <chr>   <date>     <dbl>   <int>
# #>  1 US       gmail   2010-01-01    22       1
# #>  ...
# #> 10 US       gmail   2010-10-01    27       1
# #> # ... with 39,590 more rows
# 
# # export_score -----------------------------------------------------------------
# export_score(object = 1, control = 1)
# #> # A tibble: 23,760 x 6
# #>    location keyword   date       score    control  object
# #>    <chr>    <chr>     <date>     <dbl>    <int>    <int>
# #>  1 US       coca cola 2010-01-01 0.00362  1        1
# #>  ...
# #> 10 US       coca cola 2010-10-01 0.00347  1        1
# #> # ... with 23,750 more rows
# 
# # export_doi and purrr interaction ---------------------------------------------
# purrr::map_dfr(c("coca cola", "microsoft"), export_doi, control = 1)
# #> # A tibble: 240 x 8
# #>    keyword   date       gini   hhi entropy control object locations
# #>    <chr>     <date>     <dbl> <dbl>   <dbl>   <int>  <int> <chr>
# #>  1 coca cola 2010-01-01 0.397 0.874  -0.938       1     1 countries
# #>  ...
# #> 10 coca cola 2010-10-01 0.574 0.968  -0.303       1     1 countries
# #> # ... with 230 more rows

## ----eval = FALSE-------------------------------------------------------------
# # export and dplyr interaction -------------------------------------------------
# library(dplyr)
# export_doi(object = 1, control = 1) %>%
#   filter(lubridate::year(date) == 2019) %>%
#   summarise(gini = mean(gini), .by = keyword)
# #> # A tibble: 3 x 2
# #>   keyword    gini
# #>   <chr>     <dbl>
# #> 1 coca cola 0.615
# #> 2 facebook  0.707
# #> 3 microsoft 0.682

## ----eval = FALSE-------------------------------------------------------------
# # change locations -------------------------------------------------------------
# download_control(control = 1, locations = us_states)
# download_object(object = 1:2, locations = us_states)
# compute_score(object = 2, control = 1, locations = us_states)
# compute_doi(object = 1:2, control = 1, locations = "us_states")

## ----eval = FALSE-------------------------------------------------------------
# add_locations(c("AT", "CH", "DE"), type = "dach")
# #> Successfully created new location set dach (AT, CH, DE).
# data <- export_score(keyword = "coca cola", location = dach)
# dplyr::count(data, location)
# #> # A tibble: 3 x 2
# #>   location     n
# #>   <chr>    <int>
# #> 1 AT         127
# #> 2 CH         127
# #> 3 DE         127

