pyCthon
pyCthon

Reputation: 12361

simple R project

Hi I'm new to R and i'm building off of two guides from the web, I figured out how to automate a script for data mining but instead of appending the data is then over written each time the code is run. I would like to have it appended can any one point me in the right direction.

here is the script as such

# loading the package is required once each session
require(XML)

# initialize a storage variable for Twitter tweets
mydata.vectors <- character(0)

# paginate to get more tweets
for (page in c(1:15))
{
    # search parameter
    twitter_q <- URLencode('#google OR #apple')
    # construct a URL
    twitter_url = paste('http://search.twitter.com/search.atom?q=',twitter_q,'&rpp=100&page=', page, sep='')
    # fetch remote URL and parse
    mydata.xml <- xmlParseDoc(twitter_url, asText=F)
    # extract the titles
    mydata.vector <- xpathSApply(mydata.xml, '//s:entry/s:title', xmlValue, namespaces =c('s'='http://www.w3.org/2005/Atom'))
    # aggregate new tweets with previous tweets
    mydata.vectors <- c(mydata.vector, mydata.vectors)
}

# how many tweets did we get?
length(mydata.vectors)

Upvotes: 0

Views: 783

Answers (1)

Jesse Anderson
Jesse Anderson

Reputation: 4613

I think what you want is to save the results to disk between runs. So, something like this at the beginning:

if (!file.exists('path/to/file'))
    mydata.vectors <- character(0)
else
    load('path/to/file')

And something like this at the end:

save(mydata.vectors, file='path/to/file')

Should do the trick. Of course you could get more sophisticated with save file types etc.

Upvotes: 2

Related Questions