Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Last active February 10, 2020 16:58
Show Gist options
  • Save romainfrancois/cc26a44d59b0f3646d50c059d085cf30 to your computer and use it in GitHub Desktop.
Save romainfrancois/cc26a44d59b0f3646d50c059d085cf30 to your computer and use it in GitHub Desktop.
library(purrr)
library(assertthat)
let <- function(...) quos(...)
`:=` <- function(left, right){
names <- map_chr(left, quo_name)
assert_that( length(right) >= length(names) )
env <- parent.frame()
for( i in seq_along(names) ){
assign( names[i], right[[i]], envir = env)
}
invisible(NULL)
}
x <- rnorm(100)
let(low,up) := range(x)
# alternatively
library(purrr)
library(assertthat)
tie <- structure( NA, class = "tie_proxy")
`[<-.tie_proxy` <- function(x, ..., value){
dots <- quos(...)
assert_that( length(value) >= length(dots) )
env <- parent.frame()
for( i in seq_along(names) ){
dot <- dots[[i]]
if( !quo_is_missing(dot) ){
assign( quo_name(dot), value[[i]], envir = env)
}
}
invisible(x)
}
x <- rnorm(100)
tie[low,up] <- range(x)
@romainfrancois
Copy link
Author

> x <- rnorm(100)
> let(low,up) := range(x)
> low
[1] -2.327442
> up
[1] 2.273341

@romainfrancois
Copy link
Author

> let[,q25,q50,q75,q100] = quantile(x)
> q25
[1] -0.5364728
> q75
[1] 0.8586446

@gaborcsardi
Copy link

It is a pity that just defining let<- is not possible.

@romainfrancois
Copy link
Author

Yeah that was the first thing I tried. 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment