Learn R Programming

PytrendsLongitudinalR (version 0.1.4)

initialize_request_trends: Initialize Google Trends Request

Description

This function initializes a request to the Google Trends API using pytrends, creates necessary directories, and prepares parameters for data collection.

Usage

initialize_request_trends(
  keyword,
  topic = NULL,
  folder_name,
  start_date,
  end_date,
  data_format
)

Value

A list containing initialized values and objects for further interaction with the package:

logger

A logging object for recording messages.

keyword

The keyword used for data collection.

topic

The topic associated with the keyword.

folder_name

Name of the parent folder for storing data.

start_date

Start date for data collection.

end_date

End date for data collection.

data_format

Time basis for the data query ('daily', 'weekly', or 'monthly').

num_of_days

Number of days between start_date and end_date.

pytrend

Initialized pytrends request object.

time_window

Optional. Time window parameter, applicable for 'weekly' data format.

times

Optional. Time periods determined for 'weekly' or 'daily' data formats.

Arguments

keyword

The keyword to be used for collecting Google Trends data.

topic

The topic associated with the keyword. For example, '/m/0ddwt' will give Google Trends data for Insomnia as topic of 'Disorder'. If identical to the keyword, data will reflect Google Trends search term data. NOTE: URL's have certain codes for special characters. For example, %20 = white space, %2F = / (forward slash) etc.

folder_name

Name of the parent folder where all data will be stored.

start_date

The start date from which to collect Google Trends data.

end_date

The end date until which to collect Google Trends data.

data_format

Time basis for the query. Can be one of 'daily', 'weekly', or 'monthly'.

Details

The initiation stage involves creating two folders automatically: - The main folder chosen by the user (`folder_name`). - A subfolder corresponding to the `data_format` (e.g., 'daily', 'weekly', 'monthly') for storing data.

Examples

Run this code
# Create a temporary folder for the example

# Ensure the temporary folder is cleaned up after the example
if (reticulate::py_module_available("pytrends")) {
  # Run the function with the temporary folder
  params <- initialize_request_trends(
    keyword = "Coronavirus disease 2019",
    topic = "/g/11j2cc_qll",
    folder_name = file.path(tempdir(), "test_folder"),
    start_date = "2024-05-01",
    end_date = "2024-05-03",
    data_format = "daily"
  )
  on.exit(unlink("test_folder", recursive = TRUE))
} else {
  message("The 'pytrends' module is not available.
  Please install it by running install_pytrendslongitudinalr()")
}

Run the code above in your browser using DataLab