

assign {base}                                R Documentation

_A_s_s_i_g_n _a _V_a_l_u_e _t_o _a _N_a_m_e

_U_s_a_g_e_:

     assign(x, value, pos = -1, envir = sys.frame(sys.parent()),
            inherits = FALSE, immediate = TRUE)
     x <- value
     x <<- value
     value -> x
     value ->> x

_A_r_g_u_m_e_n_t_s_:

       x: a variable name (given as a quoted string).

   value: a value to be assigned to `x'.

     pos: an index into the search list which determines
          which environment the assignment is to take place
          in. A character string may also be used. The envi-
          ronment can also be specified directly with
          `envir'.

   envir: the `environment' in which to assign.  The default
          is the environment where the call to `assign'
          takes place.

inherits: should the enclosing frames of the environment be
          inspected?

immediate: an ignored compatibility feature.

_V_a_l_u_e_:

     This function is invoked for its side effect, which is
     assigning `value' to the variable `x'.  If no `envir'
     is specified, then the assignment takes place in the
     currently active environment.

     If `inherits' is `TRUE', parents of the supplied envi-
     ronment are searched until the variable `x' is encoun-
     tered.  The value is then assigned in the environment
     in which the variable is encountered.  If the symbol is
     not encountered then assignment takes place in the
     global environment.

     If `inherits' is `FALSE', assignment takes place in the
     initial frame of `envir'.

     The arrow forms of assignment provide shortcut ways to
     carry out assignment.  The `<-' and `->' forms carry
     out assignment in the local environment frame, while
     the `<<-' and `->>' forms cause a search to made
     through the environment for an existing definition of
     the variable being assigned.  If such a variable is
     found then its value is redefined, otherwise assignment
     takes place globally.

     Note that the action of `<<-' and `->>' differs from
     that in the S language, but is useful in conjunction
     with the scoping rules of R.

_S_e_e _A_l_s_o_:

     `get', `environment'.

_E_x_a_m_p_l_e_s_:

     for(i in 1:6) { #-- Create objects  'r1', 'r2', ... 'r6' --
      nam <- paste("r",i, sep=".")
      assign(nam, 1:i)
     }
     ls(pat="^r..$")

     ##-- Global assignment within a function:
     myf <- function(x) {
      innerf <- function(x) assign("Global.res", x^2, env = .GlobalEnv)
      innerf(x+1)
     }
     myf(3)
     Global.res # 16

