R/sentiment.R

Defines functions print.available_sentiment format.available_sentiment get_sentiment

Documented in get_sentiment

#' Get sentiment of name
#'
#' @param name Name of package to search
#' @export
get_sentiment <- function(name) {
  # Workaround for https://github.com/juliasilge/tidytext/issues/64
  ("base" %:::% "library")("tidytext")
  on.exit(detach("package:tidytext"))
  sentiment_dictionary <- tidytext::get_sentiments("bing")

  res <- sentiment_dictionary[sentiment_dictionary$word == name, ][["sentiment"]]
  if (length(res) == 0) {
    res <- NA
  }
  structure(res == "positive", class = "available_sentiment")
}

#' @export

format.available_sentiment <- function(x, ...) {
  paste0(
    crayon::bold("Sentiment:"),
    if (is.na(x[[1]])) {
      "???"
    } else if (isTRUE(x[[1]])) {
      crayon::green("+++")
    } else{
      crayon::red("---")
    },
    "\n"
  )
}

#' @export

print.available_sentiment <- function(x, ...) {
  cat(format(x, ...))
  invisible(x)
}

Try the available package in your browser

Any scripts or data that you put into this service are public.

available documentation built on July 18, 2018, 5:04 p.m.