From e027767f60f430216d77487f035693d06113d533 Mon Sep 17 00:00:00 2001 From: Kearney Date: Tue, 29 Aug 2017 15:59:12 -0500 Subject: [PATCH] pkg docs --- .Rbuildignore | 3 ++ .gitignore | 3 ++ DESCRIPTION | 10 ++++++ NAMESPACE | 5 +++ R/analyze_sentiment.R | 42 +++++++++++++++++++++++++ R/api-call.R | 63 ++++++++++++++++++++++++++++++++++++++ R/auth.R | 28 +++++++++++++++++ R/utils.R | 19 ++++++++++++ googleapis.Rproj | 21 +++++++++++++ make.R | 9 ++++++ man/analyze_sentiment.Rd | 17 ++++++++++ man/googleapis_token.Rd | 11 +++++++ man/update_api_base_url.Rd | 18 +++++++++++ 13 files changed, 249 insertions(+) create mode 100644 .Rbuildignore create mode 100644 .gitignore create mode 100644 DESCRIPTION create mode 100644 NAMESPACE create mode 100644 R/analyze_sentiment.R create mode 100644 R/api-call.R create mode 100644 R/auth.R create mode 100644 R/utils.R create mode 100644 googleapis.Rproj create mode 100644 make.R create mode 100644 man/analyze_sentiment.Rd create mode 100644 man/googleapis_token.Rd create mode 100644 man/update_api_base_url.Rd diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..fd6eac7 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,3 @@ +^.*\.Rproj$ +^\.Rproj\.user$ +^make\.R$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..807ea25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..3cedb9b --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,10 @@ +Package: googleapis +Title: What the Package Does (one line, title case) +Version: 0.0.0.9000 +Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre")) +Description: What the package does (one paragraph). +Depends: R (>= 3.4.1) +License: What license is it under? +Encoding: UTF-8 +LazyData: true +RoxygenNote: 6.0.1 diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..1e6e709 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,5 @@ +# Generated by roxygen2: do not edit by hand + +export(analyze_sentiment) +export(api_call) +export(googleapis_token) diff --git a/R/analyze_sentiment.R b/R/analyze_sentiment.R new file mode 100644 index 0000000..22d0e06 --- /dev/null +++ b/R/analyze_sentiment.R @@ -0,0 +1,42 @@ + +#' analyze_sentiment +#' +#' Returns sentiment analyzis from Google cloud language API. +#' +#' @param data Vector of plain text to analyze. +#' @return List of parsed response objects. +#' @export +analyze_sentiment <- function(data) { + eval(call("analyze_sentiment_", data)) +} + +analyze_sentiment_ <- function(text) { + analyze_sentiment_internal <- function(text) { + ## API path + path <- "analyzeSentiment" + ## format text for request + text <- jsonify_text(text) + ## execute request + r <- httr::POST(api_call(path), body = text) + ## parse + parser(r) + } + lapply(text, analyze_sentiment_internal) +} + +parser <- function(x) jsonlite::fromJSON(httr::content(x, as = "text", encoding = "UTF-8")) + +jsonify_text <- function(text) { + lst <- list( + encodingType = "UTF8", + document = list( + type = "PLAIN_TEXT", + content = text + ) + ) + jsonlite::toJSON( + lst, + pretty = TRUE, + auto_unbox = TRUE + ) +} diff --git a/R/api-call.R b/R/api-call.R new file mode 100644 index 0000000..d57ddca --- /dev/null +++ b/R/api-call.R @@ -0,0 +1,63 @@ + +api_base <- function() { + baseurl <- getOption("googleapisbaseurl") + if (baseurl == "" || is.null(baseurl)) { + options( + googleapisbaseurl = list( + scheme = "https", + base = "language.googleapis.com", + version = "v1" + ) + ) + } + baseurl <- getOption("googleapisbaseurl") + paste0(baseurl$scheme, "://", baseurl$base, "/", baseurl$version) +} + + +#' update base url +#' +#' @param scheme http or https +#' @param base base api, e.g., api.twitter.com +#' @param version version string, e.g., v1.1 +update_api_base_url <- function(scheme, base, version) { + abu <- getOption("googleapisbaseurl") + abu[["scheme"]] <- scheme + abu[["base"]] <- base + abu[["version"]] <- version + options(googleapisbaseurl = abu) +} + + +#' api_call +#' +#' Composes API requests +#' +#' @param path Specific API hosted at base site. +#' @param ... Other named args are converted as query parameters. +#' @export +#' @noRd +api_call <- function(path, ..., token = NULL) { + ## add documents: if not already + if (!grepl("^documents:", path)) { + path <- paste0("documents:", path) + } + ## base URL + base <- api_base() + ## params + params <- c(...) + params <- params[names(params) != ""] + ## if no key provided, find it + if (!"key" %in% names(params) && is.null(token)) { + params["key"] <- googleapis_token() + } else if (!"key" %in% names(params) && !is.null(token)) { + params["key"] <- token + } + if (length(params) > 0L) { + params <- paste(names(params), params, sep = "=") + params <- paste(params, collapse = "&") + params <- paste0("?", params) + } + ## build complete request + paste0(base, "/", path, params) +} diff --git a/R/auth.R b/R/auth.R new file mode 100644 index 0000000..4e5e370 --- /dev/null +++ b/R/auth.R @@ -0,0 +1,28 @@ + +#' token +#' +#' Executes authorization method(s). +#' +#' @export +googleapis_token <- function() { + PKG_KEY <- paste0(toupper("googleapis"), "_KEY") + if (!PKG_KEY %in% names(Sys.getenv())) { + ## check renv file + home_dir <- normalizePath("~") + renv_pat <- file.path(home_dir, ".Renviron") + check_renv(renv_pat) + key <- readline_("Please enter your API key below:") + KEY_PAT <- paste0(toupper("googleapis"), "_KEY") + ## set key + .Internal(Sys.setenv(KEY_PAT, key)) + new_env_var <- paste0(KEY_PAT, "=", key) + ## save key + cat( + new_env_var, + file = renv_pat, + fill = TRUE, + append = TRUE + ) + } + Sys.getenv(PKG_KEY) +} diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..0a40e4a --- /dev/null +++ b/R/utils.R @@ -0,0 +1,19 @@ + +menuline <- function(q, a) { + message(q) + menu(a) +} + +readline_ <- function(...) { + input <- readline(paste(c(...), collapse = "")) + gsub("^\"|\"$", "", input) +} + +check_renv <- function(path) { + con <- file(path) + x <- readLines(con, warn = FALSE) + close(con) + x <- paste(x, collapse = "\n") + cat(x, file = path, fill = TRUE) + invisible() +} diff --git a/googleapis.Rproj b/googleapis.Rproj new file mode 100644 index 0000000..0d0f923 --- /dev/null +++ b/googleapis.Rproj @@ -0,0 +1,21 @@ +Version: 1.0 + +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: XeLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/make.R b/make.R new file mode 100644 index 0000000..34a7660 --- /dev/null +++ b/make.R @@ -0,0 +1,9 @@ +devtools::load_all() +api_call("asdf") + +rt <- rtweet::search_tweets("trump is the worst president", include_rts = FALSE) +text <- rt$text[1:5] + +x <- analyze_sentiment(text) +x + diff --git a/man/analyze_sentiment.Rd b/man/analyze_sentiment.Rd new file mode 100644 index 0000000..fe2856e --- /dev/null +++ b/man/analyze_sentiment.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/analyze_sentiment.R +\name{analyze_sentiment} +\alias{analyze_sentiment} +\title{analyze_sentiment} +\usage{ +analyze_sentiment(data) +} +\arguments{ +\item{data}{Vector of plain text to analyze.} +} +\value{ +List of parsed response objects. +} +\description{ +Returns sentiment analyzis from Google cloud language API. +} diff --git a/man/googleapis_token.Rd b/man/googleapis_token.Rd new file mode 100644 index 0000000..7a6b538 --- /dev/null +++ b/man/googleapis_token.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/auth.R +\name{googleapis_token} +\alias{googleapis_token} +\title{token} +\usage{ +googleapis_token() +} +\description{ +Executes authorization method(s). +} diff --git a/man/update_api_base_url.Rd b/man/update_api_base_url.Rd new file mode 100644 index 0000000..a039cba --- /dev/null +++ b/man/update_api_base_url.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/api-call.R +\name{update_api_base_url} +\alias{update_api_base_url} +\title{update base url} +\usage{ +update_api_base_url(scheme, base, version) +} +\arguments{ +\item{scheme}{http or https} + +\item{base}{base api, e.g., api.twitter.com} + +\item{version}{version string, e.g., v1.1} +} +\description{ +update base url +}