Skip to content

Commit

Permalink
pkg docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kearney committed Aug 29, 2017
0 parents commit e027767
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
^make\.R$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Rproj.user
.Rhistory
.RData
10 changes: 10 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", 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
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(analyze_sentiment)
export(api_call)
export(googleapis_token)
42 changes: 42 additions & 0 deletions R/analyze_sentiment.R
Original file line number Diff line number Diff line change
@@ -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
)
}
63 changes: 63 additions & 0 deletions R/api-call.R
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 28 additions & 0 deletions R/auth.R
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 19 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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()
}
21 changes: 21 additions & 0 deletions googleapis.Rproj
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions make.R
Original file line number Diff line number Diff line change
@@ -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

17 changes: 17 additions & 0 deletions man/analyze_sentiment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/googleapis_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/update_api_base_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e027767

Please sign in to comment.