First session of the R meetup for Digital Humanists main page second session
American University of Beirut
On the first day of our meetup we downloaded RStudio, spoke about its layout, functions, packages, CRAN and very first steps.
Then, we jumped ahead to some practical examples using some R packages:
#working with computational stylistics
install.packages(stylo)
library(stylo)
#set wd to spanish corpus
stylo ()
#setting up twitter mining
install.packages("twitteR")
install.packages("RCurl")
install.packages("tm")
install.packages("base64enc")
install.packages("devtools")
install.packages("wordcloud")
require(twitteR)
require(RCurl)
require(tm)
require(base64enc)
require(devtools)
require(wordcloud)
devtools::install_github("hadley/httr")
# these values you can obtain from Twitter, in the applications section
api_key <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
api_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
pope <- searchTwitter('pope+washington', lang = 'en', n =300, resultType = "recent") #getting tweets
class(pope)
pope_text <- sapply(pope, function(x) x$getText()) #creating a character vector, extracting text
str(pope_text) #looking at the structure of the vector (full of tweets)
pope_corpus <- Corpus(VectorSource(pope_text)) #creating a corpus - TM package function
pope_corpus
inspect (pope_corpus[1]) #see document 1
pope_clean <- tm_map(pope_corpus, removePunctuation)
pope_clean <- tm_map(pope_clean, content_transformer(tolower))
pope_clean <- tm_map(pope_clean, removeWords, stopwords ("english"))
pope_clean <- tm_map(pope_clean, removeNumbers)
pope_clean <- tm_map(pope_clean, stripWhitespace)
pope_clean <- tm_map(pope_clean, removeWords, c("pope", "washington", "arrives", "visit", "today"))
inspect(pope_clean[1:300])
wordcloud(pope_clean) # see a basic wordcloud
wordcloud(pope_clean, random.order = F, scale = c(6, 0.5)) #playing with settings
#some leaflet basics
devtools::install_github("rstudio/leaflet")
library(leaflet)
leaflet()
m <- leaflet()
m <- addTiles (m)
m <- addMarkers (m, lng= 35.485008, lat = 33.899367, popup = "our R meetup today")
m