RalphM
RalphM

Reputation: 13

Error in twFromJSON(out) - R twitteR package

I have searched extensively online but am still unable to find a work-around for the following error whilst using the 'twitteR' package in R:

"Error in twFromJSON(out) : 
Error: Malformed response from server, was not JSON.
The most likely cause of this error is Twitter returning a character which
can't be properly parsed by R. Generally the only remedy is to wait long
enough for the offending character to disappear from searches (e.g. if
using searchTwitter())."    

It comes after running the following code:

# Clear the previously used libraries
rm(list=ls())

#Set Working Directory
setwd("C:/Users/toshiba/Google Drive/Programming/Projects/HDS/Shiny/Twitter")

#Load Libraries
library (twitteR)
library (RJSONIO)
library (dismo)
library (maps)
library (ggplot2)
library (XML)
load("twitteR_credentials")
registerTwitterOAuth(twitCred)
##############################Start App########################################
start_date = '2014-10-10'
end_date = toString(as.Date(start_date)+1)

#Search for tweets containing ebola - only goes back 8 days including today
ebolaTweets <- searchTwitter("ebola", 
                             n = 1250,
                             since=start_date,
                             until=end_date,
                             cainfo="cacert.pem")
tweetFrame <- twListToDF(ebolaTweets) # convert to dataframe

Is it not possible to somehow skip the offending tweet instead of breaking the loop?

Any help is much appreciated!

Upvotes: 1

Views: 848

Answers (1)

jbaums
jbaums

Reputation: 27388

Using the current version of twitteR (1.1.8) on Github (which handles authentication much more simply), I have no problems.

library(devtools)
install_github('twitteR', 'geoffjentry')

library (twitteR)
setup_twitter_oauth(consumer_key='blah', consumer_secret='blah', 
                    access_token='blah', access_secret='blah')
# get keys and tokens from apps.twitter.com

start.date <- '2014-10-10'
end.date <- as.character(as.Date(start_date) + 1)
ebolaTweets <- searchTwitter('ebola', 1250, since=start.date, until=end.date)
ebola <- twListToDF(ebolaTweets)

head(ebola)
#                                                                                                                                           text
# 1                                                         RT @ChillVibessonly: Ebola: I'm in the US broom broom\n\nAmerica: Get out me country
# 2 RT @RubenSanchezTW: #Alucinante TVE usa imágenes de un hospital alemán para ilustrar una info sobre el Carlos III http://t.co/5f5okmsjar ht…
# 3                                                                                          RT @DanLpda: Dejen de hablar del Ébola, me da miedo
# 4     Realiza Colombia estudios a 3 viajeros por temor a ébola: Pese a no presentar síntomas del virus los pasajeros... http://t.co/C5iJmgoQkI
# 5                                                                                                           Que es eso del ebola? Voy a llorar
# 6                                                                            RT @MicaMamonde: ebola no te tenemos miedo http://t.co/osgLmAmFnP
#   favorited favoriteCount replyToSN             created truncated replyToSID                 id replyToUID
# 1     FALSE             0      <NA> 2014-10-10 23:59:59     FALSE       <NA> 520725464223326209       <NA>
# 2     FALSE             0      <NA> 2014-10-10 23:59:59     FALSE       <NA> 520725464185581568       <NA>
# 3     FALSE             0      <NA> 2014-10-10 23:59:59     FALSE       <NA> 520725463304785921       <NA>
# 4     FALSE             0      <NA> 2014-10-10 23:59:59     FALSE       <NA> 520725463270821889       <NA>
# 5     FALSE             1      <NA> 2014-10-10 23:59:59     FALSE       <NA> 520725463120244737       <NA>
# 6     FALSE             0      <NA> 2014-10-10 23:59:59     FALSE       <NA> 520725463044734976       <NA>
#                                                                           statusSource      screenName retweetCount
# 1 <a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>    JaMeseKisses          684
# 2 <a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>   patillagrande         2059
# 3                   <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a> AriiFranciscovi            2
# 4                      <a href="http://twitterfeed.com" rel="nofollow">twitterfeed</a>         ColnRos            0
# 5   <a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>     ZoeVignieri            0
# 6                   <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a> felicitasalbano           10
#   isRetweet retweeted longitude latitude
# 1      TRUE     FALSE      <NA>     <NA>
# 2      TRUE     FALSE      <NA>     <NA>
# 3      TRUE     FALSE      <NA>     <NA>
# 4     FALSE     FALSE      <NA>     <NA>
# 5     FALSE     FALSE      <NA>     <NA>
# 6      TRUE     FALSE      <NA>     <NA>

Upvotes: 1

Related Questions