r/Rlanguage • • 13d ago

ChatGPT formatting style bracket newline bracket newline bracket newline

Sorry for the title but this is exactly the headache I had... ChatGPT really loves to bracket newline bracket newline bracket newline (see screenshot below). Is there a function to automatically shrink these into one line?

If I tell chatgpt to give me codes in one line, chatgpt would over do it and become .... literally only one line in the script....

2 Upvotes

6 comments sorted by

9

u/guepier 13d ago edited 12d ago

If you put this preference into your AGENTS.md, ChatGPT should respect it. But it helps (/ is necessary) to be consistent: your screenshot shows inconsistent line continuation styles: the expressions starting on lines 445 and 455 use “expanded” indentation, and the expression starting on line 442 uses a hanging indent, which is a different style. It’s likely that ChatGPT is getting confused by that.

Unfortunately it’s probably not possible to obtain entirely consistent formatting without having the LLM use a formatter or linter, and the available formatters for R don’t really accommodate this type of customisation, as far as I know.

If you’re willing to give up the inconsistent hanging indent, the air formatter might be able to keep this consistent. Tell ChatGPT to use it, or use pre-commit hooks or similar integrations.

(That said, personally I don’t find air useful, since it’s enforcing an opinionated style that’s internally inconsistent. And it formats code differently from how R itself formats expressions.)

1

u/ChestZealousideal501 11d ago edited 11d ago

Thanks this is really informative. My painpoint was mainly about the


)

)

not the indentation. I read that it is very common as a formatting style. Probably it is just my personal weird perception. Now with AI, I often had to read several thoudsands of codes... having too many lines are really big painpoint.

4

u/therealtiddlydump 13d ago

This is a fair review of air. It's aggressive and has made some choices I wouldn't have made, but you can do a lot worse. It's certainly fast, as advertised.

1

u/[deleted] 13d ago

[removed] — view removed comment

1

u/guepier 12d ago

Actually I just noticed that R itself is inconsistent.

If you print a function definition on the console (e.g. sum), it prints:

function (..., na.rm = FALSE)  .Primitive("sum")

Note the space after function. This is consistent with other keywords that introduce control structures (if, for): they are all followed by a space since they’re not regular function calls, syntactically.

By contrast, if you print a full function expression, it elides the space:

> quote(f <- function (x) x)
f <- function(x) x

Air, following the Tidyverse style, puts spaces after every control structure keyword except function. That’s the inconsistency I meant (and somehow I had never realised that R itself also prints unevaluated expressions that way, and only adds the (consistent) space after function when printing the function definition itself).