Emir
Emir

Reputation: 481

Converting twitteR results to data frame

I have a simple for loop to write the past 100 tweets of a few usernames to .csv files:

library(twitteR)
mclist <- read.table('usernames.txt')

for (mc in mclist) 
{
    tweets <- userTimeline(mc, n = 100)

    df <- do.call("rbind", lapply(tweets, as.data.frame))

    write.csv(df, file=paste("Desktop/", mc, ".csv", sep = ""), row.names = F)
}

I mostly followed what I've read on StackOverflow but I continue to get this error message:

Error in file(file, ifelse(append, "a", "w")) : 
  invalid 'description' argument
In addition: Warning message:
In if (file == "") file <- stdout() else if (is.character(file)) { :
  the condition has length > 1 and only the first element will be used

Where did I go wrong?

Upvotes: 0

Views: 2468

Answers (3)

geoffjentry
geoffjentry

Reputation: 4754

There's a convenience function in the package twListToDF which will handle the conversion of the list of tweets to a data.frame.

Upvotes: 2

Dinre
Dinre

Reputation: 4216

I just cleaned up the code a bit, and everything started working.

Step 1: Let's set the working directory and load the 'twitteR' package.

library(twitteR)
setwd("C:/Users/Dinre/Desktop") # Replace with your desired directory

Step 2: First, we need to load a list of user names from a flat text file. I'm assuming that each line in the text file has one username, like so:

[contents of usernames.txt]
edclef
notch
dkanaga

Let's load it using the 'scan' function to read each line into an array:

mclist <- scan("usernames.txt", what="", sep="\n")

Step 3: We'll loop through the usernames, just like you did before, but we're not going to refer to the directory, since we're going to use the same directory for output as input. The original code had a syntax error in attempting to referring to the desktop directory, and we're just going to sidestep that.

for (mc in mclist){
    tweets <- userTimeline(mc, n = 100)
    df <- do.call("rbind", lapply(tweets, as.data.frame))
    write.csv(df, file=paste(mc, ".csv", sep = ""), row.names = F)
}

I end up with three files on the desktop, and all the data seems to be correct.

edclef.csv
notch.csv
dkanaga.csv

Update: If you really want to refer to different directories within your code, use the '.' character to refer to the parent directory. For instance, if your working directory is your Windows user profile, you would refer to the 'Desktop' folder like so:

setwd("C:/Users/Dinre")
...
write.csv(df, file=paste("./Desktop/". mc, ".csv", sep = ""), row.names = F)

Upvotes: 4

agstudy
agstudy

Reputation: 121618

Since your mclist is a data.frame, you can replace your for by apply

apply( mclist, 1,function(mc){
  tweets <- userTimeline(mc, n = 100)
  df <- do.call("rbind", lapply(tweets, as.data.frame))
  write.csv(df, file=paste("Desktop/", mc, ".csv", sep = ""), ##!! Change Desktop to  
                                                              ## something like Desktop/tweets/
            row.names = F)
})

PS :

The userTimeline function will only work if the user requested has a public timeline, or you have previously registered a OAuth object using registerTwitterOAuth

Upvotes: 0

Related Questions