Lecture8: Network representation, TwitteR

d3Networkをインストールする

install.packages("d3Network")

d3SimpleNetwork関数

d3SimpleNetwork(net, width=800, height=500, 
                    standAlone=FALSE, opacity = 1.0,
                    charge = -200, fontsize = as.numeric(input$sel_fontsize),
                    parentElement = "#d3networkPlot")

netwkアプリケーションの起動(d3Networkを利用)

  library(shiny)
  runApp("app_netwk")

実習: 前回の課題テキストをd3Networkを利用して描画

Twitterアプリケーションの作成&登録

OAuth 認証用

https://apps.twitter.com/

ROAuth, twitteRのインストール

  install.packages('twitteR')
 install.packages('ROAuth')

ROAuth, twitteRのインストールの読み込み

  library(twitteR)
  library(ROAuth)

twitteRからのOauth認証

cacert.pemをダウンロード

  download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")

認証情報(Twitterアプリケーション)

consr_key="***********"
consr_secrt="***********"
req_url ="https://api.twitter.com/oauth/request_token"
acs_url = "https://api.twitter.com/oauth/access_token"
auth_url="https://api.twitter.com/oauth/authorize"
cred<-OAuthFactory$new(consumerKey=consr_key,consumerSecret=consr_secrt,requestURL =req_url,accessURL = acs_url,authURL=auth_url)

handshake: twitterクライアント接続

cred$handshake(cainfo="cacert.pem")

認証情報取得

setup_twitter_oauth(consr_key, consr_secrt, acs_token, acs_token_sec)

検索例

searchTwitter("#センター試験", n=10)
searchTwitter("#成人式", n=10)

username = "langstat"
userTimeline(username)

詳細情報取得

stweets<-searchTwitter("#センター試験", n=10)
stweets[1]

#converting a list type into a dataframe
stweet.df<-twListToDF(stweets)

#colnames
colnames(stweet.df)

cDateTokens<-stweet.df$created
cDateFreq<-table(as.Date(cDateLst))
cDateTypes<- as.Date(names(cDateFreq))
cDateTokens
cDateFreq
cDateTypes

タイムライン情報

tweets<-searchTwitter("#センター試験", n=1500)

#converting a list type into a dataframe
tweet.df<-twListToDF(tweets)

cDateTokens<-tweet.df$created
cDateFreq<-table(as.Date(cDateTokens))
cDateTypes<- as.Date(names(cDateFreq))

cDateFreq
cDateTypes

タイムライン表示1

#par(xaxt="s")
par(family="HiraMaruProN-W4")
plot(cDateTypes , as.numeric(cDateFreq), type="h", ylab="frequency")

タイムライン表示2

par(xaxt="n")
plot(cDateTypes , as.numeric(cDateFreq), type="h", ylab="frequency")
par(xaxt = "s")
axis.Date(1, at = seq(min(cDateTypes),max(cDateTypes),"days"),format = "%m/%d")

トレンド情報取得

aTL <- availableTrendLocations()
head(aTL)
subset(aTL, subset=country=="Japan")
subset(aTL, subset=name=="Osaka")

getTrends(subset(aTL, subset=name=="Osaka")[3])