Title: | Print Function Signatures |
---|---|
Description: | Print function signatures and find overly complicated code. |
Authors: | Richard Cotton [aut, cre] |
Maintainer: | Richard Cotton <[email protected]> |
License: | Unlimited |
Version: | 0.0-6 |
Built: | 2024-11-06 02:55:06 UTC |
Source: | https://bitbucket.org/richierocks/sig |
Get or set a subset of a siglist.
## S3 method for class 'siglist' x[i, ...] ## S3 method for class 'siglist' x[[i, ...]] ## S3 replacement method for class 'siglist' x[...] <- value ## S3 replacement method for class 'siglist' x[[...]] <- value
## S3 method for class 'siglist' x[i, ...] ## S3 method for class 'siglist' x[[i, ...]] ## S3 replacement method for class 'siglist' x[...] <- value ## S3 replacement method for class 'siglist' x[[...]] <- value
x |
A |
i |
An integer vector index. |
... |
Passed from other index methods. |
value |
A value to set the subset to. |
A siglist.
methods_sigs <- list_sigs(pkg2env(methods)) methods_sigs[1:5] methods_sigs[[1]]
methods_sigs <- list_sigs(pkg2env(methods)) methods_sigs[1:5] methods_sigs[[1]]
Strips class attributes to return a list.
## S3 method for class 'sig' as.list(x, ...) ## S3 method for class 'siglist' as.list(x, ...) ## S3 method for class 'sigreport' as.list(x, ...)
## S3 method for class 'sig' as.list(x, ...) ## S3 method for class 'siglist' as.list(x, ...) ## S3 method for class 'sigreport' as.list(x, ...)
x |
|
... |
Passed from other |
A list.
as.list(sig(read.csv)) head(as.list(list_sigs(pkg2env(stats)))) as.list(sig_report(baseenv()))
as.list(sig(read.csv)) head(as.list(list_sigs(pkg2env(stats)))) as.list(sig_report(baseenv()))
Coerces an object to be a sig
.
as.sig(x, ...) ## Default S3 method: as.sig(x, ...) ## S3 method for class 'siglist' as.sig(x, ...) ## S3 method for class 'list' as.sig(x, ...) ## S3 method for class 'sig' as.sig(x, ...)
as.sig(x, ...) ## Default S3 method: as.sig(x, ...) ## S3 method for class 'siglist' as.sig(x, ...) ## S3 method for class 'list' as.sig(x, ...) ## S3 method for class 'sig' as.sig(x, ...)
x |
Object to coerce. |
... |
Passed to other |
An object of class sig
.
as.sig( list(name = "fun", alist(x =,y = 1)) )
as.sig( list(name = "fun", alist(x =,y = 1)) )
Coerces an object to be a siglist
.
as.siglist(x, ...) ## S3 method for class 'sig' as.siglist(x, ...) ## S3 method for class 'list' as.siglist(x, ...) ## S3 method for class 'siglist' as.siglist(x, ...)
as.siglist(x, ...) ## S3 method for class 'sig' as.siglist(x, ...) ## S3 method for class 'list' as.siglist(x, ...) ## S3 method for class 'siglist' as.siglist(x, ...)
x |
Object to coerce. |
... |
Passed to other |
An object of class siglist
.
as.siglist(list( sig(mean), list(name = "fun", alist(x =,y = 1)) ))
as.siglist(list( sig(mean), list(name = "fun", alist(x =,y = 1)) ))
Wraps strings in backquotes.
backquote(x)
backquote(x)
x |
A character vector. |
A character vector.
Existing backquote characters are escaped with a backslash.
## Not run: backquote(c("foo bar", "a`b`c")) ## End(Not run)
## Not run: backquote(c("foo bar", "a`b`c")) ## End(Not run)
Wrapper to cut
for positive integers.
exponential_cut(x)
exponential_cut(x)
x |
A vector of positive integers. |
A factor.
The breaks are 1, 2, 3 to 4, 5 to 8, etc. No input checking is done; use at your peril.
## Not run: exponential_cut(c(1:10, 500)) ## End(Not run)
## Not run: exponential_cut(c(1:10, 500)) ## End(Not run)
Make anonymous functions and special functions safe.
fix_fn_names(fn_name)
fix_fn_names(fn_name)
fn_name |
A character vector. |
A character vector.
Strings beginning with “function” are given the value
"..anonymous.."
.
Special function names are wrapped in backquotes.
## Not run: fix_fn_names(c("%foo%", "?", "foo bar", "repeat", "function")) ## End(Not run)
## Not run: fix_fn_names(c("%foo%", "?", "foo bar", "repeat", "function")) ## End(Not run)
Does the input inherit from “sig”?
is.sig(x)
is.sig(x)
x |
Object to test. |
TRUE
if the object inherits from class “sig”,
and FALSE otherwise.
stopifnot( is.sig(sig(with)), !is.sig(with) #functions are not their signatures. )
stopifnot( is.sig(sig(with)), !is.sig(with) #functions are not their signatures. )
Does the input inherit from “siglist”?
is.siglist(x)
is.siglist(x)
x |
Object to test. |
TRUE
if the object inherits from class “siglist”
and is.sig
returns TRUE
for each element of the input,
and FALSE otherwise.
stopifnot( !is.siglist(sig(with)) #1 sig is not a siglist. )
stopifnot( !is.siglist(sig(with)) #1 sig is not a siglist. )
Lists the signatures of all functions in an environment or file.
list_sigs(x, pattern = NULL, ...) ## Default S3 method: list_sigs(x, ...) ## S3 method for class 'environment' list_sigs(x, pattern = NULL, ...) ## S3 method for class 'character' list_sigs(x, ...)
list_sigs(x, pattern = NULL, ...) ## Default S3 method: list_sigs(x, ...) ## S3 method for class 'environment' list_sigs(x, pattern = NULL, ...) ## S3 method for class 'character' list_sigs(x, ...)
x |
An environment or the the path to a file. |
pattern |
An optional regular expression. Only names matching pattern are returned. |
... |
Currently ignored |
An object of class siglist
, which is a list
of sig
obejcts.
#From a package list_sigs(pkg2env(graphics)) #Just functions beginning with 'a'. list_sigs(pkg2env(graphics), pattern = "^a") #From a file list_sigs(system.file("extdata", "sample.R", package = "sig"))
#From a package list_sigs(pkg2env(graphics)) #Just functions beginning with 'a'. list_sigs(pkg2env(graphics), pattern = "^a") #From a file list_sigs(system.file("extdata", "sample.R", package = "sig"))
Utility function to get the environment of a package on the search path.
pkg2env(pkg) pkg2env_(pkg)
pkg2env(pkg) pkg2env_(pkg)
pkg |
A package. |
the environment corresponding to pkg
.
# Non-standard evaluation version pkg2env(graphics) # Standard evaluations versions pkg2env_("tools") pkg2env_(~ utils) pkg2env_(quote(stats))
# Non-standard evaluation version pkg2env(graphics) # Standard evaluations versions pkg2env_("tools") pkg2env_(~ utils) pkg2env_(quote(stats))
Wraps toString methods with cat.
print_engine(x, ...)
print_engine(x, ...)
x |
Object to print |
... |
Passed to |
The input is invisibly returned, but the function is mostly invoked for the side effect of printing the object.
Not intended for general consumption. This function is only exported because of package build requirements.
Generates a signature object for a function.
sig(fn, name_override)
sig(fn, name_override)
fn |
A function. |
name_override |
Override the default function name. See examples. |
A list, with the elements
nameThe name of the function.
argsThe arguments of the function.
Anonymous functions are given the name "..anonymous..".
Nonstandard names ("foo bar"), assignment fns ("foo<-"), operators (" in backquotes.
sig(R.Version) #no args sig(scan) #lots of args sig(function(x, y) {x + y}) #anonymous sig(sum) #primitive fn_list <- list( mean = mean, var = var ) lapply(fn_list, sig) #names are a mess Map( #use Map for lists sig, fn_list, names(fn_list) #Map mangles names, so override )
sig(R.Version) #no args sig(scan) #lots of args sig(function(x, y) {x + y}) #anonymous sig(sum) #primitive fn_list <- list( mean = mean, var = var ) lapply(fn_list, sig) #names are a mess Map( #use Map for lists sig, fn_list, names(fn_list) #Map mangles names, so override )
Summarise function complexity of a file or environment
sig_report(x, ...) ## Default S3 method: sig_report(x, ...) ## S3 method for class 'environment' sig_report(x, too_many_args = 10, too_many_lines = 50, length_metric = c("deparse", "body"), ...) ## S3 method for class 'character' sig_report(x, ...) ## S3 method for class 'sigreport' print(x, ...)
sig_report(x, ...) ## Default S3 method: sig_report(x, ...) ## S3 method for class 'environment' sig_report(x, too_many_args = 10, too_many_lines = 50, length_metric = c("deparse", "body"), ...) ## S3 method for class 'character' sig_report(x, ...) ## S3 method for class 'sigreport' print(x, ...)
x |
A path to an R file or an environment. |
... |
Passed to |
too_many_args |
Upper bound for a sensible number of args. |
too_many_lines |
Upper bound for a sensible number of lines. |
length_metric |
Either |
sig_report
summarises the number of input arguments and the
number of lines of each function in an environment of file, and
identifies problem files, in order to help you refactor your code.
If the input is a path to an R file, then that file is sourced into
a new environment and and the report is generated from that.
The number of lines of code that a function takes up is subjective
in R; this function gives you a choice of length(deparse(fn))
or
length(body(fn))
, depending upon the value of length_metric
.
The body
metric tends to give smaller values than deparse
, so
you may want to reduce the too_many_lines
argument.
An object of class “sigreport” with the elements:
n_varsNumber of variables.
n_fnsNumber of functions.
n_argsTable of the number of args of each function.
too_many_argsUpper bound for a sensible number of args.
fns_with_many_argsNames of each function with more args
than too_many_args
.
n_linesTable of the number of lines of each function body.
too_many_linesUpper bound for a sensible number of lines.
long_fnsNames of each function with more lines than
too_many_lines
.
#Summarise function complexity in an environment sig_report(pkg2env(stats)) #Summarise function complexity in a file # From a file tmp <- tempfile(fileext = ".R") writeLines(c(toString(sig(scan)), deparse(body(scan))), tmp) sig_report(tmp) # From an environment, adjusting the cutoff for reporting sig_report( baseenv(), too_many_args = 20, too_many_lines = 100 ) # Alternate length metric sig_report(baseenv(), length_metric = "body")
#Summarise function complexity in an environment sig_report(pkg2env(stats)) #Summarise function complexity in a file # From a file tmp <- tempfile(fileext = ".R") writeLines(c(toString(sig(scan)), deparse(body(scan))), tmp) sig_report(tmp) # From an environment, adjusting the cutoff for reporting sig_report( baseenv(), too_many_args = 20, too_many_lines = 100 ) # Alternate length metric sig_report(baseenv(), length_metric = "body")
Silently sources a file into a new environment, returning that environment.
source_to_new_env(file, encoding = getOption("encoding"))
source_to_new_env(file, encoding = getOption("encoding"))
file |
a file to source. |
encoding |
character encoding of that file. |
An environment containing the sourced variables.
Prints a function signature object.
## S3 method for class 'sig' toString(x, width = getOption("width"), exdent = nchar(x$name), ...) ## S3 method for class 'sig' print(x, width = getOption("width"), exdent = nchar(x$name), ...)
## S3 method for class 'sig' toString(x, width = getOption("width"), exdent = nchar(x$name), ...) ## S3 method for class 'sig' print(x, width = getOption("width"), exdent = nchar(x$name), ...)
x |
An object of class |
width |
Width of string to display. |
exdent |
Non-negative integer specifying the indentation of subsequent lines in the string. |
... |
Passed to |
toString
creates a string representation of a
function signature.
print
is mostly invoked for the side effect of printing
a function
signature, invisibly returning its input.
print_default_sig <- sig(print.default) print(print_default_sig) print(print_default_sig, width = 40) print(print_default_sig, width = 40, exdent = 2) toString(print_default_sig)
print_default_sig <- sig(print.default) print(print_default_sig) print(print_default_sig, width = 40) print(print_default_sig, width = 40, exdent = 2) toString(print_default_sig)
Prints a list of function signature objects.
## S3 method for class 'siglist' toString(x, width = getOption("width"), ...) ## S3 method for class 'siglist' print(x, width = getOption("width"), ...)
## S3 method for class 'siglist' toString(x, width = getOption("width"), ...) ## S3 method for class 'siglist' print(x, width = getOption("width"), ...)
x |
An object of class |
width |
Width of string to display. |
... |
Passed to the equivalent |
toString
creates a string representation of a function signature.
print
is mostly invoked for the side effect of printing a function
signature, invisibly returning its input.
method_sigs <- list_sigs(pkg2env(methods)) print(method_sigs) print(method_sigs, width = 40) print(method_sigs, width = 40, exdent = 2) toString(method_sigs)
method_sigs <- list_sigs(pkg2env(methods)) print(method_sigs) print(method_sigs, width = 40) print(method_sigs, width = 40, exdent = 2) toString(method_sigs)
Writes a list of function signatures to a file.
write_sigs(x, file = stdout(), ...) ## Default S3 method: write_sigs(x, file = stdout(), ...) ## S3 method for class 'siglist' write_sigs(x, file = stdout(), ...) ## S3 method for class 'environment' write_sigs(x, file = stdout(), ...) ## S3 method for class 'character' write_sigs(x, file = stdout(), ...)
write_sigs(x, file = stdout(), ...) ## Default S3 method: write_sigs(x, file = stdout(), ...) ## S3 method for class 'siglist' write_sigs(x, file = stdout(), ...) ## S3 method for class 'environment' write_sigs(x, file = stdout(), ...) ## S3 method for class 'character' write_sigs(x, file = stdout(), ...)
x |
A list of function signatures. See details. |
file |
A file path or connection to write the output to (stdout by default). |
... |
passed to |
Where x
is an object of class |codesiglist, the
function essentially calls writeLines(tostring(x))
.
If the input is a single function signature (of class sig
),
then it is coerced into a siglist
. If the input is an
environment or path to a file, then list_sigs
is called on
the input before writing.
A character vector of the lines that were written to file is invisibly returned. Mostly invoked for the side effect of writing function signatures to a file.
#Step by step: #First, list some sigs. utils_sigs <- list_sigs(pkg2env(utils)) #Without a file argument, sigs are just printed to the console. head(write_sigs(utils_sigs)) #Write to a file tmpf <- tempfile("sig", fileext = ".R") write_sigs(utils_sigs, tmpf) ## Not run: Open the file we've just written shell(tmpf, wait = FALSE) ## End(Not run) #Can also list and write in one line. tmpf2 <- tempfile("sig", fileext = ".R") write_sigs(pkg2env(grDevices), tmpf2) #Single sigs are coerced to siglists write_sigs(sig(stats::var))
#Step by step: #First, list some sigs. utils_sigs <- list_sigs(pkg2env(utils)) #Without a file argument, sigs are just printed to the console. head(write_sigs(utils_sigs)) #Write to a file tmpf <- tempfile("sig", fileext = ".R") write_sigs(utils_sigs, tmpf) ## Not run: Open the file we've just written shell(tmpf, wait = FALSE) ## End(Not run) #Can also list and write in one line. tmpf2 <- tempfile("sig", fileext = ".R") write_sigs(pkg2env(grDevices), tmpf2) #Single sigs are coerced to siglists write_sigs(sig(stats::var))