How to set 4 spaces indentation instead of 2 inside the .lintr configuration file?

Hello everyone

I would like to ask a question about lintr package and precisely about its indentation customization.

If I understand correctly, using lintr package allows to check whether a code conforms to The Tidyverse style guide. I use lintr as part of ESS installation in Emacs. So far, this has always worked pretty well.

I use Emacs for almost everything (R, Python, SQL, Rust, etc.) I have a global setting in my Emacs Init file to set tabs equivalent of 4 spaces:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

However, recently I had to upgrade to a new version of Emacs and I reinstalled everything including ESS and lintr. It appears that the latest version of lintr expects 2 spaces for indentation with tabs and because of this, currently I have loads of warning messages with the latest version of lintr in my R programs:

[indentation lintr] : Indentation should be 2 spaces but is 4 spaces.

Well, I cannot have two spaces for indentation, first of all because I don't find the code to be readable at all and my existing R programs, all of them have 4 spaces in indentations, and second, as I said earlier, it is a general configuration in my Emacs environment. Therefore, I started looking to see how I might be able to change lintr settings in this regard. Based on what I read in Using lintr, it seems that by creating a hidden file named .lintr placed in $HOME directory, one can customize lintr's behaviour, something like:

linters: linters_with_defaults(
    variable_1 = value,
    variable_2 = value,
    . . . 
  )

The problem is that I couldn't manage to find the name of the specific variable allowing to set indentation to 4 spaces instead of tabs. Does anyone have an idea about how to solve this problem?

Thanks in advance.

Edit: It's indentation_linter(), and it's new to the devel version (hence it not being in the release docs yet). Found it in the lintr NEWS.md!

It's in the development-version documentation

1 Like

Thank you very much for your help. I use Windows 11, therefore my $HOME directory is Roaming where I've just created a hidden file named: .lintr with the following content:

linters: linters_with_defaults(
    indentation_linter(indent = 4L)
)
encoding: "UTF-8"

Normally, this should set the configuration globally for all R buffers in Emacs. Yet, the very same problem persists despite having created the above-mentioned .lintr file.

Is there a (syntax) problem with my .lintr file?

Do you have options(lintr.linter_file) pointing to that file? Asking since that's what should take first precedence in the configuration options—the file should work from $HOME, but it's worth giving it a try to make sure it's parsing the file:

If this don't help, I'd recommend filing an issue in the lintr repo if you haven't already, since it looks to me like you've followed the docs.

I think this issue is yours. Posting it below so follow up can be found here:

1 Like

Awesome!! Thank you very much. This solved the problem. Indeed, what I did was opening a terminal and start a new R session. Then I simply run options() to see what were the default values for available options. And I saw that the value of "lintr.linter_file" was ".lintr" which means that the (hidden) file name was correct, except that R looks for it in the current working directory (or at least this is how I interpret it).

So what I did was simply add the following line at the end of my .Rpofile file:

options(lintr.linter_file="C:/Users/[MyUsrName]/AppData/Roaming/.lintr")

And right after that, everything worked pretty well!

I don't know whether it's just me who misunderstands completely the following paragraph in their documentation:

  1. If options(lintr.linter_file) is an absolute path, this file will be used. The default for this option is ".lintr".
  2. A linter file (that is a file named like lintr.linter_file) in the currently searched directory, i.e. the directory of the file passed to lint(). When run from lint_package(), this directory can differ for each linted file.
  3. A linter file in a parent directory of the currently-searched directory, starting from the deepest path, moving upwards one level at a time.
  4. A linter file in the user’s HOME directory.

What I understand from this, is that if someone doesn't do any setting among the first three above-mentioned parts, then finally R will look automatically for .lintr in users's HOME directory. Well, at least my case shows that this is not true and it's up to the user to specify explicitly in .Rprofile to where look for the configuration .lintr file.

Anyway, it works now.

I thank you very much for your time, your patience and your help.

Regards.

1 Like

No problem! Glad it got sorted.

Definitely worth mentioning in your issue that the fourth option in the list didn't work, since I would've interpreted the documentation the same way (and, at the very least, it's worth fixing that)!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.