Introduction
This article describes how to import data from Twitter into Displayr.
Requirements
- A Displayr document.
- A Twitter Developer account with a valid consumer API key, consumer API secret, access token, and access secret from your Developer Platform via Apps > Details > Keys and tokens.
Method - Search Twitter
1. In the ribbon select Data Sets > Plus (+) > R.
2. Enter a name for the data set under Name.
3. Update and paste the below R code where it states "Enter your R code here":
library("twitteR")
library("ROAuth")
# Authentication
consumer_key = 'your_consumer_key'
consumer_secret = 'your_consumer_secret'
access_token = 'your_access_token'
access_secret = 'your_access_secret'
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
# Query
search.string = 'Tesla'
no.of.tweets = 100
tweets <- searchTwitter(search.string, n=no.of.tweets, lang="en")
search.df <- twListToDF(tweets)
Note, you will need to replace 'your_consumer_key', 'your_consumer_secret','your_access_token', and 'your_access_secret' with the appropriate user credentials. You will also need to update the search string and the number of tweets.
The above code uses the twitteR and ROAuth R packages to authenticate your session using your user credentials. It then uses the searchTwitter function to search for the specified text. In this example, we are searching for the last 100 tweets for a mention of 'Tesla'.
4. OPTIONAL: You can test the R code first by selecting Calculation > Custom Code and going to the object inspector > Properties > R Code and pasting the above code into the window.
This method allows for troubleshooting meaning you can amend the code until you have it working.
Method - User Timeline
1. In the ribbon select Data Sets > Plus (+) > R.
2. Enter a name for the data set under Name.
3. Update and paste the below R code where it states "Enter your R code here":
library("twitteR")
library("ROAuth")
# Authentication
consumer_key = 'your_consumer_key'
consumer_secret = 'your_consumer_secret'
access_token = 'your_access_token'
access_secret = 'your_access_secret'
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
# Query
tweets.user = userTimeline("displayrr", n=1000, maxID=NULL, sinceID=NULL, includeRts=TRUE)
tweets.user = twListToDF(tweets.user)
Note, you will need to replace your 'your_consumer_key', 'your_consumer_secret','your_access_token', and 'your_access_secret' with the appropriate user credentials.
This code again uses the twitteR and ROAuth R packages to authenticate your session using your user credentials. It then uses the userTimeline function to view the specified timeline. In this example, we are pulling the last 1000 tweets from the Displayr Twitter timeline.
3. OPTIONAL: If you wish to make this call take place daily, you can add the below lines to the top of your R code:
library(flipTime)
UpdateEvery(1, "days", options = "wakeup")
4. OPTIONAL: You can test the R code first via Calculation > Custom Code until you have it working as this method allows for troubleshooting.
See Also
How to Import Data into Displayr
Comments
0 comments
Article is closed for comments.