RJSON is an object specification that is not unlike JSON, but better adapted to represent R objects (i.e., richer than JSON). It is also easier to parse and evaluate in both R and JavaScript to render the objects in both languages. RJSON objects are used by SciViews to exchange data between R and SciViews GUIs like Komodo/SciViews-K.
to_rjson(x, attributes = FALSE)
eval_rjson(rjson)
list_to_json(x)
toRjson(x, attributes = FALSE)
evalRjson(rjson)
listToJson(x)
Any R object to be converted into RJSON (do not work with objects containing C pointers, environments, promises or expressions, but should work with almost all other R objects).
If FALSE
(by default), a simple object is created by
ignoring all attributes. This is usually the suitable option to transfer data
to another language, like JavaScript that do not understand R attributes
anyway. With attributes = TRUE
, the complete information about the object
is written, so that the object could be recreated (almost) identical when
evaluated in R (but prefer save()
and load()
to transfer objects between
R sessions!).
A string containing an object specified in RJSON notation. The specification is evaluated in R... and it can contain also R code. There is no protection provided against execution of bad code. So, you must trust the source!
For to_rjson()
, a character string vector with the RJSON
specification of the argument.
For eval_rjson()
, the corresponding R object in case of a pure RJSON
object specification, or the result of evaluating the code, if it contains R
commands (for instance, a RJSONp -RJSON with padding- item where a RJSON
object is an argument of an R function that is evaluated. In this case, the
result of the evaluation is returned).
For list_to_json()
, correct (standard) JSON code is generated if x
is a
list of character strings, or lists.
JSON (JavaScript Object Notation) allows to specify fairly complex
objects that can be rather easily exchanged between languages. The notation
is also human-readable and not too difficult to edit manually (although not
advised, of course). However, JSON has too many limitations to represent R
objects (no NA
versus NaN
, no infinite numbers, no distinction between
lists and objects with attributes, or S4 objects, etc.). Moreover, JSON is
not very easy to interpret in R and the existing implementations can convert
only specified objects (simple objects, lists, data frames, ...).
RJSON slightly modifies and enhances JSON to make it: (1) more complete to represent almost any R object (except objects with pointers, environments, ..., of course), and (2) to make it very easy to parse and evaluate in both R and JavaScript (and probably many other) languages.
With attributes = FALSE
, factors and Dates are converted to their usual
character representation before encoding the RJSON object. If
attributes = TRUE
, they are left as numbers and their attributes (class,
-and levels for factor-) completely characterize them (i.e., using
eval_rjson()
and such objects recreate factors or Dates, respectively).
However, they are probably less easy to handle in JavaScript of other
language where you import the RJSON representation.
Note also that a series of objects are not yet handled correctly. These include: complex numbers, the different date flavors other that Date, functions, expressions, environments, pointers. Do not use such items in objects that you want to convert to RJSON notation.
A last restriction: you cannot have any special characters like linefeed, tabulation, etc. in names. If you want to make your names most compatible with JavaScript, note that the dot is not allowed in syntactically valid names, but the dollar sign is allowed.
# A complex R object
obj <- structure(list(
a = as.double(c(1:5, 6)),
LETTERS,
c = c(c1 = 4.5, c2 = 7.8, c3 = Inf, c4 = -Inf, NA, c6 = NaN),
c(TRUE, FALSE, NA),
e = factor(c("a", "b", "a")),
f = 'this is a "string" with quote',
g = matrix(rnorm(4), ncol = 2),
`h&$@` = data.frame(x = 1:3, y = rnorm(3),
fact = factor(c("b", "a", "b"))),
i = Sys.Date(),
j = list(1:5, y = "another item")),
comment = "My comment",
anAttrib = 1:10,
anotherAttrib = list(TRUE, y = 1:4))
# Convert to simplest RJSON, without attributes
rjson1 <- to_rjson(obj)
rjson1
#> [1] list("a" := c(1., 2., 3., 4., 5., 6.), "" := c("A",
#> [2] "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
#> [3] "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"),
#> [4] "c" := c(c1 = 4.5, c2 = 7.8, c3 = Inf, c4 = -Inf,
#> [5] NA, c6 = NaN), "" := c(TRUE, FALSE, NA), "e" := c("a",
#> [6] "b", "a"), "f" := "this is a \\"string\\" with quote",
#> [7] "g" := c(0.24288873391774, 0.168512104987275, 0.239064666838325,
#> [8] 0.236321420782995), "h&$@" := list("x" := seq(1, 3),
#> [9] "y" := c(-0.259119167915808, 0.649045697573966,
#> [10] -1.21764097652835), "fact" := c("b", "a", "b"
#> [11] )), "i" := "2024-11-14", "j" := list(
#> [12] "" := seq(1, 5), "y" := "another item"))
eval_rjson(rjson1)
#> $a
#> [1] 1 2 3 4 5 6
#>
#> [[2]]
#> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
#> [20] "T" "U" "V" "W" "X" "Y" "Z"
#>
#> $c
#> c1 c2 c3 c4 c6
#> 4.5 7.8 Inf -Inf NA NaN
#>
#> [[4]]
#> [1] TRUE FALSE NA
#>
#> $e
#> [1] "a" "b" "a"
#>
#> $f
#> [1] "this is a \"string\" with quote"
#>
#> $g
#> [1] 0.2428887 0.1685121 0.2390647 0.2363214
#>
#> $`h&$@`
#> $`h&$@`$x
#> [1] 1 2 3
#>
#> $`h&$@`$y
#> [1] -0.2591192 0.6490457 -1.2176410
#>
#> $`h&$@`$fact
#> [1] "b" "a" "b"
#>
#>
#> $i
#> [1] "2024-11-14"
#>
#> $j
#> $j[[1]]
#> [1] 1 2 3 4 5
#>
#> $j$y
#> [1] "another item"
#>
#>
# More complex RJSON, with attributes
rjson2 <- to_rjson(obj, TRUE)
rjson2
#> [1] list("Data_" := list("a" := c(1., 2., 3., 4., 5., 6.), "" := c("A",
#> [2] "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
#> [3] "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"),
#> [4] "c" := c(c1 = 4.5, c2 = 7.8, c3 = Inf, c4 = -Inf,
#> [5] NA, c6 = NaN), "" := c(TRUE, FALSE, NA), "e" := list("Data_" := c(1,
#> [6] 2, 1), "levels" := c("a", "b"), "class" := "factor"),
#> [7] "f" := "this is a \\"string\\" with quote", "g" := list("Data_" := c(0.24288873391774,
#> [8] 0.168512104987275, 0.239064666838325, 0.236321420782995), "dim" := c(2,
#> [9] 2)), "h&$@" := list("Data_" := list("x" := seq(1, 3),
#> [10] "y" := c(-0.259119167915808, 0.649045697573966,
#> [11] -1.21764097652835), "fact" := list("Data_" := c(2,
#> [12] 1, 2), "levels" := c("a", "b"), "class" := "factor")), "row.names" := seq(1, 3), "class" := "data.frame"),
#> [13] "i" := list("Data_" := 20041., "class" := "Date"),
#> [14] "j" := list("" := seq(1, 5), "y" := "another item")), "comment" := "My comment", "anAttrib" := seq(1, 10), "anotherAttrib" := list(
#> [15] "" := TRUE, "y" := seq(1, 4)))
obj2 <- eval_rjson(rjson2)
obj2
#> $a
#> [1] 1 2 3 4 5 6
#>
#> [[2]]
#> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
#> [20] "T" "U" "V" "W" "X" "Y" "Z"
#>
#> $c
#> c1 c2 c3 c4 c6
#> 4.5 7.8 Inf -Inf NA NaN
#>
#> [[4]]
#> [1] TRUE FALSE NA
#>
#> $e
#> [1] a b a
#> Levels: a b
#>
#> $f
#> [1] "this is a \"string\" with quote"
#>
#> $g
#> [,1] [,2]
#> [1,] 0.2428887 0.2390647
#> [2,] 0.1685121 0.2363214
#>
#> $`h&$@`
#> x y fact
#> 1 1 -0.2591192 b
#> 2 2 0.6490457 a
#> 3 3 -1.2176410 b
#>
#> $i
#> [1] "2024-11-14"
#>
#> $j
#> $j[[1]]
#> [1] 1 2 3 4 5
#>
#> $j$y
#> [1] "another item"
#>
#>
#> attr(,"anAttrib")
#> [1] 1 2 3 4 5 6 7 8 9 10
#> attr(,"anotherAttrib")
#> attr(,"anotherAttrib")[[1]]
#> [1] TRUE
#>
#> attr(,"anotherAttrib")$y
#> [1] 1 2 3 4
#>
# Numbers near equivalence comparison (note: identical(Robj, Robj2) is FALSE)
all.equal(obj, obj2)
#> [1] TRUE
rm(obj, obj2, rjson1, rjson2)