5

Just wondering, is there anyway to change character encoding of a R list object? I have a feeling that I will have to take some multiple steps but I am not sure. I have googled on the topic but I am not quite getting help from internet.

for instance, consider the following:

 library(twitteR)
 library(RJSONIO)


 #Authorize with Twitter's API
 reqURL <- "https://api.twitter.com/oauth/request_token"
 accessURL <- "http://api.twitter.com/oauth/access_token"
 authURL <- "http://api.twitter.com/oauth/authorize"
 consumerKey = "myconsumerkey"
 consumerSecret = "myconsumersecret"
 twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                         consumerSecret=consumerSecret,
                         requestURL=reqURL,
                         accessURL=accessURL,
                         authURL=authURL)
  twitCred$handshake()

  B<-read.csv("BCorp RAW.csv") 
  handles<-B$Twitter.handle
  handles<-na.omit(handles)

  start <- getUser(handles[12])                        

  library(rjson)
  friends.object<- lookupUsers(start$getFriendIDs(), includeNA=TRUE)
  followers.object<-lookupUsers(start$getFollowerIDs(), includeNA=TRUE)

the command

   followers.object<-lookupUsers(start$getFollowerIDs(), includeNA=TRUE)

throws the following error:

   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()).

How can I allow R to preserve the special characters when working with twitter? hope I edited it nicely...let me know if you want me to edit it again

thank you,

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
Jin-Dominique
  • 3,043
  • 6
  • 19
  • 28
  • 1
    Can you elaborate a little bit more your question by providing a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and showing what you expect? – Jilber Urbina Oct 21 '13 at 20:03
  • No. You need to come up with a better example that shows *how* you get the data into R in the first place, because R will by default convert non-ascii characters to UTF-8 (or perhaps `"Unknown"`). Check `Encoding("£")` for instance, which gives `[1] "UTF-8"`. – Simon O'Hanlon Oct 21 '13 at 20:23
  • is it good? I can't really add real data because my raw data is in .csv file – Jin-Dominique Oct 21 '13 at 21:03
  • Yes, it is good. I think it will be easier for people to work on an answer now. – Simon O'Hanlon Oct 21 '13 at 21:05
  • 1
    Arrrgh. The edit makes it clear this is really a duplicate of an earlier question. http://stackoverflow.com/questions/19503130/r-working-with-non-latin-twitter-data – IRTFM Oct 21 '13 at 21:07
  • I deleted my old question...sorry about that – Jin-Dominique Oct 21 '13 at 21:13

1 Answers1

0

There is an Encoding<- function that lets you specify encoding of character vectors. List elements might have different encodings within the same list, but all elements in a character vector would have the same encoding. Furthermore, there are 'fileEncoding' and 'encoding' parameters to read.table and its read.* cousins to allow you to read character data in something other than the default.

?Encoding
?read.table

There are also a lot of encoding questions answered in the Archives of Rhelp.

IRTFM
  • 258,963
  • 21
  • 364
  • 487