Skip to content

Commit

Permalink
styleR (#664)
Browse files Browse the repository at this point in the history
* styleR as used by IUC
  • Loading branch information
drosofff authored Feb 9, 2024
1 parent 5e25392 commit 1bcc034
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
36 changes: 36 additions & 0 deletions scripts/styleR/styler.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env Rscript

library("argparse")
library("styler")

parser <- ArgumentParser(description = "Call styler")
parser$add_argument("dir",
metavar = "DIR", type = "character",
help = "File to parse"
)
parser$add_argument("--dry",
choices = c("off", "on"), default = "on"
)
args <- parser$parse_args()

file_info <- file.info(args$dir)
is_directory <- file_info$isdir

if (is_directory) {
captured_output <- capture.output({
result <- style_dir(args$dir, indent_by = 4, dry = args$dry, recursive = TRUE)
})
} else {
captured_output <- capture.output({
result <- style_file(args$dir, indent_by = 4, dry = args$dry)
})
}

n <- nrow(subset(result, changed == TRUE))
if (n > 0) {
if (args$dry == "off") {
print(paste("Changed", n, "files"))
} else {
stop(paste("Linting failed for", n, "files"))
}
}
45 changes: 45 additions & 0 deletions scripts/styleR/use_styler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Introduction

styleR will parse your R codes and fix formating issue at various levels, adapting correct
spacing, correct indentation, correct break lines and even variable naming.

The main advantage, compare to lintr is that styleR does the reformating job *in your
place*, which in some occasion may be a huge time saving.

The disavantage of this avantage is that styleR change your code *in your place*... so be
prepared.

I have tested it and was literally impressed by its ability to radically intervene on the
code. In my test case, styleR split a recursive if-else loop in tree independent
non-recursive loops. It was a beautiful simplification, and a real progress for the code
maintenance.

# Create the proper conda environment

If it does not exist yet, create the conda environnement:

```
conda create --strict-channel-priority --solver libmamba --override-channels --channel conda-forge --channel bioconda --channel defaults --name r-styler r-styler r-argparse
```

# get the styler.R script in your environment and ensure it is executable

to make style.R executable, `chmod 755 styler.R`

# `conda activate r-styler`

# Run styler.R

:warning: styler.R will modify your target file using the following command; work in a
versioned (git) environment !

```
<path>/./styler.R --dry off <path>/<Rfile-to-lint>
```

- Note that `--dry on` will **not** make any change in your code.
- For more info on the multiple parameters of styler (which can be integrated to Rstudio
and will by available in the plug-in menu), see the complete reference below.
# Reference

[https://styler.r-lib.org/index.html](https://styler.r-lib.org/index.html)

0 comments on commit 1bcc034

Please sign in to comment.