Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to let expressions in r

Tags:

r

Is there some equivalent to express let expressions in r? As an example take this simple haskell code:

let x = 1 in x + 1

Many thanks in advance.

like image 684
jules Avatar asked Nov 25 '25 00:11

jules


1 Answers

Here are a few:

x <- 100

# 1
with(list(x = 1), x + 1)
## [1] 2

# 2
local(x + 1, list(x = 1))
## [1] 2

# 2a
local({
  x <- 1
  x + 1
})
## [1] 2

# 3
eval(substitute(x + 1, list(x = 1)))
## [1] 2

# 4
library(wrapr)
let(c(x = "1"),
  x + 1,
  subsMethod = "stringsubs",
  strict = FALSE)
## [1] 2

x
## [1] 100

Also there is an open issue in the lambda.r package to add let.

like image 69
G. Grothendieck Avatar answered Nov 27 '25 13:11

G. Grothendieck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!