Skip to content

Commit

Permalink
imrprove consistency
Browse files Browse the repository at this point in the history
- `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.
  • Loading branch information
atusy committed May 24, 2019
1 parent 96d2429 commit 8b4a45f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 82 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ export("?")
export(felp)
importFrom(utils,"?")
importFrom(utils,help)
importFrom(utils,savehistory)
importFrom(utils,tail)
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
31 changes: 11 additions & 20 deletions R/felp.R
Original file line number Diff line number Diff line change
@@ -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], ...))

}
26 changes: 0 additions & 26 deletions R/print.R

This file was deleted.

30 changes: 16 additions & 14 deletions R/question.R
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions man/felp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions man/print.function.Rd

This file was deleted.

5 changes: 5 additions & 0 deletions man/question.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b4a45f

Please sign in to comment.