Skip to content

Instantly share code, notes, and snippets.

@abresler
Last active December 18, 2015 15:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abresler/edf23d98b4fd08c28065 to your computer and use it in GitHub Desktop.
Save abresler/edf23d98b4fd08c28065 to your computer and use it in GitHub Desktop.
Netsdaily Polarity Plot in R
### Parsing Netsdaily for a Polarity Plot -- working with JSON
packages <-
c('magrittr', 'dplyr', 'qdap', 'jsonlite', 'ggplot2')
lapply(packages, library, character.only = T)
url <-
'http://www.netsdaily.com/comments/load_comments/8261850'
data <-
url %>%
fromJSON(simplifyDataFrame = T,flatten = T) %>%
.[2] %>%
data.frame %>%
tbl_df
names(data) %<>% gsub('comments.','',.)
data %<>%
mutate(title = title %>% tolower,
username = username %>% tolower) %>%
select(title, username) %>%
distinct
data %<>% qdap_df(text.var = 'title')
data %&%
polarity(username) %>%
scores %>%
plot
@trinker
Copy link

trinker commented Apr 28, 2015

Thanks for sharing. Nice uses of magrittr I hadn't considered many of the %<>% uses before. You might want to include this line as the header as it's cleaner IMO for those that may not have these packages installed already.

if (!require("pacman")) install.packages("pacman")
pacman::p_load(magrittr, dplyr, qdap, jsonlite, ggplot2)

Also I saw I had a misspelling in the warning message qdap_df throws. Fixed that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment