Error Crawling Data Twitter With R Programming - r

How do I fix an error like this:
library(twitteR)
library(ROAuth)
customer_key<-"-aaaaaa"
customer_secret<-"aaaaaaaa"
access_token<-"212123213-aaaaa"
access_secret<-"ccccccccccccc"
setup_twitter_oauth(customer_key,customer_secret,access_token,access_secret)
Tweets = searchTwitter("anxiety", n=100, lang = "en")
Error: invalid assignment for reference class field ‘language’, should be from class “character” or a subclass (was class “NULL”)
I User RStudio for run this source code, this is my first time using the twitter API. before I saw the tutorial and they succeeded, after I tried it and there was an error like that. thankyou

consumer_key <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
consumer_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
The steps below are the process of extracting Twitter with keyword #statistics and retrieving 10000 data
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
tw = searchTwitter('#statistics',
n = 10000,
retryOnRateLimit = 1617)
Make sure the token you use has been updated, and the possibility that the word you will scrape is "anxiety" does not exist in the last 7 days. Because scraping using the API will extract data 7 days back only. Suppose I extract data on the 8th, the data that will be pulled is data or sentences containing the word "anxiety" from the 2nd to the 8th.

Related

No search results for a # that really exists

I am analysing #angtunaynalalake or #AngTunayNaLalake - a tweet that is famous in the Philippines. I run the following code but there are 0 results. I tried the same code using other famous #s like #MeToo or as #rstats and the code was successful. I tried using 'lalake' instead of '#angtunaynalalake' and again the code was successful.
I was able to get results for #angtunaynalalake when I use twitteR. I want to use rtweet because it can give more search results than twitteR.
Why do you think this happens?
create_token(
app = "my_twitter_research_app",
consumer_key = "xxxx",
consumer_secret = "xxxx",
access_token = "xxxx",
access_secret = "xxxx")
> rt <- search_tweets(
+ "#angtunaynalalake", n = 25000, retryonratelimit = TRUE
+ )
Searching for tweets...
This may take a few seconds...
Finished collecting tweets!
> rt
data frame with 0 columns and 0 rows
'''
This is an issue with the twitter API and not with the rtweet package.
See this quote from the twitter API page.
"Keep in mind that the search index has a 7-day limit. In other words, no tweets will be found for a date older than one week."
The most recent tweet to #angtunaynalalake seems to have been in January 2019, therefore the standard twitter API won't find any tweets.
If you need to access tweets older than 7-days, consider the twitter API pricing plans.

Error : Not all elements of twList are of the same class, when I convert the tweets with all variables to dataframe

I am using rtweet package in R to extract tweets of particular hashtag which basically needs appname,api_key,api_secret,access_token,access_token_secret.
So i have created an app in Twitter to get all the above details. And then I pass the above as follows
twitter_token <- create_token(
app = appname,
consumer_key = api_key,
consumer_secret = api_secret)
tweets <-search_tweets("#iPhone8", n=1000, include_rts = FALSE, type = "recent",lang = "en")
So this will basically extract tweets with 88 variables. Now I want to write these tweets to an excel file for further analysis.
And for that I am using the below code.
tweets.df <-twListToDF(tweets)
When I run this I am getting the following error.
Error in twListToDF(tweets) :
Not all elements of twList are of the same class
Just want to understand what could be the potential issue here.
Problem is I cannot put the exact code here as that needs my api key and all.
Any help would be really appreciated.
Regards,
Akash
You can as well use this
tweets.df <-as.data.frame(tweets)

Accessing Twitter Ads API from R using twitteR package?

I am trying to access Twitter Ads API from R, using twitteR package to authenticate, however, I cannot move further, any ideas, did anybody try it using a similar approach? Current error I get as this one (below) My app is whitelisted for ads, and my credentials work through Twurl, using this credential I can without problem access public API, but struggle with Ads one. Thanks!
Error: $ operator is invalid for atomic vectors
library(twitteR)
library(BBmisc)
library(httr)
appname <- “xxxxx”
api_key <- “xxxxxxx”
api_secret <- “xxxxxxxx”
access_token <- “xxxxxxx”
access_secret <- “xxxxxx”
account_id <- “xxxxxxx”
setup_twitter_oauth(api_key,api_secret, access_token, access_secret)
campaign_id <- itostr(18257572,base=36)
reach_campaign.request <- GET(“https://ads-api.twitter.com/2/stats/accounts/xxxxxxx/reach/campaigns?start_time=2018-06-10T00:00:00+01:00Z&campaign_ids=xxxxx&end_time=2018-06-15T00:00:00+01:00Z”,
query = list(granularity = “DAY”,
metric_groups = “ENGAGEMENT”,
placement = “ALL_ON_TWITTER”),
accept_json(),
config(token = access_token))

R - Twitter - fromJSON - get list of tweets

I would like to retrieve a list of tweets from Twitter for a given hashtag using package RJSONIO in R. I think I am pretty close to the solution, but I seem to miss one step.
My code reads as follows (in this example, I use #NBA as a hashtag):
library(httr)
library(RJSONIO)
# 1. Find OAuth settings for twitter:
# https://dev.twitter.com/docs/auth/oauth
oauth_endpoints("twitter")
# Replace key and secret below
myapp <- oauth_app("twitter",
key = "XXXXXXXXXXXXXXX",
secret = "YYYYYYYYYYYYYYYYY"
)
# 3. Get OAuth credentials
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
# 4. Use API
req=GET("https://api.twitter.com/1.1/search/tweets.json?q=%23NBA&src=typd",
config(token = twitter_token))
req <- content(req, as = "text")
response=fromJSON(req)
How can I get the list of tweets from object 'response'?
Eventually, I would like to get something like:
searchTwitter("#NBA", n=5000, lang="en")
Thanks a lot in advance!
The response object should be a list of length two: statuses and metadata. So, for example, to get the text of the first tweet, try:
response$statuses[[1]]$text
However, there are a couple of R packages designed to make just this kind of thing easier: Try streamR for the streaming API, and twitteR for the REST API. The latter has a searchTwitter function exactly as you describe.

Using R to send tweets

I saw a cute demonstration of tweeting from R in a presentation some months ago. The scratch code used by the presenter is here:
http://www.r-bloggers.com/twitter-from-r%E2%80%A6-sure-why-not/
the code is short and sweet:
library("RCurl")
opts <- curlOptions(header = FALSE,
userpwd = "username:password", netrc = FALSE)
tweet <- function(status){
method <- "http://twitter.com/statuses/update.xml?status="
encoded_status <- URLencode(status)
request <- paste(method,encoded_status,sep = "")
postForm(request,.opts = opts)
}
With this function, you can send a tweet simply by using the update function:
tweet("This tweet comes from R! #rstats")
I thought that this could be a useful way of announcing when long jobs are completed. I tried to run this on my machine, and I got some error:
[1] "\n\n Basic authentication is not supported\n\n"
attr(,"Content-Type")
charset
"application/xml" "utf-8"
Warning message:
In postForm(request, .opts = opts) : No inputs passed to form
I'm wondering if there has been some changes on the twitter end of this, that make this code produce this error? I don't know too much about getting R to talk to webpages, so any guidance is much appreciated!!
E
Yes, the basic authentication scheme was disabled on the 16th August 2010.. You'll need to set it up to use OAuth. Unfortunately that is not nearly as simple as using basic authentication
See this twitter wiki page for more information and this StackOverflow question about OAuth for R.
Besides the code you show, there is also a full-blown twitteR package on CRAN you could look at.
The easiest way to tweet in R through the Twitter-API is to use the twitteR Package.
You can set your Twitter-API-APP here: https://apps.twitter.com/
First step is to authenticate:
consumer_key <- "yourcredentials"
consumer_secret <- "yourcredentials"
access_token <- "yourcredentials"
access_secret <- "yourcredentials"
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
And just tweet (limit per day:2400 tweets):
tweet("Hello World")
If twitteR does not work or you simply want to try to build it yourself ...
See here for a demo of how to do your own Twitter authentication and use of the API with help of the httr package.

Resources