Skip to content

Instantly share code, notes, and snippets.

@klmr
Created October 20, 2023 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klmr/86752f3bee5ee35048eb90b6c7b2bb7c to your computer and use it in GitHub Desktop.
Save klmr/86752f3bee5ee35048eb90b6c7b2bb7c to your computer and use it in GitHub Desktop.
Alternative R linters
linters:
config_dir = dirname(parent.frame(3L)$config_file)
lapply(dir(config_dir, '\\.r$', full.names = TRUE), source, local = environment())
lintr::linters_with_defaults(
assignment_linter = NULL,
function_left_parentheses_linter = NULL,
line_length_linter(120L),
object_usage_linter = NULL, # unusably buggy
lintr::quotes_linter("'")
)
line_length_linter = function (length = 120L) {
general_msg = paste('Lines should not be more than', length, 'characters.')
lintr::Linter(\(source_expression) {
if (! lintr::is_lint_level(source_expression, 'file')) {
return(list())
}
# Note that this will handle “comment-looking” lines in multi-line strings incorrectly. But that’s fine.
comment_lines = grep('^\\s*#', source_expression$file_lines)
line_lengths = nchar(source_expression$file_lines)
long_lines = setdiff(which(line_lengths > length), comment_lines)
Map(
\(long_line, line_length) {
lintr::Lint(
filename = source_expression$filename,
line_number = long_line,
column_number = length + 1L,
type = 'style',
message = paste(general_msg, 'This line is', line_length, 'characters.'),
line = source_expression$file_lines[long_line],
ranges = list(c(1L, line_length))
)
},
long_lines,
line_lengths[long_lines]
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment