parentheses_style {roger}R Documentation

Validation of Spacing Around Parentheses

Description

Check that spacing around parentheses is valid.

Usage

open_parenthesis_style(srcData)
close_parenthesis_style(srcData)
left_parenthesis_style(srcData)

Arguments

srcData

a list as returned by getSourceData.

Details

Good coding practices dictate the correct spacing around parentheses. First, opening parentheses should not be immediately followed by a space. Second, closing parentheses should not be immediately preceded by a space. Third, left (or opening) parentheses should always be preceded by a space, except: in function calls; at the start of sub-expressions; after the unary operators !, + and -; after the operators [, [[, ^, :, and /, or after an optional left parenthesis.

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 parentheses
fil <- tempfile(fileext = ".R")
cat("x <- c(2, 3, 5)",
    "if (any((2 * x) > 4))",
    "    sum(x)",
    "!x",
    "-x",
    "1/(x + 1)",
    "2^(x - 1)",
    "2^((x - 1))",
    file = fil, sep = "\n")
srcData <- getSourceData(fil)
open_parenthesis_style(srcData)
close_parenthesis_style(srcData)
left_parenthesis_style(srcData)

## Incorrect use of spacing around parentheses
fil <- tempfile(fileext = ".R")
cat("x <- c(2, 3, 5 )",
    "if(any(x > 4))",
    "    sum( x )",
    file = fil, sep = "\n")
srcData <- getSourceData(fil)
open_parenthesis_style(srcData)
close_parenthesis_style(srcData)
left_parenthesis_style(srcData)

[Package roger version 1.5-1 Index]