From d7a187517b1229e54e5ae0c627aa725990d3caeb Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Mon, 16 Feb 2015 15:37:35 -0800 Subject: [PATCH 1/2] new fxn tbl() to coerce gist class or list of gist class objects to df #25 also changed delete fxn to act on gist class or tbl class as eg of various gist actions on data.frames --- DESCRIPTION | 3 +- NAMESPACE | 6 ++++ R/delete.R | 21 +++++++++++--- R/tbl.R | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ man/delete.Rd | 10 +++++-- man/tbl.Rd | 37 +++++++++++++++++++++++++ 6 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 R/tbl.R create mode 100644 man/tbl.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 105c739..9bd62e1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,8 @@ Imports: httr, magrittr, assertthat, - knitr + knitr, + dplyr Suggests: roxygen2, testthat diff --git a/NAMESPACE b/NAMESPACE index 37e8b87..312b9ac 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,9 +4,13 @@ S3method(as.gist,character) S3method(as.gist,gist) S3method(as.gist,list) S3method(as.gist,numeric) +S3method(delete,gist) +S3method(delete,tbl) S3method(print,commit) S3method(print,gist) S3method(print,gist_rate) +S3method(tbl,gist) +S3method(tbl,list) export("%>%") export(add_files) export(as.gist) @@ -27,10 +31,12 @@ export(rename_files) export(run) export(star) export(star_check) +export(tbl) export(unstar) export(update) export(update_files) import(assertthat) import(httr) import(knitr) +importFrom(dplyr,rbind_all) importFrom(magrittr,"%>%") diff --git a/R/delete.R b/R/delete.R index 815c860..03db421 100644 --- a/R/delete.R +++ b/R/delete.R @@ -1,16 +1,29 @@ #' Delete a gist #' -#' @param gist A gist object or something coerceable to a gist -#' @template all #' @export +#' @param x A gist object or something coerceable to a gist +#' @template all +#' #' @examples \dontrun{ #' gists("minepublic")[[29]] %>% delete() #' } +delete <- function(x, ...) UseMethod("delete") -delete <- function(gist, ...) +#' @export +#' @rdname delete +delete.gist <- function(x, ...) { - gist <- as.gist(gist) + gist <- as.gist(x) res <- gist_DELETE(paste0(ghbase(), '/gists/', gist$id), auth = gist_auth(), headers = ghead(), ...) stop_for_status(res) message('Your gist has been deleted') } + +#' @export +#' @rdname delete +delete.tbl <- function(x, ...) invisible(lapply(x$id, delgist, ...)) + +delgist <- function(z, ...){ + res <- gist_DELETE(paste0(ghbase(), '/gists/', z), auth = gist_auth(), headers = ghead(), ...) + warn_for_status(res) +} diff --git a/R/tbl.R b/R/tbl.R new file mode 100644 index 0000000..7a15d1a --- /dev/null +++ b/R/tbl.R @@ -0,0 +1,77 @@ +#' Make a table from gist class or list of gist class objects +#' +#' @export +#' @importFrom dplyr rbind_all +#' @param x Either a gist class object or a list of many gist class objects +#' @examples \dontrun{ +#' x <- as.gist('f1403260eb92f5dfa7e1') +#' tbl(x) +#' +#' # from a list +#' ss <- gists('minepublic') +#' tbl(ss[1:3]) +#' ## manipulate with dplyr +#' library("dplyr") +#' tbl(gists("minepublic")[1:4]) %>% +#' select(id, description, owner_login) %>% +#' filter(grepl("gist gist gist", description)) %>% +#' delete +#' } +tbl <- function(x) UseMethod("tbl") + +#' @export +#' @rdname tbl +tbl.gist <- function(x){ + singles <- move_cols(data.frame(null2na(x[ names(x) %in% snames ]), stringsAsFactors = FALSE), "id") + others <- x[ !names(x) %in% snames ] + files <- lappdf(others$files, "files") + owner <- data.frame(others$owner, stringsAsFactors = FALSE) + owner <- if(NROW(owner) == 0) owner else setNames(owner, paste0("owner_", names(owner))) + forks <- lappdf(others$forks, "forks") + history <- lappdf(others$history, "history") + cbind_fill(singles, files, owner, forks, history, as_df = TRUE) +} + +#' @export +#' @rdname tbl +tbl.list <- function(x) rbind_all(lapply(x, tbl)) + +snames <- c("url","forks_url", "commits_url", "id", "git_pull_url", + "git_push_url", "html_url", "public", "created_at", + "updated_at", "description", "comments", "user", "comments_url") + +lappdf <- function(x, prefix=NULL){ + tmp <- data.frame(rbind_all(lapply(x, function(z){ + data.frame(null2na(z), stringsAsFactors=FALSE) + })), stringsAsFactors=FALSE) + if(!is.null(prefix)){ + if(NROW(tmp) == 0){ + tmp + } else { + setNames( tmp, paste0(prefix, "_", names(tmp)) ) + } + } else { + tmp + } +} + +null2na <- function(x){ + x[sapply(x, is.null)] <- NA + x +} + +cbind_fill <- function(..., as_df = FALSE){ + nm <- list(...) + nm <-lapply(nm, as.matrix) + n <- max(sapply(nm, nrow)) + temp <- do.call(cbind, lapply(nm, function (x) + rbind(x, matrix(, n-nrow(x), ncol(x))))) + if(as_df){ + data.frame(temp, stringsAsFactors = FALSE) + } else { + temp + } +} + +move_cols <- function(x, y) + x[ c(y, names(x)[-sapply(y, function(z) grep(paste0('\\b', z, '\\b'), names(x)))]) ] diff --git a/man/delete.Rd b/man/delete.Rd index 64929de..f792ffc 100644 --- a/man/delete.Rd +++ b/man/delete.Rd @@ -2,12 +2,18 @@ % Please edit documentation in R/delete.R \name{delete} \alias{delete} +\alias{delete.gist} +\alias{delete.tbl} \title{Delete a gist} \usage{ -delete(gist, ...) +delete(x, ...) + +\method{delete}{gist}(x, ...) + +\method{delete}{tbl}(x, ...) } \arguments{ -\item{gist}{A gist object or something coerceable to a gist} +\item{x}{A gist object or something coerceable to a gist} \item{...}{Curl options passed on to httr::GET} } diff --git a/man/tbl.Rd b/man/tbl.Rd new file mode 100644 index 0000000..04f98f3 --- /dev/null +++ b/man/tbl.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/tbl.R +\name{tbl} +\alias{tbl} +\alias{tbl.gist} +\alias{tbl.list} +\title{Make a table from gist class or list of gist class objects} +\usage{ +tbl(x) + +\method{tbl}{gist}(x) + +\method{tbl}{list}(x) +} +\arguments{ +\item{x}{Either a gist class object or a list of many gist class objects} +} +\description{ +Make a table from gist class or list of gist class objects +} +\examples{ +\dontrun{ +x <- as.gist('f1403260eb92f5dfa7e1') +tbl(x) + +# from a list +ss <- gists('minepublic') +tbl(ss[1:3]) +## manipulate with dplyr +library("dplyr") +tbl(gists("minepublic")[1:4]) \%>\% + select(id, description, owner_login) \%>\% + filter(grepl("gist gist gist", description)) \%>\% + delete +} +} + From 77b3fc4b684a1175e854d4adc5bc81aa7aea79e4 Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Mon, 16 Feb 2015 15:43:54 -0800 Subject: [PATCH 2/2] changed tbl fxn to tabl to avoid dplyr confusion --- NAMESPACE | 6 +++--- R/tbl.R | 16 ++++++++-------- man/{tbl.Rd => tabl.Rd} | 20 ++++++++++---------- 3 files changed, 21 insertions(+), 21 deletions(-) rename man/{tbl.Rd => tabl.Rd} (77%) diff --git a/NAMESPACE b/NAMESPACE index 312b9ac..8a53a62 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,8 +9,8 @@ S3method(delete,tbl) S3method(print,commit) S3method(print,gist) S3method(print,gist_rate) -S3method(tbl,gist) -S3method(tbl,list) +S3method(tabl,gist) +S3method(tabl,list) export("%>%") export(add_files) export(as.gist) @@ -31,7 +31,7 @@ export(rename_files) export(run) export(star) export(star_check) -export(tbl) +export(tabl) export(unstar) export(update) export(update_files) diff --git a/R/tbl.R b/R/tbl.R index 7a15d1a..b16463f 100644 --- a/R/tbl.R +++ b/R/tbl.R @@ -5,23 +5,23 @@ #' @param x Either a gist class object or a list of many gist class objects #' @examples \dontrun{ #' x <- as.gist('f1403260eb92f5dfa7e1') -#' tbl(x) +#' tabl(x) #' #' # from a list #' ss <- gists('minepublic') -#' tbl(ss[1:3]) +#' tabl(ss[1:3]) #' ## manipulate with dplyr #' library("dplyr") -#' tbl(gists("minepublic")[1:4]) %>% +#' tabl(gists("minepublic")[1:10]) %>% #' select(id, description, owner_login) %>% #' filter(grepl("gist gist gist", description)) %>% #' delete #' } -tbl <- function(x) UseMethod("tbl") +tabl <- function(x) UseMethod("tabl") #' @export -#' @rdname tbl -tbl.gist <- function(x){ +#' @rdname tabl +tabl.gist <- function(x){ singles <- move_cols(data.frame(null2na(x[ names(x) %in% snames ]), stringsAsFactors = FALSE), "id") others <- x[ !names(x) %in% snames ] files <- lappdf(others$files, "files") @@ -33,8 +33,8 @@ tbl.gist <- function(x){ } #' @export -#' @rdname tbl -tbl.list <- function(x) rbind_all(lapply(x, tbl)) +#' @rdname tabl +tabl.list <- function(x) rbind_all(lapply(x, tabl)) snames <- c("url","forks_url", "commits_url", "id", "git_pull_url", "git_push_url", "html_url", "public", "created_at", diff --git a/man/tbl.Rd b/man/tabl.Rd similarity index 77% rename from man/tbl.Rd rename to man/tabl.Rd index 04f98f3..bb4d12f 100644 --- a/man/tbl.Rd +++ b/man/tabl.Rd @@ -1,16 +1,16 @@ % Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/tbl.R -\name{tbl} -\alias{tbl} -\alias{tbl.gist} -\alias{tbl.list} +\name{tabl} +\alias{tabl} +\alias{tabl.gist} +\alias{tabl.list} \title{Make a table from gist class or list of gist class objects} \usage{ -tbl(x) +tabl(x) -\method{tbl}{gist}(x) +\method{tabl}{gist}(x) -\method{tbl}{list}(x) +\method{tabl}{list}(x) } \arguments{ \item{x}{Either a gist class object or a list of many gist class objects} @@ -21,14 +21,14 @@ Make a table from gist class or list of gist class objects \examples{ \dontrun{ x <- as.gist('f1403260eb92f5dfa7e1') -tbl(x) +tabl(x) # from a list ss <- gists('minepublic') -tbl(ss[1:3]) +tabl(ss[1:3]) ## manipulate with dplyr library("dplyr") -tbl(gists("minepublic")[1:4]) \%>\% +tabl(gists("minepublic")[1:10]) \%>\% select(id, description, owner_login) \%>\% filter(grepl("gist gist gist", description)) \%>\% delete