1

I am trying to help our group of developers obtain similar code styles by using the lintr package when writing R code. To automate this step, I want our builds to fail if there are any mistakes (bad styles) in the code. We are using Jenkins for our build pipeline if that matters.

I know that we could use the expect_lint_free function, but we are not making packages, only script files. The output from the lint function looks fine, but the build passes even when the linter returns improvement proposals. How do I get a non-zero exit status that will make the Jenkins build fail?

The (simplified) code run on the command line

Rscript -e "lintr::lint('my_script.R')"

returns no error but lots of proposals.

Note: I am not interested in "Create an R package" solutions at the moment.

Thomas
  • 214
  • 1
  • 11
  • Why don't you write a wrapper function around `lint` that catches those improvement proposals and returns non-zero in that case? – Ramiro Magno Jan 30 '19 at 11:30

2 Answers2

6

The lintr::lint() function returns results in a list with class "lints". You have issues if its length is greater than zero, so you can do

Rscript -e "quit(save = 'no', status = length(lintr::lint('my_script.R')))"
user2554330
  • 37,248
  • 4
  • 43
  • 90
4

While the solution by @user2554330 works and exits with a non-zero exit code, it suppresses the output from lintr. However, you'd generally like to see where it fails when it does. You can achieve this like so:

Rscript -e "errors <- lintr::lint('my_script.R'); print(errors); quit(save = 'no', status = length(errors))"

Or for all .r files:

Rscript -e "errors <- lintr::lint_dir(); print(errors); quit(save = 'no', status = length(errors))"