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.
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.
You will also need to call the packages to start working with them:
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.
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.
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.
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:
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.
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.
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.
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.
IRun a print statement to make sure everything looks good.
What you get back should look like this:
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.
Simply print out this rule to see what you have:
You should get back something that looks like this with the handle you typed in:
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.
If you print out this variable you’ll get a body of JSON that looks like this:
You can pass the stream of the results using the attribute stream() into a variable called tweets.
When you print out the variable of tweets you will get a generator object that looks like this.
To be able to access the information in a Python generator object you can use the iterate attribute.
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.
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.
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.
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.