square_brackets_style {roger}R Documentation

Validation of Spacing Around Square Brackets

Description

Check that spacing around square brackets is valid.

Usage

open_bracket_style(srcData)
close_bracket_style(srcData)

Arguments

srcData

a list as returned by getSourceData.

Details

Good coding practices dictate the correct spacing around square brackets: opening brackets should not be immediately followed by a space; closing brackets should not be immediately preceded by a space, unless that space is after a comma.

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 square brackets
fil <- tempfile(fileext = ".R")
cat("x <- c(1, 2, 3, 5, 7, 9)",
    "x[x > 3 & x < 7]",
    "dim(x) <- c(2, 3)",
    "x[1, ]",
    file = fil, sep = "\n")
srcData <- getSourceData(fil)
open_bracket_style(srcData)
close_bracket_style(srcData)

## Incorrect use of spacing around square brackets
fil <- tempfile(fileext = ".R")
cat("x <- c(1, 2, 3, 5, 7, 9)",
    "x[ x > 3 & x < 7 ]",
    "dim(x) <- c(2, 3)",
    "x[1,]",
    "x[1,  ]",
    file = fil, sep = "\n")
srcData <- getSourceData(fil)
open_bracket_style(srcData)
close_bracket_style(srcData)

[Package roger version 1.5-1 Index]