Assign Object to Database or Frame

DESCRIPTION:
Assigns a value to a name. The location of the assignment can either be a specific frame (useful when writing functions), or a data directory.

USAGE:
assign(x, value, frame=<<see below>>, where=<<see below>>, ...)
assign.default(x, value, frame=<<see below>>, where=<<see below>>,
    immediate=F)

REQUIRED ARGUMENTS:
x:
character string giving the name of the object to be assigned.
value:
any S-PLUS object; the value to be assigned the name in x.

OPTIONAL ARGUMENTS:
frame:
number specifying in which of the frames of the current evaluation the object is to be assigned. frame=0 assigns to a database of objects (the session database) that will continue to be available throughout the current S-PLUS session, but will disappear at the end of the session.
where:
the database to which the object should be assigned. If where is supplied, it can either be a number or an object defining a database. A number implies the corresponding element of the search list, so where=2, for example, assigns an object to the second database. Using where=0 assigns to the session database. Otherwise, where is interpreted as a database, of any type. This database is accessed, but it is not put on the search list. The object is assigned to the accessed database. Normally, the accessed database is retained for the duration of the current top-level S-PLUS expression, to make repeated explicit access efficient, but see the argument immediate for overriding this.

It is an error to give both frame and where.

immediate:
logical flag: if TRUE, access the database in the where argument only for the immediate request. Do not provide any of the ordinary backup and do not retain the database for future use in the same top-level expression. This option ensures that the assignment is committed immediately. A more efficient alternative, if where is an explicit database rather than a position in the search list, and if the type of the database is known, is to call the appropriate dbwrite method directly. Most commonly, this will be a call to dbwrite.default for access to a standard S-PLUS directory of binary-object files. This argument is not meaningful if frame= has been specified, since access to frames in the evaluator is always immediate.

VALUE:
NULL.

SIDE EFFECTS:
assign is executed entirely for its side effect of assignment. Unlike the assignment operators, such as <-, assign does not return a non-NULL value. Assignments to frames are committed immediately and assignments to databases are committed when S-PLUS correctly finishes evaluating an entire expression. If an error occurs during evaluation of an expression, no assignments executed during that expression are committed.

DETAILS:
frame=1 means that the object is assigned to the expression frame. It will be available (from any frame) throughout the evaluation of the current expression, but will disappear when evaluation is complete. This is a useful way to create objects that are to be shared by several functions during the expression.

The x argument provides the name of the object that will be created. For example, assign("a$b", 1) is distinctly not the way to create the component b of object a; rather it will create an object named a$b. If you created this object, you would need to use the get function to use the object. This allows you to create objects (to correspond with objects in another language, perhaps) whose names are not legal object names in S-PLUS. If you do want to assign the b component of a, then create all of a and assign a.

If both where and frame are omitted, the assign function works exactly the same way as the <- operator; that is, it assigns (permanently) to the working data when used at the top level (typed by the user, for example), and to the local frame of a function call when used from inside a function.

This is a generic function, currently with only a default method. The methods are for the class of where; that is, if where is a special type of database, you can write an assign method to store objects into such a database. The default method creates a file containing the usual binary form of S-PLUS objects.


SEE ALSO:
Assignment , objcopy , remove , get , exists , attach , detach , search , synchronize , dbread .

EXAMPLES:
assign("abc", 1:3) # assign "abc"

assign(".Options", options, frame=0) # session dataset

assign(".Counts", counts, w=1) # to working data (even in a function)

# make up variable names in a loop for(i in 1:10) assign(paste("sample", i, sep="."), runif(10))

# save each column of matrix x under a different name for(i in seq(ncol(x))) assign(colname[i], x[,i])