user3477071
user3477071

Reputation: 304

Check syntax of R script without running it

After making changes to a R script, is there a way to check its syntax by running a command, before running the R script itself?

Upvotes: 3

Views: 1319

Answers (3)

Rudy Matela
Rudy Matela

Reputation: 6480

From within an R console/REPL session, you can use the builtin base's parse function:

$ R

> parse("hello.r")
expression(cat("Hello, World!\n"))

> parse("broken.r")
Error in parse("broken.r") : broken.r:2:0: unexpected end of input
1: cat("Hello, World!\n"
   ^

The parse function throws an error in case it fails to parse. A limitation here is that it will only detect parse errors, but not for example references to undefined functions.

Directly from the command line

You can also check parsing directly from the command line by using Rscript's -e option to call parse:

$ Rscript -e 'parse("hello.r")'
expression(cat("Hello, World!\n"))

$ Rscript -e 'parse("broken.r")'
Error in parse("broken.r") : broken.r:2:0: unexpected end of input
1: cat("Hello, World!\n"
   ^
Execution halted

A nice result of this is that Rscript returns successfully (no exit code) when the parse works and it returns an error code when the parse fails. So you can use your shell's || and && operators as usual or other forms of error detection (set -e).

Custom parsing script

You can also create a custom script that wraps everything nicely:

#!/bin/bash
#
# Rparse: checks whether R scripts parse successfully
#
# usage: Rscript script.r
# usage: Rscript file1.r file2.r file3.r ...
set -e
for file in "$@"
do
    Rscript -e "parse(\"$file\")"
done

Place the script in a folder pointed by your $PATH variable and use it to check your R files as follows:

$ Rparse hello.r broken.r 
expression(cat("Hello, World!\n"))
Error in parse("broken.r") : broken.r:2:0: unexpected end of input
1: cat("Hello, World!\n"
   ^
Execution halted

Here I am using bash as a reference language but there's nothing impeding alternatives to be built for other shells including Windows' ".bat" files or even an R script!

(Other answers already address the question quite nicely, but I wanted to document some additional possibilities in a more complete answer.)

Upvotes: 1

G. Grothendieck
G. Grothendieck

Reputation: 270020

Base R has parse which will parse a script without running it.

parse("myscript.R")

The codetools package, which comes with R, has checkUsage and checkUsagePackage to check single functions and packages respectively.

Upvotes: 6

JBGruber
JBGruber

Reputation: 12440

There is lint() from the lint package:

lintr::lint("tets.r")
#> tets.r:1:6: style: Place a space before left parenthesis, except in a function call.
#> is.na(((NA))
#>      ^
#> tets.r:1:12: error: unexpected end of input
#> is.na(((NA))
#>            ^

The tested file contains only this wrong code

is.na(((NA))

You can customise what lint() checks. By default, it is quite noisy about code style (which is the main reason I use it).

Upvotes: 3

Related Questions