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 username, allowing the user to search multiple usernames at one time, specify which year they would like summary information for, specify the number of tweets they would like to analyze (with a maximum output allowed of 3,200 tweets), customize the background color of output tables, and customize the text color of the output with the assumption of knowledge of HTML color codes.


Usage of the function:

userstats(user, n = 1000, bg_col = "white", txt_col = "#1A5276", year = 2019, x = 10)


Arguments of the function:

user: The username of a real Twitter account that the user of this function wishes to analyze n: The number of tweets the user of this function wishes to analyze from the desired account. Default = 1000 bg_col: The background color of all tables in the HTML output. Default = "white" txt_col: The text color of all tables in the HTML output. Default = "#1A5276" year: The year during which the tweets the user wishes to analyze were published. Default = 2019 x: The number of recent tweets the user wishes to display from the indicated account in the HTML output. Default = 10

library(tweetstats)
userstats <- function(user, n=1000, bg_col = "white", txt_col = "#1A5276", year=2019, x=10){
  if(!is.numeric(n))
    stop("n must be numeric number no more than 3200", call. = FALSE)
  if(n>3200)
    stop("n should not exceed 3200", call. = FALSE)
  user <- deparse(substitute(user))
  n <- deparse(substitute(n))
  year <- deparse(substitute(year))
  x <- deparse(substitute(x))
  # read in the template and modify it
  report <- readLines("inst/userstatsTemplate.txt")
  report <- gsub("xxxbg", bg_col, report, fixed = TRUE)
  report <- gsub("xxxnum", n, report, fixed = TRUE)
  report <- gsub("xxxusers", user, report, fixed = TRUE)
  report <- gsub("xxxcol", txt_col, report, fixed = TRUE)
  report <- gsub("xxxyr", year, report, fixed = TRUE)
  report <- gsub("xxxtwt", x, 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.