MyNixOS website logo
Description

Define Aliases for R Expressions.

Create aliases for other R names or arbitrarily complex R expressions. Accessing the alias acts as-if the aliased expression were invoked instead, and continuously reflects the current value of that expression: updates to the original expression will be reflected in the alias; and updates to the alias will automatically be reflected in the original expression.

aka

aka::alias() allows creating aliases for other R names or arbitrarily complex R expressions. Accessing the alias acts as-if the aliased expression were invoked instead, and continuously reflects the current value of that expression: updates to the original expression will be reflected in the alias; and updates to the alias will automatically be reflected in the original expression.

Installation

You can install the development version of aka from GitHub:

pak::pak('klmr/aka')

Example

The simplest case aliases another name:

library(aka)
x = 'hello'
alias(ax = x)
ax
## [1] "hello"
x = 'world'
ax
## [1] "world"
ax = 'goodbye'
x
## [1] "goodbye"

As we can see, updates to the original names are reflected in the alias, and updates to the alias are reflected in the original name.

As an alternative syntax, we can use an “assignment-like” form:

ax %=&% x

This form is strictly equivalent to alias(ax = x).

Furthermore, aliases can be created for complex expressions:

mercedes %=&% mtcars[grep('^Merc ', rownames(mtcars)), ]
mercedes
mpgcyldisphpdratwtqsecvsamgearcarb
Merc 240D24.44146.7623.693.1920.01042
Merc 23022.84140.8953.923.1522.91042
Merc 28019.26167.61233.923.4418.31044
Merc 280C17.86167.61233.923.4418.91044
Merc 450SE16.48275.81803.074.0717.40033
Merc 450SL17.38275.81803.073.7317.60033
Merc 450SLC15.28275.81803.073.7818.00033

… and we can even update parts of an aliased expression to modify parts of the original, underlying objects:

# Set all Mercedes engine types to V-shaped
mercedes$vs = 0

mtcars[8 : 14, 'vs', drop = FALSE]
vs
Merc 240D0
Merc 2300
Merc 2800
Merc 280C0
Merc 450SE0
Merc 450SL0
Merc 450SLC0

However, be careful when assigning to an alias of an object in a parent environment:

e = attach(new.env())
e$y = 'hello'

alias(ay = y)

This will seem to work:

ay
## [1] "hello"

… because y is found in the attached parent environment after it was not found in the current scope; however, the following will create a new variable y in the current scope, and leave e$y untouched:

ay = 'world'
e$y
## [1] "hello"
y
## [1] "world"

To prevent this: pass env_expr when defining the alias:

alias(ay = y, expr_env = e)

Why?!

aka implements an approach to reactive programming that is complementary to Shiny’s reactive programming API. Superficially, the mechanism resembles references in C++. However, under the hood they are fundamentally different. In particular, aka aliases are currently implemented as active bindings, whereas C++ references are direct aliases that are either replaced with the actual value by the compiler or converted to pointers.

This package purely provides syntactic sugar for active bindings, it does not add any functionality over manually calling makeActiveBinding() (the aliases created by akaare active bindings). It is also superficially similar to the pointr package. Unlike the latter, the API of aka uses proper R expressions instead of being stringly typed, and its usage and implementation are conceptually more straightforward and idiomatic. To allow programming on the language, aka supports bquote()’s macro interpolation syntax rather than requiring string manipulation. It also allows explicitly controlling the evaluating and defining environments of the alias.

Truth be told, for now I have not yet identified a compelling use-case for this package; it mainly exists to explore the concepts, and to provide a playground. Another purpose is to act as a tutorial for the implementation of a simple but powerful non-standard evaluation API. If you’ve found an interesting use-case, please reach out: I am genuinely curious!

Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows