Robinlovelace / styler.equals

A style for equals assignment, based on the styler package
Other
13 stars 2 forks source link

How to use as addin for Rstudio #1

Open RobinKohrs opened 1 year ago

RobinKohrs commented 1 year ago

Thank you so much for this package!!

I'm a bit confused by the several options R offers for styling/reformatting your code. Especially when looking at the amazing styler package I get a little bit lost when all I want to do is just change the <- for the =.

Anyways... I was wondering if there is any way (certainly there is as described here https://github.com/r-lib/styler/issues/860 and here https://styler.r-lib.org/reference/styler_addins.html?q=addin ) to use this package here as the formatting option when using the addin from the styler packages. I'm not really understanding how to set the options in the .Rprofile file to make this work.

Especially this sentence:

"To use this style with the addin, you need to create an R package that exports this style guide, and then set the R option styler.addins_style_transformer as described here." from here https://github.com/r-lib/styler/issues/860#issuecomment-954581742 is confusing me

I tried the following in an .Rprofile file, but this is not right definitly...

library(styler.equals)

options(styler.save_after_styling = TRUE)
options(styler.addins_style_transformer = styler.equals::equals_style())

I'm sorry about any misunderstandings and really grateful for any advises.

Robinlovelace commented 1 year ago

Many thanks for opening the issue. Short answer: I'm not sure but would welcome thoughts from others, marking as help wanted accordingly.

sorhawell commented 1 year ago

Hi @Robinlovelace

Thank you for promoting equality among statisticians :)

I wrote an addin here freely based rstudio examples repo

It seems to work alright. The saveAndStylerEqualsAddin needs to be assign to a shortcut. When triggering this addin by shortcut, it saves the active document and then styles it. Not exactly to apply styling to any normal saving, but pretty close.

I will try for some days and make a revised PR.

Robinlovelace commented 1 year ago

Amazing, thanks @sorhawell and great work you on polars! I'll aim to get this on CRAN seeing as there seem to be some users...

sorhawell commented 1 year ago

@Robinlovelace many thanks :)

I'm new to using styler. In this addin, I run default styler first, and then styler.equals after.

#' @export
saveAndStylerEqualsAddin = function() {
  ctx = rstudioapi::getActiveDocumentContext()
  rstudioapi::documentSave(id = ctx$id)
  styler::style_file(path = ctx$path)
  styler.equals::style_file(path = ctx$path)
}

I was unsure if possible to run both stylers in one pass from styler.equals ?

#' @export
saveAndStylerEqualsAddin = function() {
  ctx = rstudioapi::getActiveDocumentContext()
  rstudioapi::documentSave(id = ctx$id)
  ### styler::style_file(path = ctx$path) ## MAYBE NOT NEEDED?
  styler.equals::style_file(path = ctx$path, [SOME EXTRA ARGS OR SOMETHING])
}
Robinlovelace commented 1 year ago

I was unsure if possible to run both stylers in one pass from styler.equals ?

I imagine so. One way to find out: give it a spin!