MyNixOS website logo
Description

Print Function Signatures.

Print function signatures and find overly complicated code.

Project Status: Inactive - The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows. Is the package on CRAN? SemaphoreCI Build Status AppVeyor Build Status

sig

Print, compare and report on function signatures. (In this case, signature means the name and arguments, with possible defaults, of that function.)

Installation

To install the stable version, type:

install.packages("sig")

To install the development version, you first need the remotes package.

install.packages("remotes")

Then you can install the sig package using

install_bitbucket(
  "richierocks/sig"
)

Usage

If you want to know how to call a function, just pass a function to the sig function. For example,

sig(mean)
# mean <- function(x, ...)
sig(mean.default)
# mean.default <- function(x, trim = 0, na.rm = FALSE, ...)

If you pass an anonymous function, it is given the name ..anonymous...

sig(function(x, y) {x + y})
# ..anonymous.. <- function(x, y)

You can override the name of the function by passing a second argument. This is useful when using sig with an *apply function.

fn_list <- list(
  mean = mean, 
  var = var
)
lapply(fn_list, sig)         # names are a mess
mapply(                      # use mapply for lists
  sig, 
  fn_list, 
  names(fn_list), 
  SIMPLIFY = FALSE
)

"Black Box" is a useful game for testing how maintainable your code is. You give your friend or colleague the signatures of your functions and have them guess what the function contents. For example, if you show them

mean <- function(x, ...)

then they might be able to guess that the function calculates the arithmetic mean of a numeric input.

If you didn't know what it was, the signature for the lm function doesn't make it as clear what the function does

sig(lm)
# lm <- function(formula, data, subset, weights, na.action, method =
#   "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok =
#   TRUE, contrasts = NULL, offset, ...)

Your friend might guess that since the function takes a formula and a data arguement that it is some kind of model. Some of the other arguments may be guessable. "Oh, weights must let you run a weighted model!" Beyond that, the function's purpose is difficult to determine without consulting documentation.

In general, if you can guess what a function does, and what its body should contain, based only on the signature, then that function will be easy to use and easy to maintain. By contrast, an unclear function signature provides a warning that it may be difficult to use or maintain.

To make Black Box easy to play, use the write_sigs function to write all the functions from a file or R package to a text file.

# From an environment
write_sigs(
  pkg2env(graphics), 
  "graphics pkg sigs.R"
)

# From a file
write_sigs(
  "my R file.R",
  "my sigs.R
)
Metadata

Version

0.0-6

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows