Learn R Programming

jsonlite (version 0.9.6)

unbox: Unbox a vector or data frame

Description

This function marks an atomic vector or data frame as a http://en.wikipedia.org/wiki/Singleton_(mathematics){singleton}, i.e. a set with exactly 1 element. Thereby, the value will not turn into an array when encoded into JSON. This can only be done for atomic vectors of length 1, or data frames with exactly 1 row. Because this function alters how R objects are encoded, it should be used very sparsely, if at all.

Usage

unbox(x)

Arguments

x
atomic vector of length 1, or data frame with 1 row.

Value

  • Returns a singleton version of x.

Details

It is usually recommended to avoid this function and stick with the default encoding schema for the various R classes. The only use case for this function is if you are bound to some specific predifined JSON structure (e.g. to submit to an API), which has no natural R representation. Note that the default encoding for data frames naturally results in a collection of key-value pairs, without using unbox. If you are frequently using unbox, you're probably doing it wrong.

References

http://en.wikipedia.org/wiki/Singleton_(mathematics)

Examples

Run this code
cat(toJSON(list(foo=123)))
cat(toJSON(list(foo=unbox(123))))

x <- iris[1,]
cat(toJSON(list(rec=x)))
cat(toJSON(list(rec=unbox(x))))

Run the code above in your browser using DataLab