我一直在使用R功能進行修補,在搜索文本中輸入搜索網站的數量和每個網站周圍的半徑。例如twitterMap("#rstats",10,"10mi")
下面的代碼:
twitterMap <- function(searchtext,locations,radius){
require(ggplot2)
require(maps)
require(twitteR)
#radius from randomly chosen location
radius=radius
lat<-runif(n=locations,min=24.446667, max=49.384472)
long<-runif(n=locations,min=-124.733056, max=-66.949778)
#generate data fram with random longitude, latitude and chosen radius
coordinates<-as.data.frame(cbind(lat,long,radius))
coordinates$lat<-lat
coordinates$long<-long
#create a string of the lat, long, and radius for entry into searchTwitter()
for(i in 1:length(coordinates$lat)){
coordinates$search.twitter.entry[i]<-toString(c(coordinates$lat[i],
coordinates$long[i],radius))
}
# take out spaces in the string
coordinates$search.twitter.entry<-gsub(" ","", coordinates$search.twitter.entry ,
fixed=TRUE)
#Search twitter at each location, check how many tweets and put into dataframe
for(i in 1:length(coordinates$lat)){
coordinates$number.of.tweets[i]<-
length(searchTwitter(searchString=searchtext,n=1000,geocode=coordinates$search.twitter.entry[i]))
}
#making the US map
all_states <- map_data("state")
#plot all points on the map
p <- ggplot()
p <- p + geom_polygon(data=all_states, aes(x=long, y=lat, group = group),colour="grey", fill=NA)
p<-p + geom_point(data=coordinates, aes(x=long, y=lat,color=number.of.tweets
)) + scale_size(name="# of tweets")
p
}
# Example
searchTwitter("dolphin",15,"10mi")
有我遇到我不知道如何處理一些大的問題。首先,如代碼所示,代碼搜索15個不同的隨機生成的位置,這些位置是從美國東部最大經度的最大西部和最北部的緯度到最南部的均勻分佈生成的。這將包括不在美國的地點,比如在加拿大明尼蘇達森林湖的東邊。我想要一個函數來隨機檢查生成的位置是否在美國,如果不是,則丟棄它。更重要的是,我想搜索數千個位置,但Twitter不喜歡這樣的位置,並給我一個420 error enhance your calm
。因此,最好每隔幾個小時搜索一次,然後慢慢建立數據庫並刪除重複的推文。最後,如果選擇了一個遠程熱門話題,R給出了一個錯誤,如Error in function (type, msg, asError = TRUE) : transfer closed with 43756 bytes remaining to read
。我對如何解決這個問題感到有些迷惑。
您需要提供一個'geocode'爲'searchTwitter'使用。查看庫文檔'searchTwitter'。 – 2012-07-26 17:46:49
我發現您可以將「地理編碼」和「半徑」提供給「searchTwitter」,但不會爲每個拉動的推文產生地理編碼。 – iantist 2012-07-26 18:19:44
但你會有你提供的地理編碼,對吧?用更小的半徑可能會給你你需要的東西? – 2012-07-26 18:25:42