| tojson | R Documentation |
A JSON serializer that only works on a limited types of R data (NULL,
lists, arrays, logical/character/numeric/date/time vectors). Other types of
data will be coerced to character. A character string
wrapped in js() is treated as raw JavaScript, so will not be quoted. The
function json_vector() converts an atomic R vector to JSON.
tojson(
x,
dict = getOption("xfun.tojson.dict", 0),
pretty = getOption("xfun.tojson.pretty", TRUE),
pack = getOption("xfun.tojson.pack", Inf),
mode = getOption("xfun.tojson.mode", "js")
)
json_unpack
json_reviver
js(x)
json_vector(x, to_array = FALSE, quote = TRUE, sep = ", ", mode = "js")
x |
An R object. |
dict |
A number controlling whether to dictionary-encode atomic vectors
(and data-frame columns) to shrink the output when values repeat. Defaults
to the global option |
pretty |
Whether to pretty-print the output with line breaks,
indentation, and a space after each array/object separator. Defaults to the
global option |
pack |
Whether to pack long integer vectors as base64 (little-endian
bytes, 1/2/4 bytes per value chosen by range) to shrink the output and speed
up parsing. Defaults to the global option |
mode |
Whether to emit standard JSON or JavaScript, one of With With |
to_array |
Whether to convert a vector to a JSON array (use |
quote |
Whether to double quote the elements. |
sep |
The string separating consecutive array elements ( |
json_unpack and json_reviver are character strings of JavaScript
source.
Both NULL and NA are converted to null. Named lists are converted to
objects of the form {key1: value1, key2: value2, ...}. Unnamed lists are
converted to arrays of the form [[value1], [value2], ...]. The same rules
apply to data frames since technically they are also lists. However, please
note that unnamed data frames (i.e., without column names) will be converted
to an array with each row as an array element, whereas named data frames
will have each column as an individual element. For matrices, the JSON
array will have each row as an individual element, and names are discarded.
Dates and times are coerced to character using UTC as the timezone. By
default (mode = "js") they are represented via the JavaScript expression
new Date(value) (not standard JSON but practically more useful), and
Inf/-Inf as the JavaScript literals Infinity/-Infinity; with
mode = "json" they become a plain quoted string and null, respectively,
so the output is standard JSON (see the mode argument).
A character string.
The jsonlite package provides a full JSON serializer.
library(xfun)
tojson(NULL); tojson(1:10); tojson(TRUE); tojson(FALSE)
tojson(list(a = 1, b = list(c = 1:3, d = "abc")))
tojson(list(c("a", "b"), 1:5, TRUE, Sys.Date() + 1:3))
tojson(head(iris)) # each column is in an element
tojson(unname(head(iris))) # each row is in an element
tojson(matrix(1:12, 3))
# dictionary-encode repeated values
tojson(rep(c("b", "a", NA), 3), dict = 1)
# literal JS code
tojson(list(a = 1:5, b = js("function() {return true;}")))
# pure static JSON: dates become strings and Inf becomes null
tojson(list(d = as.Date("2024-01-15"), x = c(1, Inf)), mode = "json")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.