pkgs <- c("dplyr", "ggplot2", "knitr", "lubridate")
lapply(pkgs, function(P) 
  suppressPackageStartupMessages(require(P, character.only = TRUE)))

opts_chunk$set(echo = FALSE)
getwd()
## Load the data
data <- params$data

Period: r paste(format(Sys.Date() - 6, "%d %B"), "to", format(Sys.Date(), "%d %B %Y"))

Website

date.standard <- "1970-01-01"

webnews <- data$webnews
webnews$Date <- webnews$Date %>%
  as.Date(origin = date.standard) %>%
  as.POSIXct()

webnews <- webnews %>%
  mutate(Month = format(Date, "%B")) %>%
  mutate(Year = format(Date, "%Y"))

## make variables for monthly/annual data
thisMth <- format(today(), "%B")
thisYr <- format(today(), "%Y")

mth_news <- webnews %>%
  filter(Month == thisMth & Year == thisYr)

wk_news <- webnews %>%
  filter(as.Date(Date) >= today() - 7)

| News Stories | Result | |------------------|--------------------------------------------| | All-time | r nrow(webnews) | | In r thisMth | r nrow(mth_news) | | Last 7 days | r nrow(wk_news) | | Most recent | r webnews$Title[which.max(webnews$Date)] | | | |

make_ts(data = data$webnews, platform = 'website')

Twitter

## Process tweets by reconverting date field to POSIXct type 
## and the TRUE/FALSE fields from integer to logical.
## These were altered upon storage in the database.
twts <- process_stored_tweets(data$tweets)

## Add a column of Date objects for easy categorisation. Also carry 
## out a check to see whether the database needs to be updated.
twts$date_only <- as.Date(twts$created)

wk.data <- twts %>%
  filter(date_only >= (today() - 6) & date_only <= (today())) %>%
  arrange(date_only) %>%
  mutate(day = weekdays(date_only, abbreviate = TRUE)) %>%
  mutate(Type = ifelse(isRetweet, "Retweet", "Original"))

wk.data$day <-  factor(wk.data$day,
                       levels = unique(wk.data$day),
                       ordered = TRUE)

last_wk <- twts %>%
  filter(date_only >= (today() - 13) & date_only <= (today() - 7))

## Remove characters from the text of tweets that are not human-readable, 
## as they would be of no practical use in the analysis.
wk.data$text <- remove_nonreadables(wk.data$text)

## Some objects to be used to generate and/or display statistics
no.wk <- nrow(wk.data)
mth.beg <- floor_date(today(), "month")
mth.end <- ceiling_date(today(), "month")
mth_data <- twts %>%
  filter(date_only >= mth.beg & date_only <= mth.end)
twts.by.nesrea <- filter(wk.data, screenName == "NESREANigeria")
busy.day  <- which.max(table(wk.data$date_only))
busy.day  <- ymd(names(busy.day))

| Description | Result | |------------------------------|----------------------------------------------------| | In r format(today(), "%B") | r nrow(mth_data) | | In last 7 days | r no.wk | | Posts made | r nrow(twts.by.nesrea) | | Daily average | r floor(nrow(wk.data)/7) | | Most active on | r format(busy.day, "%d %B") | | Most liked | r wk.data$text[which.max(wk.data$favoriteCount)] | | Most retweeted | r wk.data$text[which.max(wk.data$retweetCount)] | | Comparative activity | r no.wk - nrow(last_wk) | | | |

make_ts(data = data$tweets, platform = 'twitter')
## TODO: Implement month & year transitions
simplePlot <- dens_plot(data = wk.data, platform = "twitter")
simplePlot +
  theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())
simplePlot +
  facet_grid(day ~ .) +
  theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())
ggplot(wk.data, aes(created)) +
  geom_density(aes(fill = Type), alpha = .5) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1)) +
  ggtitle(paste("Distribution of tweets")) +
  xlab("Date")
spl <- split(wk.data, wk.data$isRetweet)
origTwts <- spl[['FALSE']]

twPol <- compute_emotional_valence(origTwts$text)
visualise_pol_diff(pol.list = twPol)
origTwts$emotionalValence <- sapply(twPol, function(x) x$all$polarity)
generate_wordcloud(origTwts, twPol, site = "Twitter")
## TODO
RT <- mutate(spl[['TRUE']], sender = substr(text, 5, regexpr(':', text) - 1))

Facebook

                        ############
                        # Facebook #
                        ############
## Load data on Facebook Page posts from database;
## also do a little data wrangling
fbPosts <- data$fbposts %>%
  prepare() %>%
  select(message:shares_count) %>%
  mutate(created_mth = format(created_time, "%B")) %>%
  mutate(created_yr = format(created_time, "%Y"))

fbComments <- params$data$fbcomments
fbLikes <- params$data$fblikes

## Convert to date-time structures
fbPosts$created_time <- as.POSIXct(fbPosts$created_time)
fbComments$created_time <- as.POSIXct(fbComments$created_time)

## Remove any non-humanly readable characters
fbPosts$message <- remove_nonreadables(fbPosts$message)
fbComments$message <- remove_nonreadables(fbComments$message)

fbPosts$created_mth <-
  fbPosts$created_mth %>%
  factor(levels = month.name, ordered = TRUE)

mth_Posts <- fbPosts %>%
  filter(created_mth == thisMth & created_yr == thisYr)
wk_Posts <- mth_Posts %>%
  filter(created_time >= (today() - 6))

| Description | Result | |----------------------------|------------------------------------------| |NESREA Page Posts (All-time)|r nrow(fbPosts) | |Posts in r thisMth |r nrow(mth_Posts) | |Posts in the past 7 days |r nrow(wk_Posts) | |Most Liked (Overall) |r return_text(fbPosts, "likes_count") | |Most Liked in r thisMth |r return_text(mth_Posts, "likes_count") | |Most Shared (Overall) |r return_text(fbPosts, "shares_count") | |Most Shared in r thisMth |r return_text(mth_Posts, "shares_count")| |Most Commented (Overall) |r return_text(fbPosts, "comments_count")| | | |

make_ts(data = data$fbposts, platform = 'facebook')
ggplot(fbPosts, aes(created_time)) +
  geom_density(fill = "purple", alpha = 0.6) +
  ggtitle("Distribution of Facebook posts")
dens_plot(fbComments, platform = "facebook")
fbPol <- compute_emotional_valence(text.var = fbComments$message)
visualise_pol_diff(pol.list = fbPol)
fbComments$emotionalValence <- sapply(fbPol, function(x) x$all$polarity)
generate_wordcloud(fbComments, fbPol, site = "Facebook")
amk <- person(given = "Amaka", family = 'Ejiofor', middle = 'U.', email = 'amaka.ejiofor@nesrea.gov.ng', comment = 'Asst. Chief Information Officer')

Prepared and submitted by
r amk



NESREA/webreport documentation built on May 22, 2019, 1:58 p.m.