Learn R Programming

rmongodb (version 1.8.0)

mongo.bson.buffer.append.object: Append an R object onto a mongo.bson.buffer

Description

Append an R object onto a mongo.bson.buffer.

Usage

mongo.bson.buffer.append.object(buf, name, value)

Arguments

buf
(mongo.bson.buffer) The buffer object to which to append.
name
(string) The name (key) of the field appended to the buffer.
value
(object) The object to append to the buffer as a subobject.

Value

TRUE if successful; otherwise, FALSE if an error occured appending the data.

Details

This function allows you to store higher level R objects in the database without losing their attribute information. It will correctly handle data frames, matrices and arrays for instance; although, empty objects, such as a data frame with no rows, are not permitted.

Note that the names attribute will not be preserved if the object is multidimensional (although dimnames will be).

The object's value will look like this in the buffer:

 { ...
name : { R_OBJ : true, value : xxx, attr : { attr1 : yyy, attr2 : zzz } }
...  } 

name will be substituted with the value of the name parameter. xxx will be substituted with the low level value of the object (as would be appended by mongo.bson.buffer.append()). attr1 and attr2 will be substituted with the names of attributes. yyy and zzz will be substituted with the values of those attributes.

Note that it is inadvised to construct this wrapper manually as mongo.bson.value() and mongo.bson.iterator.value() bypass the special checking and handling that is done by R code that set attributes.

See Also

mongo.bson, mongo.bson.buffer, mongo.bson.buffer.append, mongo.bson.value, mongo.bson.iterator.value

Examples

Run this code
age <- c(5, 8)
height <- c(35, 47)
d <- data.frame(age=age, height=height)
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append.object(buf, "table", d)
b <- mongo.bson.from.buffer(buf)

# this produces a BSON object of the form:
# { "table" : { "R_OBJ" : true,
#               "value" : {
#                    "age"    : [ 5, 8 ],
#                    "height" : [35, 47 ]
#               },
#               "attr" : {
#                  "row.names" : [ -2147483648, -2 ],
#                  "class" : "data.frame"
#               }
#             }
# }
# row.names is stored in the compact form used for integer row names.

Run the code above in your browser using DataLab