ops_spaces_style {roger}R Documentation

Validation of Spacing Around Operators

Description

Check that spacing around infix and unary operators is valid.

Usage

ops_spaces_style(srcData, ops = ops_list)

Arguments

srcData

a list as returned by getSourceData.

ops

vector of R parser tokens corresponding to infix operators to check for correct spacing; see details.

Details

Good coding practices dictate to surround infix operators with spaces (including line breaks) and to follow unary operators immediately by their argument.

The default value for argument ops is an internal object containing the following R parser tokens: '+', '-', '*', GT, GE, LT, LE, EQ, NE, AND, OR, AND2, OR2, '!', LEFT_ASSIGN, RIGHT_ASSIGN, EQ_ASSIGN, EQ_SUB, IN, SPECIAL.

Value

Boolean. When FALSE, a message indicates the nature of the error and the faulty lines, and the returned value has the following attributes:

lines

faulty line numbers;

message

text of the error message.

Examples

## Keep parse data in non interactive sessions.
if (!interactive())
    op <- options(keep.source = TRUE)

## Correct use of spacing around '+' and '-' operators
fil <- tempfile(fileext = ".R")
cat("2 + 3",
    "-2",
    "(4\n + 2)",
    "4 +\n2",
    file = fil, sep = "\n")
ops_spaces_style(getSourceData(fil), c("'+'", "'-'"))

## Incorrect use of spacing around '>' and '!' operators
fil <- tempfile(fileext = ".R")
cat("2> 3",
    "4 >2",
    "6>3",
    "! FALSE",
    "!\nFALSE",
    file = fil, sep = "\n")
ops_spaces_style(getSourceData(fil), c("GT", "'!'"))

[Package roger version 1.5-1 Index]