mirror of https://git.sr.ht/~hrbrmstr/freebase
boB Rudis
6 years ago
19 changed files with 255 additions and 3 deletions
@ -1,8 +1,13 @@ |
|||
# Generated by roxygen2: do not edit by hand |
|||
|
|||
export("%>%") |
|||
export(use_detect) |
|||
export(use_infix_helpers) |
|||
export(use_isers) |
|||
export(use_keepers) |
|||
export(use_mappers) |
|||
export(use_safely) |
|||
export(use_walkers) |
|||
import(rprojroot) |
|||
import(usethis) |
|||
importFrom(magrittr,"%>%") |
|||
|
@ -0,0 +1,17 @@ |
|||
#' Use base-ified equivalents of "detect"-ors |
|||
#' |
|||
#' * Creates `R/utils-detect.R` with base R pseudo-equivalents for `purrr` `detect` (et al). |
|||
#' |
|||
#' These support formula functions (i.e. `~{}`). |
|||
#' |
|||
#' @md |
|||
#' @param save_as Where to save/what to name the file. Defaults to "`R/utils-detect.R`" |
|||
#' @param open if `TRUE`, open the resultant file |
|||
#' @export |
|||
use_detect <- function(save_as = "R/utils-detect.R", open = TRUE) { |
|||
|
|||
check_is_package("use_detect()") |
|||
|
|||
use_template("detect.R", save_as = save_as , open = open, package = "freebase") |
|||
|
|||
} |
@ -0,0 +1,15 @@ |
|||
#' Use "is_" functions |
|||
#' |
|||
#' * Creates `R/utils-isers.R` filled with `is_...` testors |
|||
#' |
|||
#' @md |
|||
#' @param save_as Where to save/what to name the file. Defaults to "`R/utils-detect.R`" |
|||
#' @param open if `TRUE`, open the resultant file |
|||
#' @export |
|||
use_isers <- function(save_as = "R/utils-isers.R", open = TRUE) { |
|||
|
|||
check_is_package("use_isers()") |
|||
|
|||
use_template("is-ers.R", save_as = save_as , open = open, package = "freebase") |
|||
|
|||
} |
@ -0,0 +1,18 @@ |
|||
#' Use base-ified equivalents of keep/discard/compact |
|||
#' |
|||
#' * Creates `R/utils-walkers.R` with base R pseudo-equivalents for `purrr` |
|||
#' `walk`/`walk2` |
|||
#' |
|||
#' These support formula functions (i.e. `~{}`). |
|||
#' |
|||
#' @md |
|||
#' @param save_as Where to save/what to name the file. Defaults to "`R/utils-walkers.R`" |
|||
#' @param open if `TRUE`, open the resultant file |
|||
#' @export |
|||
use_walkers <- function(save_as = "R/utils-walkers.R", open = TRUE) { |
|||
|
|||
check_is_package("use_walkers()") |
|||
|
|||
use_template("walkers.R", save_as = save_as , open = open, package = "freebase") |
|||
|
|||
} |
@ -0,0 +1,11 @@ |
|||
#' Pipe operator |
|||
#' |
|||
#' See \code{magrittr::\link[magrittr]{\%>\%}} for details. |
|||
#' |
|||
#' @name %>% |
|||
#' @rdname pipe |
|||
#' @keywords internal |
|||
#' @export |
|||
#' @importFrom magrittr %>% |
|||
#' @usage lhs \%>\% rhs |
|||
NULL |
@ -0,0 +1,49 @@ |
|||
# NOTE: is_true() is also in use_isers() |
|||
is_true <- function (x) identical(x, TRUE) |
|||
|
|||
# These support formula functions (i.e. `~{}`). |
|||
detect <- function (.x, .f, ..., .right = FALSE, .p) { |
|||
|
|||
if (inherits(.f, "formula")) { |
|||
.body <- dimnames(attr(terms(.f), "factors"))[[1]] |
|||
.f <- function(.x, .=.x) {} |
|||
body(.f) <- as.expression(parse(text=.body)) |
|||
} |
|||
|
|||
for (i in index(.x, .right)) { |
|||
if (inherits(.f, "function")) { |
|||
if (is_true(.f(.x[[i]], ...))) return(.x[[i]]) |
|||
} else { |
|||
if (is_true(.x[[i]][[.f]])) return(.x[[i]]) |
|||
} |
|||
} |
|||
|
|||
NULL |
|||
|
|||
} |
|||
|
|||
detect_index <- function (.x, .f, ..., .right = FALSE, .p) { |
|||
|
|||
if (inherits(.f, "formula")) { |
|||
.body <- dimnames(attr(terms(.f), "factors"))[[1]] |
|||
.f <- function(.x, .=.x) {} |
|||
body(.f) <- as.expression(parse(text=.body)) |
|||
} |
|||
|
|||
for (i in index(.x, .right)) { |
|||
if (inherits(.f, "function")) { |
|||
if (is_true(.f(.x[[i]], ...))) return(i) |
|||
} else { |
|||
if (is_true(.x[[i]][[.f]])) return(i) |
|||
} |
|||
} |
|||
|
|||
0L |
|||
|
|||
} |
|||
|
|||
index <- function (x, right = FALSE) { |
|||
idx <- seq_along(x) |
|||
if (right) idx <- rev(idx) |
|||
idx |
|||
} |
@ -0,0 +1,7 @@ |
|||
is_even <- function(x) x %% 2 == 0 |
|||
is_odd <- function(x) x %% 2 != 0 |
|||
|
|||
is_empty <- function(x) length(x) == 0 |
|||
|
|||
is_true <- function (x) identical(x, TRUE) |
|||
is_false <- function(x) identical(x, FALSE) |
@ -0,0 +1,37 @@ |
|||
# NOTE these aren't 100% equivalent to the purrr mappers but cover very common use-cases |
|||
# |
|||
# NOTE formula function (e.g. ~{}) are 100% supported |
|||
|
|||
walk <- function(.x, .f, ...) { |
|||
|
|||
if (inherits(.f, "formula")) { |
|||
.body <- dimnames(attr(terms(.f), "factors"))[[1]] |
|||
.f <- function(.x, . = .x) {} |
|||
body(.f) <- as.expression(parse(text=.body)) |
|||
} |
|||
|
|||
if (inherits(.f, "function")) { |
|||
lapply(.x, .f, ...) |
|||
} else { |
|||
stop("I'm not sure indexing by name or number makes sense for walk().") |
|||
} |
|||
|
|||
invisible(.x) |
|||
|
|||
} |
|||
|
|||
walk2 <- function(.x, .y, .f, ...) { |
|||
|
|||
if (inherits(.f, "formula")) { |
|||
.body <- dimnames(attr(terms(.f), "factors"))[[1]] |
|||
.f <- function(.x, .y, . = .x) {} |
|||
body(.f) <- as.expression(parse(text=.body)) |
|||
} |
|||
|
|||
if (inherits(.f, "function")) { |
|||
mapply(.f, .x, .y, ..., SIMPLIFY=FALSE, USE.NAMES=FALSE) |
|||
} |
|||
|
|||
invisible(.x) |
|||
|
|||
} |
@ -0,0 +1,12 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/utils-pipe.R |
|||
\name{\%>\%} |
|||
\alias{\%>\%} |
|||
\title{Pipe operator} |
|||
\usage{ |
|||
lhs \%>\% rhs |
|||
} |
|||
\description{ |
|||
See \code{magrittr::\link[magrittr]{\%>\%}} for details. |
|||
} |
|||
\keyword{internal} |
@ -0,0 +1,21 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/use-detect.R |
|||
\name{use_detect} |
|||
\alias{use_detect} |
|||
\title{Use base-ified equivalents of "detect"-ors} |
|||
\usage{ |
|||
use_detect(save_as = "R/utils-detect.R", open = TRUE) |
|||
} |
|||
\arguments{ |
|||
\item{save_as}{Where to save/what to name the file. Defaults to "\code{R/utils-detect.R}"} |
|||
|
|||
\item{open}{if \code{TRUE}, open the resultant file} |
|||
} |
|||
\description{ |
|||
\itemize{ |
|||
\item Creates \code{R/utils-detect.R} with base R pseudo-equivalents for \code{purrr} \code{detect} (et al). |
|||
} |
|||
} |
|||
\details{ |
|||
These support formula functions (i.e. \code{~{}}). |
|||
} |
@ -0,0 +1,18 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/use-isers.R |
|||
\name{use_isers} |
|||
\alias{use_isers} |
|||
\title{Use "is_" functions} |
|||
\usage{ |
|||
use_isers(save_as = "R/utils-isers.R", open = TRUE) |
|||
} |
|||
\arguments{ |
|||
\item{save_as}{Where to save/what to name the file. Defaults to "\code{R/utils-detect.R}"} |
|||
|
|||
\item{open}{if \code{TRUE}, open the resultant file} |
|||
} |
|||
\description{ |
|||
\itemize{ |
|||
\item Creates \code{R/utils-isers.R} filled with \code{is_...} testors |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/use-walkers.R |
|||
\name{use_walkers} |
|||
\alias{use_walkers} |
|||
\title{Use base-ified equivalents of keep/discard/compact} |
|||
\usage{ |
|||
use_walkers(save_as = "R/utils-walkers.R", open = TRUE) |
|||
} |
|||
\arguments{ |
|||
\item{save_as}{Where to save/what to name the file. Defaults to "\code{R/utils-walkers.R}"} |
|||
|
|||
\item{open}{if \code{TRUE}, open the resultant file} |
|||
} |
|||
\description{ |
|||
\itemize{ |
|||
\item Creates \code{R/utils-walkers.R} with base R pseudo-equivalents for \code{purrr} |
|||
\code{walk}/\code{walk2} |
|||
} |
|||
} |
|||
\details{ |
|||
These support formula functions (i.e. \code{~{}}). |
|||
} |
Loading…
Reference in new issue