@@ -279,20 +279,29 @@ write_fileMetadata_json <- function(scoreCodeName = "scoreCode.R",
279
279
# '
280
280
# ' @param df data frame to be transformed in JSON format rows
281
281
# ' @param scr boolean, if `TRUE` will write the new json format with metadata
282
+ # ' @param scr_batch boolean, if `TRUE` will write the batch format JSON for SCR, metadata is ignored
282
283
# ' @param metadata_columns columns names to be used as metadata. If scr is set to `FALSE`, metatada_columns is ignored
283
- # ' @return a vector of JSON strings
284
+ # ' @return a vector of JSON strings or a single json string when `scr_batch` is set to `TRUE`
284
285
# ' @examples
285
286
# '
286
287
# ' json_output <- format_data_json(mtcars)
287
288
# ' json_output
288
289
# '
290
+ # ' json_output <- format_data_json(mtcars, scr = TRUE)
291
+ # ' json_output
292
+ # '
293
+ # ' json_output <- format_data_json(mtcars, scr_batch = TRUE)
294
+ # ' jsonlite::prettify(json_output)
295
+ # '
289
296
# ' @export
290
297
# '
291
298
292
299
293
- format_data_json <- function (df , scr = FALSE , metadata_columns = NULL ){
294
-
300
+ format_data_json <- function (df , scr = FALSE , scr_batch = FALSE , metadata_columns = NULL ){
295
301
302
+ if (scr_batch & scr ) {
303
+ stop(" Only one of'scr' and 'scr_batch' can be set to TRUE" )
304
+ }
296
305
# # check is any is factor, otherwise print in write_json_row
297
306
# # will write factor as numeric string
298
307
@@ -312,6 +321,8 @@ format_data_json <- function(df, scr = FALSE, metadata_columns = NULL){
312
321
outs <- by(df , list (seq_len(nrow(df ))), write_json_row_scr ,
313
322
metadata_columns = metadata_columns )
314
323
324
+ } else if (scr_batch ) {
325
+ outs <- write_json_batch_scr(df )
315
326
} else {
316
327
317
328
outs <- by(df , list (seq_len(nrow(df ))), write_json_row )
@@ -338,7 +349,6 @@ format_data_json <- function(df, scr = FALSE, metadata_columns = NULL){
338
349
# '
339
350
# ' @noRd
340
351
341
-
342
352
write_json_row <- function (row ) {
343
353
344
354
# ## goal is to create a son in the following format
@@ -353,10 +363,10 @@ write_json_row <- function(row) {
353
363
var_vect <- paste0(' {"name": "' , colnames(row ), ' "' ,
354
364
ifelse(sapply(row , is.na ), paste0(' , "value": null' ),
355
365
ifelse(sapply(row , is.character ), paste0(' , "value": ' , ' "' , row , ' "' ),
356
- # # the "error " string output may give a silent error
366
+ # # the "\"CONVERSION_ERROR\" " string output may give a silent error
357
367
# # it is here for placeholder because if I add stop() it
358
- # # will halt the function, even though it is never returning "error "
359
- ifelse(sapply(row , is.numeric ), paste0(' , "value": ' , row ), " error " ))), ' }' )
368
+ # # will halt the function, even though it is never returning "\"CONVERSION_ERROR\" "
369
+ ifelse(sapply(row , is.numeric ), paste0(' , "value": ' , row ), " \" CONVERSION_ERROR \" " ))), ' }' )
360
370
361
371
out <- paste0(' {"inputs": [ ' , paste0(var_vect , collapse = " , " ), ' ] }' )
362
372
@@ -378,7 +388,7 @@ write_json_row <- function(row) {
378
388
# ' @examples
379
389
# '
380
390
# ' json_output <- write_json(mtcars[1,])
381
- # ' json_output
391
+ # ' jsonlite::prettify( json_output)
382
392
# '
383
393
# ' @noRd
384
394
# '
@@ -415,26 +425,100 @@ write_json_row_scr <- function(row, metadata_columns = NULL) {
415
425
meta_vect <- paste0(' "' , colnames(row_meta ), ' ": ' ,
416
426
ifelse(sapply(row_meta , is.na ), paste0(' null' ),
417
427
ifelse(sapply(row_meta , is.character ), paste0(' "' , row_meta , ' "' ),
418
- # # the "error " string output may give a silent error
428
+ # # the "\"CONVERSION_ERROR\" " string output may give a silent error
419
429
# # it is here for placeholder because if I add stop() it
420
- # # will halt the function, even though it is never returning "error "
421
- ifelse(sapply(row_meta , is.numeric ), paste0(row_meta ), " error " ))), ' ' )
430
+ # # will halt the function, even though it is never returning "\"CONVERSION_ERROR\" "
431
+ ifelse(sapply(row_meta , is.numeric ), paste0(row_meta ), " \" CONVERSION_ERROR \" " ))), ' ' )
422
432
} else {
423
433
meta_vect <- list ()
424
434
}
425
435
426
436
var_vect <- paste0(' "' , colnames(row ), ' ": ' ,
427
437
ifelse(sapply(row , is.na ), paste0(' null' ),
428
438
ifelse(sapply(row , is.character ), paste0(' "' , row , ' "' ),
429
- # # the "error " string output may give a silent error
439
+ # # the "\"CONVERSION_ERROR\" " string output may give a silent error
430
440
# # it is here for placeholder because if I add stop() it
431
- # # will halt the function, even though it is never returning "error "
432
- ifelse(sapply(row , is.numeric ), paste0(row ), " error " ))), ' ' )
441
+ # # will halt the function, even though it is never returning "\"CONVERSION_ERROR\" "
442
+ ifelse(sapply(row , is.numeric ), paste0(row ), " \" CONVERSION_ERROR \" " ))), ' ' )
433
443
434
444
out <- paste0(' {"metadata": {' , paste0(meta_vect , collapse = " , " ), " }, " ,
435
445
436
446
' "data": {' , paste0(var_vect , collapse = " , " ), ' } }' )
447
+
448
+ return (out )
449
+
450
+ }
451
+
452
+ # ' Helper to create SCR batch json
453
+ # '
454
+ # ' Helper to be used on `write_json_batch_scr`
455
+ # '
456
+ # ' @param row single row data.frame
457
+ # ' @param row_number row number
458
+ # ' @return pseudo JSON string
459
+ # '
460
+ # ' @noRd
461
+
462
+ create_batch_vector <- function (row , row_number ) {
463
+ var_vect <- paste0(" \" " , colnames(row ), " \" : " , ifelse(sapply(row ,
464
+ is.na ), paste0(" null" ), ifelse(sapply(row , is.character ),
465
+ paste0(" \" " , row , " \" " ), ifelse(sapply(row , is.numeric ),
466
+ paste0(row ), " \" CONVERSION_ERROR\" " ))), " " )
467
+ return (paste0(" [" ,row_number ," , {" , paste0(var_vect , collapse = " , " ), " } ]" ))
468
+
469
+ }
470
+
471
+ # ' Format row to SCR Batch json
472
+ # '
473
+ # ' will create a single Json for a data.frame in a newer supported format (Viya 2024.7 or above)
474
+ # ' This is a vectorized version, without data manipulation
475
+ # ' Convert all factors columns to string otherwise `ifelse()` will coerce to integer
476
+ # '
477
+ # ' @param df data.frame
478
+ # ' @return a JSON string
479
+ # ' @examples
480
+ # '
481
+ # ' json_output <- write_json_batch_scr(mtcars)
482
+ # ' jsonlite::prettify(json_output)
483
+ # '
484
+ # ' @noRd
485
+ # '
486
+
487
+ write_json_batch_scr <- function (df ) {
488
+
489
+ # ## goal is to create a json in the following format
490
+ # ## SCR now has a special batch format
491
+ # ## This is the new format since viya 2024.7
492
+ # {
493
+ # "data": [
494
+ # [
495
+ # 1,
496
+ # {
497
+ # "varnumeric1": 5.1,
498
+ # "varnumeric2": 0.2,
499
+ # "varcharacter": "setosa"
500
+ # }
501
+ # ],
502
+ # [
503
+ # 2,
504
+ # {
505
+ # "varnumeric1": 5.1,
506
+ # "varnumeric2": 0.2,
507
+ # "varcharacter": "setosa"
508
+ # }
509
+ # ]
510
+ # ]
511
+ # }
512
+
513
+ if (any(lapply(df , class ) == " factor" )) {
514
+ stop(" data.frame with factor class is not supported." )
515
+ }
516
+
517
+ data <- split(df , seq(nrow(df )))
518
+
519
+ vect <- mapply(FUN = create_batch_vector , row = data , row_number = 1 : length(data ))
437
520
521
+ out <- paste0(" { \" data\" : [" , paste0(vect , collapse = " , " )," ] }" )
438
522
return (out )
439
523
440
524
}
0 commit comments