2

I am new to R and am trying to set up a minimal R development environment on my machine. I want to set up an auto-formatter for R code which formats my code upon save (Like prettier, ruff or rustfmt).

After looking into the problem it seems like styler and formatR packages seem to do what I want, but these offer API's to format files from R code using functions. What I want is a command line tool (like ruff or rustfmt) with which I can set up a hook to format my files on save from my editor (Neovim and Helix). However upon going through the documentation of the libraries, neither seem to provide a command line utility.

effectively what I want is to run the following from a terminal:

toolname Rfile.R 

and get the formatted version of RFile.R.

Does anyone have any suggestions on how I can go about this? Any help is greatly appreciated.

4
  • 2
    Have you tried putting the autoformatter code into a file and running it using Rscript? stackoverflow.com/questions/28543490/…. If you're on a Unix machine you can add a shebang and make the R-file executable as well. Commented Feb 4 at 16:54
  • @AkselA No I haven't. This feels like a workaround. I was hoping to see if there was a more straight forward or standard solution. If not, I'll be sure to try this out. Commented Feb 4 at 16:57
  • 1
    You can find existing 3rd party integration implementations through styler.r-lib.org/articles/third-party-integrations.html (gh actions, ale plugin for vim, emacs formatter, ...) Commented Feb 4 at 17:05
  • The integrations look like a good option, the R language server option might make it convenient to use from my editors. I will look into them. Thanks. Commented Feb 4 at 18:33

1 Answer 1

4

Create a file rformat and make it executable.

#!/bin/sh
Rscript -e "styler::style_file('$1')"

give it execute permission:

chmod +x rformat

format files with it:

./rformat my_script.R

Or use styleR -> install.packages("styler") Save the following script as format.R:

#!/usr/bin/env Rscript

args <- commandArgs(trailingOnly = TRUE)

if (length(args) != 1) {
  stop("Usage: format.R <file>")
}

file_path <- args[1]

if (!file.exists(file_path)) {
  stop("File does not exist: ", file_path)
}
library(styler)    
style_file(file_path)

make it executable again

chmod +x format.R

and run

./format.R path/to/your/file.R

To integrate this with Neovim, you can set up an autocommand to run the script whenever you save an R file. Add the following to your Neovim configuration init.vim:

autocmd BufWritePost *.R silent !./path/to/format.R %

It also depends on your IDE. The easiest for me is to use RStudio, mark everything (Ctrl + A) and autoformat everything (Ctrl + Shift + A). It takes 2 seconds and is super easy. You could maybe even mingle with your .rProfile file and change the .last function to autoformat all code in the directory.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi thank you for the elaborate answer. This saves me a lot of legwork. This was what I was averse to doing as it seemed like a workaround, but as of now this seems to be the best option. I will accept your answer for now and update if I find a better solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.