Reformat code function

I like the way RStudio formats R code (Code -> Reformat Code or 'Ctrl + Shift + A'). I would like to apply this a githook or GitHub action, but I cannot find out what command is actually run or how to achieve similar with formatR or Styler.

Any idea what package and command is run behind the scenes or how to replicate this?

For {styler}, you can use the Addin in RStudio and create a keyboard shortcut for it. With GitHub Actions, there is a /style command, I build {precommit} to use {styler} as a git pre-commit hook. All of this is linked in the documentation of {styler}.

Thanks, I might try that. I had used styler but it did not format the code in a nice way like RStudio does.

Can you be more specific about what you did not like? We might be able to improve it (I am the maintainer of {styler})

Hi, thanks for the reply. It may be my lack of understanding as how to customise Styler (if that is possible?). (Please bare with me as my terminology will be off as I come from Python/C) If a list of arguments exceeds the set line width, then styler breaks the line at the most convenient point and starts a new line. RStudio on the other hand breaks at each argument and indents/aligns them. I find this much more readable

1 Like

{styler} does not currently support breaking lines at a certain character width, but it's a highly requested feature. The line breaking you see is because of how lines are broken for function calls as specified in the tidyverse style guide. It's non-trivial (I think) to customize {styler} to break lines at a certain width that works in all cases, gives desired outputs and does not cause significant slow-down.

This topic was automatically closed 21 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.