Align the assignment operators within a highlighted area.
Before:
a <- 1:5
bbb <- 6:10
c <- letters
After:
a <- 1:5
bbb <- 6:10
c <- letters
A very simple aligner for a highlighted region's assignment operators (<-
). It does not "reflow" your code if the alignment breaks the page width (it does not do anything like Ctrl + Shift + /
). This addin also does not treat commented lines differently to uncommented lines. If there is an assignment operator within a highlighted comment line, then it will either align that operator or align other operators to it.
devtools::install_github("seasmith/AlignAssign")
When you highlight the following chunk of code (region) - whether you highlight the entirity or just a portion of the first and last lines - and then run the addin...
# This is a commented line
# So is this
a <- 1:5
b <- 6:10
copy_a <- a
# More comments
...the result will look like this.
# This is a commented line
# So is this
a <- 1:5
b <- 6:10
copy_a <- a
# More comments
Be mindful that highling a chunk of code which has assignment operators within commented lines, like the following, and running the addin...
# This is a commented line with an assignment operator <-
a <- 1:5
b <- 6:10
c <- 11:15
# There is an assignment operator <- here, too
...will result in this.
# This is a commented line with an assignment operator <-
a <- 1:5
b <- 6:10
c <- 11:15
# There is an assignment operator <- here, too
There is also no special handling of assignment operators within a function. So, if you highlighted the entire chunk below and then ran the addin...
var1 <- letters
var2 <- as.list(sample(1:26, 26))
names(var2) <- var1[unlist(var2)]
list.pos <- function(name, lst){
matches <- sapply(name, function(x){
matched <- which(names(lst) %in% x)
if(length(matched) == 0) matched <- NA
matched
})
return(matches)
}
positions <- list.pos(c("a", "bbb", "c"), var2)
...the result would look like this.
var1 <- letters
var2 <- as.list(sample(1:26, 26))
names(var2) <- var1[unlist(var2)]
list.pos <- function(name, lst){
matches <- sapply(name, function(x){
matched <- which(names(lst) %in% x)
if(length(matched) == 0) matched <- NA
matched
})
return(matches)
}
positions <- list.pos(c("a", "bbb", "c"), var2)