newsfreq is a package that provides access to the newsfreq.com API, which enables searching media sources for keywords in news articles.

The following functions are implemented:

The following data sets are included:

News

Installation

devtools::install_github("hrbrmstr/newsfreq")
options(width=120)

Usage

library(newsfreq)

# current verison
packageVersion("newsfreq")

library(newsfreq)
library(ggplot2)
library(scales)
library(dplyr)
library(tidyr)
library(ggthemes)
breach <- news_search(keywords="(data breach) OR hacking OR (denial of service)",
                      date_from="2010-01-01", date_to="2014-12-31")
head(breach)

breach %>%
  mutate(search_terms="breach/hack/dos") -> breach

terrorism <- news_search(keywords="terrorism",
                   date_from="2010-01-01", date_to="2014-12-31")

warming <- news_search(keywords="global warming",
                   date_from="2010-01-01", date_to="2014-12-31")

bitcoin <- news_search(keywords="bitcoin",
                       date_from="2010-01-01", date_to="2014-12-31")

bind_rows(breach, terrorism, warming, bitcoin) %>%
  select(date_from, search_terms, count) -> combined
gg <- ggplot(combined, aes(x=date_from, y=count, group=search_terms))
gg <- gg + geom_line(aes(color=search_terms))
gg <- gg + geom_point(aes(color=search_terms), size=1.5)
gg <- gg + scale_x_date(breaks=date_breaks("year"),
                        labels=date_format("%Y"), expand=c(0,0))
gg <- gg + scale_y_continuous(label=comma)
gg <- gg + scale_color_tableau(name="Topic")
gg <- gg + labs(x=NULL, y="# Articles", title="Keyword frequency since 2010")
gg <- gg + theme_bw()
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg

Test Results

library(newsfreq)
library(testthat)

date()

test_dir("tests/")


hrbrmstr/newsfreq documentation built on May 19, 2019, 8:19 a.m.