11# ################################################################
2- #
2+ #
33# File: write.json.stat.R
44# Purpose: Write an object of class 'px' a file with json-stat format
55# See http://json-stat.org/
66#
77# Created: 20130813
88# Authors: cjgb
99#
10- # Modifications:
10+ # Modifications:
11+ # cjgb, 20250324 - Remove the dependency on RJSONIO
1112#
1213# ################################################################
1314
1415
15- write.json.stat <- function ( obj.px , filename ){
16-
17- if ( ! inherits( obj.px , " px" ) )
18- stop(" Error: object needs to have class 'px'" )
19-
20- aa <- as.array(obj.px )
16+ write.json.stat <- function (obj.px , filename ){
17+
18+ if (! inherits(obj.px , " px" ))
19+ stop(" Error: object needs to have class 'px'" )
20+
21+ aa <- as.array(obj.px )
22+ js.dim.id <- names(dimnames(aa ))
23+ js.dim.size <- sapply(dimnames(aa ), length )
24+ names(js.dim.size ) <- NULL
25+
26+ js.dim.ids <- lapply(
27+ dimnames(aa ),
28+ function (x ) {
29+ list (category = list (index = x ))
30+ }
31+ )
32+
33+ js.value <- as.numeric(aa )
34+ js.updated <- as.character(Sys.time())
35+ js.source <- obj.px $ SOURCE $ value
36+ js.label <- obj.px $ TITLE $ value
37+
38+ js <- list (
39+ label = js.label ,
40+ source = js.source ,
41+ updated = js.updated ,
42+ value = js.value ,
43+ dimension = c(list (id = js.dim.id , size = js.dim.size ), js.dim.ids )
44+ )
45+
46+ js <- list (js )
47+ names(js ) <- obj.px $ MATRIX $ value
2148
22- js.dim.id <- names(dimnames(aa ))
23-
24- js.dim.size <- sapply(dimnames(aa ), length )
25- names(js.dim.size ) <- NULL
26-
27- js.dim.ids <- lapply( dimnames(aa ),
28- function (x ){
29- list ( category = list ( index = x ) )
30-
31- }
32- )
33-
34- js.value <- as.numeric(aa )
35-
36- js.updated <- as.character(Sys.time())
37- js.source <- obj.px $ SOURCE $ value
38- js.label <- obj.px $ TITLE $ value
39-
40-
41- js <- list ( label = js.label ,
42- source = js.source ,
43- updated = js.updated ,
44- value = js.value ,
45- dimension = c( list ( id = js.dim.id ,
46- size = js.dim.size ),
47-
48- js.dim.ids
49-
50- )
51- )
52-
53- js <- list (js )
54- names(js ) <- obj.px $ MATRIX $ value
55-
56-
57- # encoding must be UTF-8 for json files
58- con <- file( description = filename , open = " w" , encoding = " UTF-8" )
59- on.exit( close(con ) )
60-
61- cat(toJSON(js ), file = con )
62-
63- }
49+ write_json(
50+ js ,
51+ path = filename ,
52+ auto_unbox = TRUE ,
53+ pretty = TRUE ,
54+ encoding = " UTF-8" )
6455
56+ }
0 commit comments