check_R_code: Check R (or Sweave) code files

Description Usage Arguments Details Value See Also Examples

View source: R/package.R

Description

Check certain aspects of the format of R code files in the ‘R’ subdirectory of a package, or of any other kinds of files. R code can also be extracted from Sweave files. Optionally write descriptions of problems to the logfile used by pkgutils, which can be set using logfile. Alternatively, check the labels used in the lines that start a Sweave code chunk.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  check_R_code(x, ...)

  ## S3 method for class 'character'
 check_R_code(x, lwd = 80L,
    indention = 2L, roxygen.space = 1L, comma = TRUE,
    ops = TRUE, parens = TRUE, assign = TRUE,
    modify = FALSE, ignore = NULL, accept.tabs = FALSE,
    three.dots = TRUE, what = "R", encoding = "",
    filter = c("none", "sweave", "roxygen"), ...)

  check_Sweave_start(x, ...)

  ## S3 method for class 'character'
 check_Sweave_start(x, ignore = TRUE,
    what = c("vignettes", file.path("inst", "doc")),
    encoding = "", ...)

  logfile(x)

  ## S3 method for class 'NULL'
 logfile(x)

  ## S3 method for class 'character'
 logfile(x)

Arguments

x

For check_R_code, a character vector of names of input files, or names of R package directories. The latter will be expanded as appropriate. x is passed to pkg_files with the ‘installed’ argument set to FALSE. See there for further details.

For logfile, a character scalar for setting the logfile, or NULL for getting the current value. Use an empty string to turn logging off.

lwd

Numeric scalar. Maximum line width allowed. Set this to a reasonably large number to effectively turn checking off.

indention

Numeric scalar. Number of spaces used for one unit of indention. Set this to 1 to effectively turn checking off.

roxygen.space

Numeric scalar. Number of spaces expected after roxygen2 comments (which start with ‘#’ followed by a single quote).

comma

Logical scalar indicating whether it should be checked that each comma is not preceded by a space but followed by a space.

ops

Logical scalar indicating whether it should be checked that operators are surrounded by spaces.

parens

Logical scalar indicating whether it should be checked that control-flow constructs are not directly followed by parentheses, that opening parentheses (and brackets) are not followed by a space, and that closing parentheses (and brackets) are followed by appropriate characters only and are not preceded by a space.

assign

Logical scalar indicating that it should be checked that there is no line break within named function-argument assignments.

modify

Logical scalar indicating whether the source code should be modified (non-destructively, of course) and input files overwritten (if changes were possible). The modifications currently only comprise the removal of whitespace from the ends of the lines and optionally the replacement of each tabulator by indention numbers of spaces (see also the next argument).

accept.tabs

Logical scalar indicating whether tabulators are accepted.

three.dots

Logical scalar indicating whether ::: operators should result in a warning.

what

Character vector naming the subdirectories to consider; passed to pkg_files

encoding

Character scalar passed as ‘.encoding’ argument to map_files.

ignore

Passed to pkg_files. See there for details. A logical scalar is used for selecting or discarding a default filter suitable for the target files.

filter

Character scalar indicating the filter to use. The "roxygen" filter extracts the examples from roxygen2-style comments, assuming that the ‘#’ character that start such a comment are not preceded by whitespace. (Lines in which this is the case are detected if no filtering is used.)

...

Optional other arguments passed to pkg_files.

Details

check_R_code is intended to ensure a consistent and readable R coding style. Not all problems can be checked, however. For instance, + and - are binary as well as unary operators and should either be followed by spaces or not, respectively. Yielding no problem messages is thus just a minimum requirement for a good coding style. For instance, indentation checking does not check whether continuation lines have the correct number of leading spaces. Rather, checks are made line-per-line throughout. In addition to such false negatives, check_R_code falsely complains about numbers in exponential notation.

Functions such as check_keywords print detected problems, if any, using message. These character vectors can also be saved by appending to a logfile.

Value

check_R_code yields a logical vector; see map_files for details. Here the result is returned invisibly. As a side effect, problem messages are printed to stderr. See logfile for how to send these messages to a file.

logfile returns a character scalar with the name of the current logfile.

See Also

Other package-functions: copy_pkg_files, delete_o_files, is_pkg_dir, pack_desc, pkg_files, run_R_CMD, swap_code,

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Checking the R scripts that come with the package
(scripts <- pkg_files("pkgutils", "scripts"))
if (length(scripts)) {
  result <- check_R_code(scripts) # should not yield any messages
  stopifnot(is.logical(result), names(result) == names(scripts))
} else {
  warning("scripts not found")
}

# See also the 'docu.R' script provided with this package, options
# '--blank', '--jspaces', '--width', '--assignoff', '--commaoff',
# '--opsoff', '--modify', '--good', '--parensoff', '--Rcheck', '--tabs' and
# '--untidy'. Checking can be turned off generally or specifically.

## logfile()
old <- logfile()
new <- tempfile()
logfile(new)
stopifnot(new == logfile())
logfile(old)
stopifnot(old == logfile())

pkgutils documentation built on May 2, 2019, 5:49 p.m.