Read and write data sets in the JSON-stat format.
From CRAN (most people use this):
('rjstat') install.packages
From github (development version):
(devtools)
library("ajschumacher/rjstat") install_github
(rjstat)
library
<- "https://json-stat.org/samples/oecd-canada.json"
oecd.canada.url
# Read from JSON-stat to a list of data frames:
<- fromJSONstat(readLines(oecd.canada.url))
results (results)
names
## [1] "Unemployment rate in the OECD countries 2003-2014"
## [2] "Population by sex and age group. Canada. 2012"
# You can also read in using the typically terser IDs rather than labels.
<- fromJSONstat(readLines(oecd.canada.url), naming="id")
results (results)
names
## [1] "oecd" "canada"
# Convert from a list of data frames to a JSON-stat string.
# (The data frames must have exactly one value column.)
(reshape)
library<- melt(cbind(iris, Specimen=rep(1:50, 3)),
irises =c("Species", "Specimen"))
id.vars<- toJSONstat(list(iris=irises))
irisJSONstat (substr(irisJSONstat, 1, 76))
cat
## {"version":"2.0","class":"collection","link":{"item":[{"class":"dataset","id
# You can successfully convert back and forth, but only for the features that
# make sense in both R and JSON-stat.
(fromJSONstat(irisJSONstat)[[1]])
head
## Species Specimen variable value
## 1 setosa 1 Sepal.Length 5.1
## 2 setosa 1 Sepal.Width 3.5
## 3 setosa 1 Petal.Length 1.4
## 4 setosa 1 Petal.Width 0.2
## 5 setosa 2 Sepal.Length 4.9
## 6 setosa 2 Sepal.Width 3.0