Locations Search Part I

Christopher Marks

2019-01-18

PART I: Initialize and Execute Search Queries

This is the first part of a two-part vignette demonstrate the use of the Twitter Search API collection and analysis functionality of the twinfoR package. One use of Twitter analyses is to investigate what is going on in different locations. A simple way to collect Tweets related to a location is to use the Twitter Search API, searching on the hashtag containing the location name. Of course, this introduces a bias into the analysis, but can still be useful.

In this Vignette, we’ll look at two locations: Boston and Guatemala City. We’ll use the following hashtags:

  1. Boston
    • #Boston
  2. Guatemala City
    • #GuatemalaCity
    • #CiudaddeGuatemala
    • #CuidadGuatemala

Package Import

Let’s start by importing the twinfoR namespace. Don’t forget to install the package, if you haven’t already.

# install.packages("twinfoR.zip",repos=NULL,type="binary")
library(twinfoR)

Some important initializations

Query Text Table

We’ll be using the query_text table functionality of the twitter_database. See twitter_database documentation for details. We begin by creating the query_text table containing our search queries.

Initialize database

We need to set a working directory to store the database and analysis products. Once we set the directory, we’ll change into it and initialize the location database with our query.text.dataframe. Note that this vignette does not use the query_users table, but it could. See the US Politics vignette and the twitter_database documentation for examples on how to include and use a query_users table.

Authenticate to Twitter API

We’ll use the INFORM-Tutorial Twitter Application. Alternatively, you can supply your own application credentials using the authorize_app function.

You will need a Twitter Account to continue! If you don’t have one, you can easily create one on the Twitter website. If you do not have a Twitter account and do not wish to make one, this package will be of very limited use to you.

I recommend logging onto Twitter in your Browser before running this line. Either way, be careful not to hit immediately after running it, for example by copying and pasting the whole line with the ‘newline’ character into an R console.

Run this line, log into Twitter, and get the authentication PIN. If you get an error message from Twitter saying the URL is expired, the probable cause is that you hit before entering the PIN, possibly when you copied and pasted the command.

It is important to name the result auth.vector as shown above, as the functions in the twinfoR package look for this global variable. If you give this result a different name, you must manually supply it to the Twitter API methods. Optionally, save this vector in order to skip the authentication process in the future.

Summarize the database

To wrap up part I, we can view a summary of what is in our Twitter database.

summarize_database(con)
#> No query user table.
#> Search queries: 4
#> Search categories:
#> location
#> since_id
#> Users: 11520
#> Statuses: 22012
#> Unique user mentions: 5684
#> Unique hashtags: 16128
#> Unique URLs: 7370
#> Total Images: 6430
#> Unique Images: 5511
#> Total relationships: 0
#> Database file size: 89MB

Finally, to conclude this part of the vignette, we close the data connection.

DBI::dbDisconnect(con)