knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Anna Sophie Peters, Nurah Jaradat, Jiner Zheng

12 December 2019

This function allows Twitter users to search the website by keyword or hashtag, returning multiple graphics and statistical summaries such as a bar plot of related unique words, a word cloud, a radar chart, and two bar plots of location statistics.


Usage of the function:

keystats(key, n = 10000, top_num = 10, graph_col = "dodgerblue3")


Arguments of the function:

key: The keyword or hashtag that the user of this function wishes to analyze n: The number of tweets relating to key that the user of this function wishes to analyze. The maximum number of tweets to analyze at once is 18,000. Default = 10,000 top_num: The number of top words/hashtags/locations displayed in the HTML output. Default = 10 graph_col: The color of graphs in the HTML output. Default = "dodgerblue3"

library(tweetstats)
keystats <- function(key,n=10000,top_num=10, graph_col = "dodgerblue3"){
  if(!is.numeric(n))
    stop("n must be numeric number no more than 18000", call. = FALSE)
  if(n>18000)
    stop("n should not exceed 18000", call. = FALSE)
  if(!is.numeric(top_num))
    stop("top_num must be numeric")
  key <- deparse(substitute(key))
  n <- deparse(substitute(n))
  top_num <- deparse(substitute(top_num))
  graph_col <- deparse(substitute(graph_col))

  # read in the template and modify it
  report <- readLines("inst/keystatsTemplate.txt")
  report <- gsub("xxxnum", n, report, fixed = TRUE)
  report <- gsub("xxxkeyword", key, report, fixed = TRUE)
  report <- gsub("xxxtop_num", top_num, report, fixed = TRUE)
  report <- gsub("xxxfill", graph_col, report, fixed = TRUE)

  # write out template
  tf <- tempfile(fileext = ".Rmd")
  to <- tempfile(fileext = ".Html")
  writeLines(report, tf)

  # render R markdown and display
  library(rmarkdown)
  render(input = tf,
         output_format = "html_document",
         output_file = to)
  file.show(to)

  invisible(to)
}


nurahjaradat/tweetstats documentation built on Dec. 16, 2019, 10:51 p.m.