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..8a53a62 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(tabl,gist) +S3method(tabl,list) export("%>%") export(add_files) export(as.gist) @@ -27,10 +31,12 @@ export(rename_files) export(run) export(star) export(star_check) +export(tabl) 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..b16463f --- /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') +#' tabl(x) +#' +#' # from a list +#' ss <- gists('minepublic') +#' tabl(ss[1:3]) +#' ## manipulate with dplyr +#' library("dplyr") +#' tabl(gists("minepublic")[1:10]) %>% +#' select(id, description, owner_login) %>% +#' filter(grepl("gist gist gist", description)) %>% +#' delete +#' } +tabl <- function(x) UseMethod("tabl") + +#' @export +#' @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") + 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 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", + "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/tabl.Rd b/man/tabl.Rd new file mode 100644 index 0000000..bb4d12f --- /dev/null +++ b/man/tabl.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/tbl.R +\name{tabl} +\alias{tabl} +\alias{tabl.gist} +\alias{tabl.list} +\title{Make a table from gist class or list of gist class objects} +\usage{ +tabl(x) + +\method{tabl}{gist}(x) + +\method{tabl}{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') +tabl(x) + +# from a list +ss <- gists('minepublic') +tabl(ss[1:3]) +## manipulate with dplyr +library("dplyr") +tabl(gists("minepublic")[1:10]) \%>\% + select(id, description, owner_login) \%>\% + filter(grepl("gist gist gist", description)) \%>\% + delete +} +} +