Tips

Running the Python package for Search Tweets in R

By Jessica Garson

Running search-tweets-python in R

Reticulate is a package for R that allows you to run Python code inside of R. Since both Python and R are very popular for common data science tasks, it makes sense that you would want to use them together. A few times, I’ve been asked if we had a R version of the Python package, search-tweets-python. This package allows you to connect to the premium and enterprise search endpoints easily. With reticulate, you can call this Python package inside of R. This tutorial will walk you through how to use the search-tweets-python package inside of R to get a data frame that contains the date, ID and text of Tweets from a Twitter handle of your choosing from the past 30 days.

This Tweet is unavailable
This Tweet is unavailable.

What you need to get started with the Twitter API

First, you will need an approved developer account. You can apply for a developer account here. Once your account is approved, you will need to create a Twitter app. Then, set up that app for the Search Tweets: 30-Day environment. You will also need to set a dev environment name – for the purposes of this tutorial mine is named `Testaroo`. After you have completed the initial set up, you’ll need to locate your keys and tokens.

This Tweet is unavailable
This Tweet is unavailable.

Installing the packages you need

To follow this tutorial, you will need to install the packages reticulate and dplyr. Reticulate is the package that will allow you to run python code inside of R and dplyr allows you to shape your data to get exactly the results you are looking for.

This Tweet is unavailable
This Tweet is unavailable.
install.packages('reticulate')
install.packages('dplyr')

You will also need to call the packages to start working with them:

This Tweet is unavailable
This Tweet is unavailable.
library('reticulate')
library('dplyr')

Getting your Python environment to set up inside of R

To work with reticulate you will need to make sure that your python environment is set up in R. You will need to create a variable for your Python path and use the use_python module to set this path.

This Tweet is unavailable
This Tweet is unavailable.
path_to_python <- "/usr/bin/python3.7"
use_python(path_to_python)

You will need to install the Python packages searchtweets to help you connect to the Twitter API,  and pandas to create a data frame. You’ll create a virtual environment that provides an isolated Python environment to install packages into. You also have the option of using conda if you prefer.

The following code creates a virtual environment called tweetenv. You’ll use two lines of code to install into this environment. After installing it, you can tell R to use the virtual environment you just created called tweetenv by using the function use_virtualenv.

This Tweet is unavailable
This Tweet is unavailable.
virtualenv_install("tweetenv", "searchtweets", ignore_installed = TRUE)
virtualenv_install("tweetenv", "pandas", ignore_installed = TRUE)
use_virtualenv("tweetenv", required = TRUE)

You’ll need to import two packages you just installed into your code. You’ll create a variable called st that imports the package searchtweets and one called pd for pandas.

This Tweet is unavailable
This Tweet is unavailable.
st <- import("searchtweets")
pd <- import("pandas")

Storing and accessing your credentials

To store your credentials and endpoint information you can create a file in the same directory you are using called secret.yaml that contains the following:

This Tweet is unavailable
This Tweet is unavailable.
search_tweets_api:
  account_type: premium
  endpoint: https://api.twitter.com/1.1/tweets/search/30day/Testaroo.json
  consumer_key: xxxxxxxxxxxxxxxxxxx
  consumer_secret: xxxxxxxxxxxxxxxxxxx

The env_name in the endpoint is the name of the dev environment you created on developer.twitter.com. You will need to change this to the name of your own dev environment. 

You want to make sure you add this file to your .gitignore before you push it to GitHub. For more information about working with a .gitignore file check out this page. You also might want to review our guide on keeping tokens secure.

In your R file, you will want to create a variable that uses the load_creditionals function of the search-tweets-python library to load in your secret.yaml file. You may need to type the full path for the file if you run into any issues.

This Tweet is unavailable
This Tweet is unavailable.
cred <-
  st$load_credentials(filename = "secret.yaml",
                      yaml_key = "search_tweets_api",
                      env_overwrite = FALSE)

Getting key information 

You’ll first need to set up a variable to allow us to type in the Twitter handle you’re looking for. For the purposes of this tutorial, the example being used is @TwitterDev. But you can type in any Twitter handle you wish to after being prompted to do so by the code.

This Tweet is unavailable
This Tweet is unavailable.
input <- readline('What handle do you want to get Tweets from? ')

You’ll need to set up two variables one for today that pulls in the current date and one that gets us the data from 30 days back.

This Tweet is unavailable
This Tweet is unavailable.
today <- toString(Sys.Date())
thirty_days <- toString(as.Date(today) - 30)

In order for your text to be formatted correctly so that you can construct a valid search query, you’ll need to do some string formatting to create a variable called pt_format.

This Tweet is unavailable
This Tweet is unavailable.
pt_format <- sprintf("from:%s", input)

Run a print statement to make sure everything looks good.

This Tweet is unavailable
This Tweet is unavailable.
print(pt_format)

What you get back should look like this:

This Tweet is unavailable
This Tweet is unavailable.
[1] "from:TwitterDev"

Now you can pass this information into a rule by passing in the pt_format you just created, thirty_days, today and set the number of results per a call.

This Tweet is unavailable
This Tweet is unavailable.
rule <-
  st$gen_rule_payload(
    pt_rule = pt_format,
    from_date = thirty_days,
    to_date = today,
    results_per_call = 500
  )

Simply print out this rule to see what you have:

This Tweet is unavailable
This Tweet is unavailable.
print(rule)

You should get back something that looks like this with the handle you typed in:

This Tweet is unavailable
This Tweet is unavailable.
[1] "{\"query\": \"from:TwitterDev\", \"toDate\": \"202002070000\", \"fromDate\": \"202001080000\"}"

Getting the Tweets

Next you’ll need to create a variable called rs which lets us connect to the endpoint, pass in the max results, and pass in your username, endpoint, and Bearer token.

This Tweet is unavailable
This Tweet is unavailable.
rs <-
  st$ResultStream(
    rule_payload = rule,
    max_results = 500,
    username = cred$extra_headers_dict,
    endpoint = cred$endpoint,
    bearer_token = cred$bearer_token
  )

If you print out this variable you’ll get a body of JSON that looks like this:

This Tweet is unavailable
This Tweet is unavailable.
ResultStream: 
	{
    "username": null,
    "endpoint": "https://api.twitter.com/1.1/tweets/search/30day/Testaroo.json",
    "rule_payload": {
        "query": "from:TwitterDev",
        "toDate": "202002070000",
        "fromDate": "202001080000"
    },
    "tweetify": true,
    "max_results": 1000000000000000
}

You can pass the stream of the results using the attribute stream() into a variable called tweets.

This Tweet is unavailable
This Tweet is unavailable.
tweets <- rs$stream()
print(tweets)

When you print out the variable of tweets you will get a generator object that looks like this.

This Tweet is unavailable
This Tweet is unavailable.
<generator object ResultStream.stream at 0x11f4a1af0>

To be able to access the information in a Python generator object you can use the iterate attribute.

This Tweet is unavailable
This Tweet is unavailable.
it_tweets <- iterate(tweets)

Creating a data frame

A data frame is a table-like data structure which can be particularly useful for working with datasets. To get a data frame of Tweets you can use the DataFrame attribute of pandas. A data frame object from pandas is compatible with the data frames in R, so you can use your favorite R packages from this point you can use without doing extra conversions.

This Tweet is unavailable
This Tweet is unavailable.
tweets_df <- pd$DataFrame(data=it_tweets)

If you want to look through the data frame, It’s a bit easier to use View than print, which will show you a large data frame with many columns.

This Tweet is unavailable
This Tweet is unavailable.
View(tweets_df)

If you want to work with a smaller data frame that contains only a few columns you can select the specific columns using dyplr, a package for R that is helpful for reshaping data. The following code will return a data frame that has the date, Tweet ID and the text for all Tweets that fit the query you ran earlier.

This Tweet is unavailable
This Tweet is unavailable.
smaller_df <- select(tweets_df, created_at, id, text)
View(smaller_df)

Next steps

It was exciting to explore the interoperability of working with Twitter data with elements of two different languages. Other things you can do to explore this further include running R inside of a Jupyter Notebook, and use a package called rpy2 to run R code inside of Python.The full version of the code can be found here. Let us know on the forums if you run into any troubles along the way or Tweet us at @TwitterDev if this inspires you to create anything. 

I used several libraries and tools beyond the Twitter API to make this tutorial, but you may have different needs and requirements and should evaluate whether those tools are right for you.

 

This Tweet is unavailable
This Tweet is unavailable.
@jessicagarson

Jessica Garson

‎@jessicagarson‎

Developer Advocate

Only on Twitter