Print the reformatted usage of a function. The arguments of the function are searched by argsAnywhere(), so the function can be either exported or non-exported from a package. S3 methods will be marked.

usage(
  FUN,
  width = getOption("width"),
  tidy = TRUE,
  output = TRUE,
  indent.by.FUN = FALSE,
  fail = c("warn", "stop", "none")
)

Arguments

FUN

The function name.

width

The width of the output.

tidy

Whether to reformat the usage code.

output

Whether to print the output to the console (via cat()).

indent.by.FUN

Whether to indent subsequent lines by the width of the function name (see “Details”).

fail

A character string that represents the action taken when the width constraint is unfulfillable. "warn" and "stop" will signal warnings and errors, while "none" will do nothing.

Value

Reformatted usage code of a function, in character strings (invisible).

Details

Line breaks in the output occur between arguments. In particular, default values of arguments will not be split across lines.

When indent.by.FUN is FALSE, indentation is set by the option getOption("formatR.indent", 4L), the default value of the indent argument of tidy_source().

See also

Examples

library(formatR)
usage(var)
#> var(x, y = NULL, na.rm = FALSE, use)

usage(plot)
#> plot(x, y, ...)

usage(plot.default)  # default method
#> ## Default S3 method:
#> plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL, log = "", main = NULL,
#>     sub = NULL, xlab = NULL, ylab = NULL, ann = par("ann"), axes = TRUE,
#>     frame.plot = axes, panel.first = NULL, panel.last = NULL, asp = NA,
#>     xgap.axis = NA, ygap.axis = NA, ...)
usage("plot.lm")  # on the 'lm' class
#> Warning: Could not fit all lines to width 80 (with indent 4):
#> (182) "    caption = list("Residuals vs Fitted", "Q-Q Residuals", "Scale-Location", "Cook's distance", "Residuals vs Leverage", expression("Cook's dist vs Leverage* " * h[ii]/(1 - h[ii]))),"
#> (104) "    panel = if (add.smooth) function(x, y, ...) panel.smooth(x, y, iter = iter.smooth, ...) else points,"
#> ## S3 method for class 'lm'
#> plot(x, which = c(1, 2, 3, 5),
#>     caption = list("Residuals vs Fitted", "Q-Q Residuals", "Scale-Location", "Cook's distance", "Residuals vs Leverage", expression("Cook's dist vs Leverage* " * h[ii]/(1 - h[ii]))),
#>     panel = if (add.smooth) function(x, y, ...) panel.smooth(x, y, iter = iter.smooth, ...) else points,
#>     sub.caption = NULL, main = "",
#>     ask = prod(par("mfcol")) < length(which) && dev.interactive(), ...,
#>     id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75, qqline = TRUE,
#>     cook.levels = c(0.5, 1), cook.col = 8, cook.lty = 2,
#>     cook.legendChanges = list(), add.smooth = getOption("add.smooth"),
#>     iter.smooth = if (isGlm) 0 else 3, label.pos = c(4, 2), cex.caption = 1,
#>     cex.oma.main = 1.25, extend.ylim.f = 0.08)

usage(usage)
#> usage(FUN, width = getOption("width"), tidy = TRUE, output = TRUE,
#>     indent.by.FUN = FALSE, fail = c("warn", "stop", "none"))

usage(barplot.default, width = 60)  # output lines have 60 characters or less
#> ## Default S3 method:
#> barplot(height, width = 1, space = NULL, names.arg = NULL,
#>     legend.text = NULL, beside = FALSE, horiz = FALSE,
#>     density = NULL, angle = 45, col = NULL,
#>     border = par("fg"), main = NULL, sub = NULL,
#>     xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL,
#>     xpd = TRUE, log = "", axes = TRUE, axisnames = TRUE,
#>     cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
#>     inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
#>     add = FALSE, ann = !add && par("ann"),
#>     args.legend = NULL, ...)

# indent by width of 'barplot('
usage(barplot.default, width = 60, indent.by.FUN = TRUE)
#> ## Default S3 method:
#> barplot(height, width = 1, space = NULL, names.arg = NULL,
#>         legend.text = NULL, beside = FALSE, horiz = FALSE,
#>         density = NULL, angle = 45, col = NULL,
#>         border = par("fg"), main = NULL, sub = NULL,
#>         xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL,
#>         xpd = TRUE, log = "", axes = TRUE, axisnames = TRUE,
#>         cex.axis = par("cex.axis"),
#>         cex.names = par("cex.axis"), inside = TRUE,
#>         plot = TRUE, axis.lty = 0, offset = 0, add = FALSE,
#>         ann = !add && par("ann"), args.legend = NULL, ...)

if (FALSE) { # \dontrun{
# a warning is raised because the width constraint is unfulfillable
usage(barplot.default, width = 30)
} # }