Lintr - is there a way to suppress capitalised data frame column name warnings?

642 views Asked by At

I've been writing an R package and using lintr to tidy it up stylistically.

One problem I am seeing a lot is that my data.frame columns are named from the CSV and are capitalised, e.g. MyVariableName. This is outside my control and outputted data will need to follow the same style. Therefore I don't want to rename them on import as it will lead to confusion when following the code from inputted data.

I'm using the tidyverse and NSE. I also tend to use lots of quasi quotation stuff in the code (where I'm building up analysis from quoted building blocks, ie. defining lists containing:

rlang::quo(MyFirstVar + MySecondVar) 

I've tried using .data$ to scope them but still get the warnings for that:

rlang::quo(.data$MyFirstVar + .data$MySecondVar) 

I've found for dplyr select commands you can quote the column names as strings - so that solves some of the warnings.

Is there a way to suppress warnings about data frame column names?

2

There are 2 answers

0
Andrew Hill On BEST ANSWER

Hmm - by accident I appear to have solved my own answer.

Instead of using:

rlang::quo(.data$MyFirstVar + .data$MySecondVar)

You can use:

rlang::quo(.data[["MyFirstVar"]] + .data[["MySecondVar"]])

(and of course doing that you can also replace the string with a variable if your column name is unknown...)

4
Thomas K On

lintr is a package to help you write with consistent style. If you have valid reasons for why you need to use camelCase instead of snake_case, I wouldn't try to circumvent that. I would definitely not try to remove warnings by changing your code. This will make it less readable, and the goal of a style guide is to have more readable and consistent code!

There are however other possibilities to suppress warnings when linting. If you look at the Readme.md at GitHub, there are at least two possibilities:

  • You can append each offending line with # nolint or whole blocks of code with # nolint start and # nolint end.
  • You can create a configuration file (.lint) in your project root and change the default linters.

Depending on how you call lintr when checking your code, there might be other options as well.