-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kearney
committed
Aug 29, 2017
0 parents
commit e027767
Showing
13 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^make\.R$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.