Skip to content

burchill/unpackr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unpackr: Python's unpacking for variable assignment in R

Important: unpackr overwrites some of base R's assignment operators and can significantly slow down some code, depending on the circumstances. If speed is most important to you and you are fine ditching the Python analogy and looks, try the zeallot package.

Installation

devtools::install_github("burchill/unpackr")

Python's variable unpacking and assignment

In Python 3, you can unpack values for assignment in a single line of code, like

a, b, c = "A", True, [1,2,3]

which will set a, b, and c to "A", True, and [1,2,3], respectively. You can even add the * operator to make variables take zero through infinite values. For

a, *b, c = [1,2,3,4,5,6,7,8,9]

b is assigned the numbers 2-8, while a is 1 and c is 9.

R does not have this feature built-in (and cannot exactly replicate it), but the unpackr package aims to provide a close analogue for R users.

Usage

With unpackr you can recreate the first Python example like so:

library(unpackr)
a %,% b %,% c <- "A" %,% TRUE %,% 1:3

a
#>[1] "A"
b
#>[1] TRUE
c
#>[1] 1 2 3

And the second one like:

a %,*% b %,% c = 1:9

a
#>[1] 1
b
#>[1] 2 3 4 5 6 7 8
c
#>[1] 9

While Python assigns an empty list to starred variables that are assigned none of the input (below b is assigned []),

a, *b, c = [1,2]

in unpackr, b is assigned NULL:

a %,*% b %,% c = c(1, 2)

Additionally, unpackr lets you assign variables with = and <<-. = behaves just like <-, but <<- behaves as if you were using base R's <<- on each variable separately, so if a and b are found in different parent environments, they will be assigned their new values in their respective environments.

a <- "original"
f <- function() {
  b <- "inside_original"
  function() {
    a %,% b <<- 1 %,% 2
  }
}
g <- f()
g()

a
#>[1] 1
b
#> Error: object 'b' not found
get("b", environment(g))
#>[1] 2

About

Python 3-style unpacking for variable assignment in R

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages