.Alias                 package:base                 R Documentation

_C_r_e_a_t_e _A_l_i_a_s (_P_o_i_n_t_e_r) _t_o _R _O_b_j_e_c_t

_D_e_s_c_r_i_p_t_i_o_n:

     `.Alias' creates an alias to another (part of) an R object which
     is more (memory-) efficient than usual assignment.

_U_s_a_g_e:

     new <- .Alias(expr)

_A_r_g_u_m_e_n_t_s:

    expr: an R expression; typically a name.

     new: new name by which `expr' can be accessed.

_V_a_l_u_e:

     an identical copy of `expr'.

_W_a_r_n_i_n_g:

     This has a dangerous semantic, and consequences can be unexpected
     (it can be used to defeat the call-by-value illusion). Know what
     you are doing before using `.Alias'!

_S_e_e _A_l_s_o:

     `<-' for usual assignments.

_E_x_a_m_p_l_e_s:

     mop <- options()
     mop$browser <- "a browser"   # not set on all platforms
     Op <- .Alias(mop)
     ## A change to mop is reflected in Op and vice versa
     ## -- ONLY if no new slots are created ...
     mop$digits <- "Wow!"
     Op$browser <- "another one"
     mop$browser; Op$digits
     all(names(mop) == names(Op) &
         sapply(seq(mop), function(i) all(Op[[i]] == mop[[i]])))
     ##> TRUE -- Op and mop ARE the same thing !

     mop$newslot <- pi #--->> 'newslot' ==> (shallow) COPY of 'mop'
     Op$newslot # R: still the old one, i.e. NULL
     all(names(mop) == names(Op))# no longer TRUE

     ## Feel the power: `call by reference', a function modifying its argument:
     tst.Al <- function(x) {
        y <- .Alias(x) ; attributes(y) <- NULL ; invisible()
     }
     (x0 <- structure(1:5, my.att = "Y"))
     tst.Al(x0) # *changes* x0 :
     x0
     stopifnot(is.null(attributes(x0)))

