Skip to content

Instantly share code, notes, and snippets.

@danlooo
Last active July 21, 2021 13:45
Show Gist options
  • Save danlooo/ea96fcda670348c22b05322e4a8b1ce7 to your computer and use it in GitHub Desktop.
Save danlooo/ea96fcda670348c22b05322e4a8b1ce7 to your computer and use it in GitHub Desktop.
Walrus Operator in R
#' Assignment expression operator
#'
#' Creates an object with name `lhs` with the value `lhs`
#' and finally returns the value e.g. to pipe and print it
`%:=%` <- function(lhs, rhs, envir = parent.frame()) {
assign(deparse(substitute(lhs)), rhs, envir = envir)
rhs
}
# Create a varaible and print it's value
a %:=% 1337 %>% paste0("a=", .)
a
# Creates the plot object and actually plots it to the device
plt %:=% ggplot2::qplot(Sepal.Width, Sepal.Length, data = iris)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment