Skip to content

Align the assignment operators within a highlighted area.

Notifications You must be signed in to change notification settings

seasmith/AlignAssign

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AlignAssign

Build Status

Align the assignment operators (either <- or =) within a highlighted area.

Before:

a <- 1:5
bbb <- 6:10
c <- letters

After:

a   <- 1:5
bbb <- 6:10
c   <- letters

Align Cursors

For alignment of any text, you can align multiple cursors with the Align Cursors addin. Add multiple cursors to your document by holding Ctrl/Cmd + Alt and clicking in the RStudio editor, or by holding Alt and clicking and dragging the mouse.

What

AlignAssign contains three addins. Two addins align all of either the <- (Align Assign) or = (Align Assign 2) assignment operators within a highlighted region and the third aligns multiple cursors across lines to the same column.

None of the addins “reflow” your code if the alignment breaks the page width. They also does not treat commented lines differently to uncommented lines. If there is either one of the assignment operators within a highlighted comment line, then it will either align that operator or align other operators to it.

Install

devtools::install_github("seasmith/AlignAssign")`

You can assign each alignment addin action to a specific keyboard shortcut in RStudio in the Modify keyboard shortcuts… menu option under the Tools menu.

Examples

Align <-’s with Align Assign

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 Align Assign 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

Align =’s with Align Assign 2

The above example also works for the = operator when using the other addin, Align Assign 2. Before…

# Perosnal information
list(surname = "Crichton",
     firstName = "John",
     address = NA,
     occupation = "fugitive")

…after.

# Perosnal information
list(surname    = "Crichton",
     firstName  = "John",
     address    = NA,
     occupation = "fugitive")

Behavior of commented-out assignment operators

Be mindful that highling a chunk of code which has assignment operators within commented lines, like the following, and running the Align Assign 2 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 something like 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

Not so smart aligner

There is also no special handling of assignment operators within a function. So, if you highlighted the entire chunk below and then ran the Align Assign 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 will 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)

About

Align the assignment operators within a highlighted area.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages