knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)


Alphbetter contains utility addins to make the code neater


Sorting

alpha_sort()

I find it very convenient to concentrate all relevant R packages at the top of my scripts, for example:

library(magrittr)
library(dplyr)
library(ggplot2)

You can alphabetize the above code by selecting relevant lines and apply alpha_sort_addin, the output will be:

library(dplyr)
library(ggplot2)
library(magrittr)

The same goes for constants:

FIG_WIDTH = 750L
DATA_PATH = "./data"

Will become:

DATA_PATH = "./data"
FIG_WIDTH = 750L

This makes things much easier to skim.


Listing

list_funs()

When developing packages, or R scripts with many functions (like utils.R), I find it useful to generate a list of all the functions in the script.
list_funs_addin will go over the entire script and generate a sorted list of all the function names. The list is sorted alphabetically, which makes it easy to skim.

For example:

# script with some functions..

# comment about b
b <- function() { "b" }

# comment about c
c <- function() { "c" }

# comment about a
a <- function() { "a" }

Applying the addin will result in:

# script with some functions..
# 1. a()
# 2. b()
# 3. c()

# comment about b
b <- function() { "b" }

# comment about c
c <- function() { "c" }

# comment about a
a <- function() { "a" }


Code Statistics

code_stats()

Counting code lines, commented lines and blank lines.
For the following script:

# script with some functions..

# comment about b
b <- function() { "b" }

# comment about c
c <- function() { "c" }

# comment about a
a <- function() { "a" }

Applying code_stats_addin outputs:

code lines: 3
comment lines: 4
blank lines: 3
code / comments ratio = 0.75 ( 3 / 4 )


Notifications

Notify on all code lines containing read/write functions. Currently readr, data.table and base methods will be matched.

This is useful to understand what artifcats the script relies on.

For example, take the following script:

library(readr)
library(dplyr)

my_data <- read_csv("raw_data.csv")
<some logic>
my_data %>% 
  select(x1, x2) %>% 
  write_csv("final_data.csv")

Applying ArtifcatsNote addin, you will see:

# line4:    read_csv    "raw_data.csv"
# line8:    write_csv   "final_data.csv"

library(readr)
library(dplyr)

my_data <- read_csv("raw_data.csv")
<some logic>
my_data %>% 
  select(x1, x2) %>% 
  write_csv("final_data.csv")




ShaulAb/alphabetter documentation built on Oct. 13, 2019, 7:33 a.m.