Learn R Programming

SocialMediaLab (version 0.20.0)

CollectDataTwitter: Collect data from Twitter for generating different types of networks

Description

This function collects data from Twitter based on hashtags or search terms, and structures the data into a data frame of class dataSource.twitter, ready for creating networks for further analysis.

Usage

CollectDataTwitter(searchTerm, numTweets, verbose, writeToFile, language)

Arguments

searchTerm
character string, specifying a search term or phrase (e.g. "Australian politics") or hashtag (e.g. "#auspol"). Many query operators are available - see the Twitter documentation for more information: https://dev.twitter.com/rest/public/search
numTweets
numeric integer, specifying how many tweets to be collected. Defaults to 1500. Maximum tweets for a single call of this function is 1500.
verbose
logical. If TRUE then this function will output runtime information to the console as it computes. Useful diagnostic tool for long computations. Default is FALSE.
writeToFile
logical. If TRUE then the data is saved to file in current working directory (CSV format), with filename denoting current system time and searchTerm. Default is FALSE.
language
character string, restricting tweets to the given language, given by an ISO 639-1 code. For example, "en" restricts to English tweets. Defaults to NULL.

Value

  • A data frame object of class dataSource.twitter that can be used for creating unimodal networks (CreateActorNetwork), bimodal networks (CreateBimodalNetwork), and semantic networks (CreateSemanticNetwork).

Details

CollectDataTwitter collects public 'tweets' from Twitter using the Twitter API.

The function then finds and maps the relationships of entities of interest in the data (e.g. users, terms, hashtags), and structures these relationships into a data frame format suitable for creating unimodal networks (CreateActorNetwork), bimodal networks (CreateBimodalNetwork), and semantic networks (CreateSemanticNetwork).

The maximum number of tweets for a single call of CollectDataTwitter is 1500.

Language support is available, using the language argument. The user can restrict tweets returned to a particular language, using the ISO 639-1 code. For example, restricting to English would use language="en". The full list of codes is available here: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes.

A variety of query operators are available through the Twitter API. For example, "love OR hate" returns any tweets containing either term (or both). For more information see the Twitter API documentation (under the heading 'Query Operators'): https://dev.twitter.com/rest/public/search

See Also

AuthenticateWithTwitterAPI must be run first or no data will be collected.

Examples

Run this code
# Firstly specify your API credentials
  my_api_key <- "1234567890qwerty"
  my_api_secret <- "1234567890qwerty"
  my_access_token <- "1234567890qwerty"
  my_access_token_secret <- "1234567890qwerty"

  # Authenticate with the Twitter API using \code{AuthenticateWithTwitterAPI}
  AuthenticateWithTwitterAPI(api_key=my_api_key, api_secret=my_api_secret,
    access_token=my_access_token, access_token_secret=my_access_token_secret)

  # Collect tweets data using \code{myTwitterData}
  myTwitterData <- CollectDataTwitter(searchTerm="#auspol",
    numTweets=150,writeToFile=FALSE,verbose=FALSE)

  # Create an 'actor' network using \code{CreateActorNetwork}
  g_actor_twitter <- CreateActorNetwork(myTwitterData)

  # Create a 'bimodal' network using \code{CreateBimodalNetwork}
  g_bimodal_twitter <- CreateBimodalNetwork(myTwitterData)

  # Create a 'semantic' network using \code{CreateSemanticNetwork}
  g_semantic_twitter <- CreateSemanticNetwork(myTwitterData)

Run the code above in your browser using DataLab