Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Edmondson authored and cran-robot committed Jun 13, 2016
0 parents commit d27b1f8
Show file tree
Hide file tree
Showing 45 changed files with 3,022 additions and 0 deletions.
28 changes: 28 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,28 @@
Package: bigQueryR
Title: Interface with Google BigQuery with Shiny Compatibility
Version: 0.1.0
Authors@R: c(person("Mark", "Edmondson",email = "r@sunholo.com",
role = c("aut", "cre")),
person("Hadley", "Wickham", , "hadley@rstudio.com", role = "ctb")
)
Description: Interface with Google BigQuery,
see <https://cloud.google.com/bigquery/> for more information.
This package uses 'googleAuthR' so is compatible with similar packages,
including Google Cloud Storage (<https://cloud.google.com/storage/>) for result extracts.
URL: http://code.markedmondson.me/bigQueryR/
BugReports: https://github.com/MarkEdmondson1234/bigQueryR/issues
License: MIT + file LICENSE
LazyData: TRUE
Depends: R (>= 3.2.0)
Imports: googleAuthR (>= 0.2.0)
Suggests: shiny (>= 0.12.1), jsonlite (>= 0.9.21), httr (>= 1.1.0),
knitr, rmarkdown
RoxygenNote: 5.0.1
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2016-06-13 15:27:06 UTC; mark
Author: Mark Edmondson [aut, cre],
Hadley Wickham [ctb]
Maintainer: Mark Edmondson <r@sunholo.com>
Repository: CRAN
Date/Publication: 2016-06-13 17:34:46
2 changes: 2 additions & 0 deletions LICENSE
@@ -0,0 +1,2 @@
YEAR: 2015
COPYRIGHT HOLDER: Sunholo Ltd.
44 changes: 44 additions & 0 deletions MD5
@@ -0,0 +1,44 @@
6af3f02cc22d5969fa9e040899540ae8 *DESCRIPTION
6346ec78b27187fcd99986124133fc2a *LICENSE
3e07399cea02e89908a577cf166f9a3e *NAMESPACE
0724adb8834a104d543457e8904259b8 *NEWS.md
648363052aba9854b423375c99657918 *R/bigQueryR.R
01672f25679abbfa0707f48efd714348 *R/cloudStorage.R
9b3a51b950a50b6ecd6b406a1be1369a *R/dataParseFunctions.R
6e7d4522868a48ce86b745c493504282 *R/downloadData.R
548d7f98d9c1a887ff3c5cfd33d79851 *R/jobs.R
2e82d5ec073e9d2b36ae027e193f7cbf *R/listBigQuery.R
440c9973dcb208844c3edb429d0b9802 *R/options.R
f772297399468e70b3e420a1516a1351 *R/query.R
c8a80e9159390e4888025b8a8910b029 *R/tables.R
a72bfa91dafebe6fe204accf2009264c *R/uploadData.R
f20c09c89a63a3f153f162a16cd64c95 *R/utilities.R
410890d9694f6f7855d320d4c0dd7a62 *README.md
b6bf9fa119de5ecadc2965be4246d273 *build/vignette.rds
9b4c6c4550611f20e113115fb16deda0 *inst/doc/bigQueryR.Rmd
53ec04dfb822bc54a4119184b36dc788 *inst/doc/bigQueryR.html
0ab1a7ba9227ed7bdd95302ad44b2645 *man/bigQueryR.Rd
21dc463667cc1fcdf1031f8d75f666d2 *man/bqr_auth.Rd
57e2b5ffc089a2058165301bb7a1514e *man/bqr_create_table.Rd
e56931b4b933cbb94f91186d9bfba040 *man/bqr_delete_table.Rd
5cead0ce5bf082e5f4ff3c443f396f5b *man/bqr_extract_data.Rd
31a7d04a4f905fe896d51917ef753c4d *man/bqr_get_job.Rd
ccbce4a844d3631cf75d6cc173e67ea3 *man/bqr_grant_extract_access.Rd
eb00021cde39fd03185b5325e3db586e *man/bqr_list_datasets.Rd
3242264356742e6692fd2381b94fe97f *man/bqr_list_jobs.Rd
384fc76e5ea61dea13447d889433f663 *man/bqr_list_projects.Rd
1a1da54c1496a3dd366a896fec099a5b *man/bqr_list_tables.Rd
8af9a2131e608514747ddd274f42aa03 *man/bqr_query.Rd
77e44347fe63363cc693ed538c7b0da1 *man/bqr_query_asynch.Rd
10c0c1853541fe29b740003e4b007400 *man/bqr_table_data.Rd
75e152274b3fb966adcb1b34517787c9 *man/bqr_table_meta.Rd
48d94d31a9d1721c054e97c29c2c17ff *man/bqr_upload_data.Rd
a2f3694cd0d6e26e4d2cb131c35ad424 *man/error.message.Rd
0d884342e1dce6a8d9dde386ccefa52f *man/gcs_update_acl.Rd
691565ef7e3c837f1f51d4d6b1b7680a *man/idempotency.Rd
2240e92cd7767970a3f56d45070895a9 *man/is.NullOb.Rd
57e5ee063d87b8133cb4785429c9685a *man/is.error.Rd
172de60e13e30d803506104947d1f6b7 *man/myMessage.Rd
5eeba1d521d9a6dda8a61512f6891b9a *man/parse_bqr_query.Rd
fc01bed2025f8af7aff31ab1c21bfef7 *man/rmNullObs.Rd
9b4c6c4550611f20e113115fb16deda0 *vignettes/bigQueryR.Rmd
17 changes: 17 additions & 0 deletions NAMESPACE
@@ -0,0 +1,17 @@
# Generated by roxygen2: do not edit by hand

export(bqr_auth)
export(bqr_create_table)
export(bqr_delete_table)
export(bqr_extract_data)
export(bqr_get_job)
export(bqr_grant_extract_access)
export(bqr_list_datasets)
export(bqr_list_jobs)
export(bqr_list_projects)
export(bqr_list_tables)
export(bqr_query)
export(bqr_query_asynch)
export(bqr_table_data)
export(bqr_table_meta)
export(bqr_upload_data)
7 changes: 7 additions & 0 deletions NEWS.md
@@ -0,0 +1,7 @@
# bigQueryR 0.1.0

* Added a `NEWS.md` file to track changes to the package.
* Initial release



45 changes: 45 additions & 0 deletions R/bigQueryR.R
@@ -0,0 +1,45 @@
#' bigQueryR
#'
#' Provides an interface with Google BigQuery
#'
#' @seealso \url{https://cloud.google.com/bigquery/docs/reference/v2/?hl=en}
#'
#' @docType package
#' @name bigQueryR
NULL

#' Do OAuth2 authentication
#'
#' @param token An existing OAuth2 token, if you have one.
#' @param new_user Set to TRUE if you want to go through the authentication flow again.
#'
#' @details
#' This function just wraps \code{\link[googleAuthR]{gar_auth}} from googleAuthR,
#' but means you don't need to explictly load that library.
#'
#' @seealso \code{\link[googleAuthR]{gar_auth}}
#'
#' @examples
#'
#' \dontrun{
#' library(bigQueryR)
#'
#' ## this will open your browser
#' ## Authenticate with an email that has access to the BigQuery project you need
#' bqr_auth()
#'
#' ## verify under a new user
#' bqr_auth(new_user=TRUE)
#'
#' }
#'
#' @family bigQuery meta functions
#' @export
bqr_auth <- function(token=NULL, new_user=FALSE){
options("googleAuthR.scopes.selected" = getOption("bigQueryR.scope") )
options("googleAuthR.client_id" = getOption("bigQueryR.client_id"))
options("googleAuthR.client_secret" = getOption("bigQueryR.client_secret"))
options("googleAuthR.webapp.client_id" = getOption("bigQueryR.webapp.client_id"))
options("googleAuthR.webapp.client_secret" = getOption("bigQueryR.webapp.client_secret"))
googleAuthR::gar_auth(token=token, new_user=new_user)
}
104 changes: 104 additions & 0 deletions R/cloudStorage.R
@@ -0,0 +1,104 @@
#' Update Google Cloud Storage ObjectAccessControls
#'
#' Requires scopes set in bigQuery.scopes
#' \code{https://www.googleapis.com/auth/devstorage.full_control}
#' \code{https://www.googleapis.com/auth/cloud-platform}
#'
#' @param bucket Google Cloud Storage bucket
#' @param object Object to update
#' @param entity entity to update or add
#' @param entity_type what type of entity
#' @param role Access permission for entity
#'
#' @seealso \href{https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/insert}{objectAccessControls on Google API reference}
#'
#' @examples
#'
#' \dontrun{
#' library(bigQueryR)
#'
#' ## Auth with a project that has at least BigQuery and Google Cloud Storage scope
#' bqr_auth()
#'
#' ## make a big query
#' job <- bqr_query_asynch("your_project",
#' "your_dataset",
#' "SELECT * FROM blah LIMIT 9999999",
#' destinationTableId = "bigResultTable")
#'
#' ## poll the job to check its status
#' ## its done when job$status$state == "DONE"
#' bqr_get_job("your_project", job$jobReference$jobId)
#'
#' ##once done, the query results are in "bigResultTable"
#' ## extract that table to GoogleCloudStorage:
#' # Create a bucket at Google Cloud Storage at
#' # https://console.cloud.google.com/storage/browser
#'
#' job_extract <- bqr_extract_data("your_project",
#' "your_dataset",
#' "bigResultTable",
#' "your_cloud_storage_bucket_name")
#'
#' ## poll the extract job to check its status
#' ## its done when job$status$state == "DONE"
#' bqr_get_job("your_project", job_extract$jobReference$jobId)
#'
#' ## to download via a URL and not logging in via Google Cloud Storage interface:
#' ## Use an email that is Google account enabled
#' ## Requires scopes:
#' ## https://www.googleapis.com/auth/devstorage.full_control
#' ## https://www.googleapis.com/auth/cloud-platform
#' ## set via options("bigQueryR.scopes") and reauthenticate if needed
#'
#' download_url <- bqr_grant_extract_access(job_extract, "your@email.com")
#'
#' ## download_url may be multiple if the data is > 1GB
#'
#' }
#'
#' @family bigQuery upload functions
#' @return TRUE if successful
gcs_update_acl <- function(bucket,
object,
entity,
entity_type = c("user",
"group",
"domian",
"project",
"allUsers",
"allAuthenticatedUsers"),
role = c("READER","OWNER")){

entity_type <- match.arg(entity_type)
role <- match.arg(role)

stopifnot(inherits(bucket, "character"),
inherits(object, "character"),
inherits(entity, "character"))

accessControls <- list(
entity = paste0(entity_type,"-",entity),
role = role
)

insert <-
googleAuthR::gar_api_generator("https://www.googleapis.com/storage/v1",
"POST",
path_args = list(b = bucket,
o = object,
acl = ""))

req <- insert(path_arguments = list(b = bucket, o = object),
the_body = accessControls)

if(req$status_code == 200){
myMessage("Access updated")
out <- TRUE
} else {
stop("Error setting access")
}

out

}
36 changes: 36 additions & 0 deletions R/dataParseFunctions.R
@@ -0,0 +1,36 @@
#' Parse table data
#'
#' @keywords internal
parse_bqr_query <- function(x){

converter <- list(
integer = as.integer,
float = as.double,
boolean = as.logical,
string = identity,
timestamp = function(x) as.POSIXct(as.integer(x), origin = "1970-01-01", tz = "UTC")
)

schema <- x$schema$fields

## when only one row, it makes it 3 obs. of 1 variable instead of 1 obs. of 3
data_f <- as.data.frame(Reduce(rbind, lapply(x$rows$f, function(x) x$v)),
stringsAsFactors = FALSE)

types <- tolower(schema$type)

out <- vector("list", length(types))
for(i in seq_along(types)){
## this needs to behave when only length 1
out[[i]] <- converter[[types[i]]](data_f[,i])
}
names(out) <- schema$name

out <- as.data.frame(out, stringsAsFactors = FALSE)
attr(out, "jobReference") <- x$jobReference
attr(out, "pageToken") <- x$pageToken

out


}

0 comments on commit d27b1f8

Please sign in to comment.