From 8b4a45f5360e71dd6abf2a9bc75c573b3a49d978 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Fri, 24 May 2019 18:03:19 +0900 Subject: [PATCH] imrprove consistency - `felp()` has more consistent arguments to `utils::help` by changing a name of first argument to "topic" from "x". - Pseudo-postfix operator `?.` supports arguments other than fucntions. --- NAMESPACE | 2 -- NEWS.md | 12 ++++++++++++ R/felp.R | 31 +++++++++++-------------------- R/print.R | 26 -------------------------- R/question.R | 30 ++++++++++++++++-------------- man/felp.Rd | 8 ++++---- man/print.function.Rd | 16 ---------------- man/question.Rd | 5 +++++ 8 files changed, 48 insertions(+), 82 deletions(-) delete mode 100644 R/print.R delete mode 100644 man/print.function.Rd diff --git a/NAMESPACE b/NAMESPACE index c30a04b..d417142 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,5 +6,3 @@ export("?") export(felp) importFrom(utils,"?") importFrom(utils,help) -importFrom(utils,savehistory) -importFrom(utils,tail) diff --git a/NEWS.md b/NEWS.md index ad9a0e6..5cd1c03 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,15 @@ +# felp 0.1.3.9000 + +# Major changes + +- `felp()` has more consistent arguments to `utils::help` by changing + a name of first argument to "topic" from "x". +- Pseudo-postfix operator `?.` supports arguments other than fucntions. + +# Minor changes + +- `felp()` is simplified. + # felp 0.1.3 - Officially support `function?.` form to print function and its help simultaneously. diff --git a/R/felp.R b/R/felp.R index a418655..5bb00aa 100644 --- a/R/felp.R +++ b/R/felp.R @@ -1,33 +1,24 @@ #' Returns source and help of a function simultaneously #' -#' @param x a name or character string specifying the function for which help and source are sought. Package to be sought can also be defined simultaneously if x is specified in the style of package::name or package:::name -#' @param package a name or character string specifying the package for which help and source are sought. If the package is specified by x, this parameter is neglected, or NULL (default). -#' @param ... other arguments passed to help +#' @param topic A name or character string specifying the function for which help and source are sought. Package to be sought can also be defined simultaneously if x is specified in the style of package::name or package:::name +#' @param package A name or character string specifying the package for which help and source are sought. If the package is specified by x, this parameter is neglected, or NULL (default). +#' @param ... Other arguments passed to help #' @importFrom utils help #' @export #' -felp <- function (x, package = NULL, ...) { - # convert package::name to list("name", "package", "`::`) - # if x = name or "name", input = list("name") - input <- if (is.character(x)) { - list(x) - } else { - rev(lapply(substitute(x), deparse)) - } - - # Package to look for help of the function - if (is.name(p <- substitute(package))) { - package <- as.character(p) - } - package <- c(package, input[2][[1]]) +felp <- function(topic, package = NULL, ...) { + # convert package::name to c("name", "package", "`::`") + # or name to "name" + t <- rev(as.character(substitute(topic))) + p <- c(as.character(substitute(package)), t[2L])[1L] + if (is.na(p)) p <- NULL # Print source of the function prettycode:::print.function(get( - input[[1]], - envir = `if`(is.null(package), parent.frame(), asNamespace(package)) + t[1L], envir = `if`(is.null(p), parent.frame(), asNamespace(p)) )) - try(help(input[[1]], package = package[1], ...)) + try(help(t[1L], p[1L], ...)) } diff --git a/R/print.R b/R/print.R deleted file mode 100644 index 8d7d0b2..0000000 --- a/R/print.R +++ /dev/null @@ -1,26 +0,0 @@ -#' Display source and help of function -#' @param x function -#' @param ... Other arguments passed to felp. felp is called when print.function is explicitly called or via print. -#' @importFrom utils savehistory tail ? -#' -print.function <- function(x, ...) { - tempfile <- tempfile(pattern="rhistory_felp_", fileext=".txt") - if(base::.Platform$OS.type == 'windows' && base::.Platform$GUI == 'RStudio') - savehistory <- get('savehistory', envir = as.environment('tools:rstudio')) - savehistory(file = tempfile) - cmd <- as.character(parse(text = tail(readLines(tempfile), 1))) - - # return result of felp when print.function is explicitly called or via print. - if(grepl('print(.function)?\\(', cmd)) { - input <- rev(lapply(substitute(x), deparse)) - if(!is.function(x)) stop("x must be a function") - return(felp(x = input[[1]], package = input[2][[1]])) - } - - #print help - try(print(eval(parse(text = paste0('?', cmd))))) - - #print source - base::print.function(eval(parse(text = cmd))) -} - diff --git a/R/question.R b/R/question.R index 376eb19..e48d0b3 100644 --- a/R/question.R +++ b/R/question.R @@ -1,27 +1,29 @@ #' S3 version of [utils::`?`] #' -#' @inheritParams ? +#' @param e1 +#' A topic ought to be documented. Refer to `topic` argument described in `` utils::`?` ``. +#' @param e2 +#' `.` equals to `missing(e2)`. If else, see `type` argument described in `` utils::`?` ``. #' @rdname question #' @aliases ? #' @export -`?` <- function() UseMethod("?") +`?` <- function(e1, e2) UseMethod("?") + #' @rdname question #' @aliases ? -#' @inheritParams ? #' @export -`?.function` <- function() { - if(!missing(e2) && as.character(substitute(e2)) != ".") { - NextMethod() - } - .felp <- felp - formals(.felp)$x <- substitute(e1) - .felp() +`?.function` <- function(e1, e2) { + if (!missing(e2) && as.character(substitute(e2)) != ".") NextMethod() + do.call(felp, list(topic = substitute(e1))) } + #' @rdname question #' @aliases ? -#' @inheritParams ? #' @importFrom utils ? #' @export -`?.default` <- utils::"?" - -formals(`?`) <- formals(`?.function`) <- formals(`?.default`) +`?.default` <- function(e1, e2) { + .arg <- list(e1 = substitute(e1)) + .e2 <- substitute(e2) + if (!missing(.e2) && as.character(.e2) != ".") .arg$e2 <- .e2 + do.call(utils::"?", .arg) +} diff --git a/man/felp.Rd b/man/felp.Rd index abec612..886d509 100644 --- a/man/felp.Rd +++ b/man/felp.Rd @@ -4,14 +4,14 @@ \alias{felp} \title{Returns source and help of a function simultaneously} \usage{ -felp(x, package = NULL, ...) +felp(topic, package = NULL, ...) } \arguments{ -\item{x}{a name or character string specifying the function for which help and source are sought. Package to be sought can also be defined simultaneously if x is specified in the style of package::name or package:::name} +\item{topic}{A name or character string specifying the function for which help and source are sought. Package to be sought can also be defined simultaneously if x is specified in the style of package::name or package:::name} -\item{package}{a name or character string specifying the package for which help and source are sought. If the package is specified by x, this parameter is neglected, or NULL (default).} +\item{package}{A name or character string specifying the package for which help and source are sought. If the package is specified by x, this parameter is neglected, or NULL (default).} -\item{...}{other arguments passed to help} +\item{...}{Other arguments passed to help} } \description{ Returns source and help of a function simultaneously diff --git a/man/print.function.Rd b/man/print.function.Rd deleted file mode 100644 index 5841e83..0000000 --- a/man/print.function.Rd +++ /dev/null @@ -1,16 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/print.R -\name{print.function} -\alias{print.function} -\title{Display source and help of function} -\usage{ -\method{print}{function}(x, ...) -} -\arguments{ -\item{x}{function} - -\item{...}{Other arguments passed to felp. felp is called when print.function is explicitly called or via print.} -} -\description{ -Display source and help of function -} diff --git a/man/question.Rd b/man/question.Rd index de3476c..be35845 100644 --- a/man/question.Rd +++ b/man/question.Rd @@ -12,6 +12,11 @@ \method{?}{default}(e1, e2) } +\arguments{ +\item{e1}{A topic ought to be documented. Refer to `topic` argument described in `` utils::`?` ``.} + +\item{e2}{`.` equals to `missing(e2)`. If else, see `type` argument described in `` utils::`?` ``.} +} \description{ S3 version of [utils::`?`] }