Skip to content

Commit

Permalink
lists, tests and examples for #29
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Jan 31, 2020
1 parent 8b6a7ad commit 42c7a8d
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 100 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: jsonify
Type: Package
Title: Convert Between 'R' Objects and Javascript Object Notation (JSON)
Version: 1.0.0001
Version: 1.0.0002
Date: 2019-11-03
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre")),
Expand Down
1 change: 1 addition & 0 deletions R/scratch.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# )
#
# cat( jsonify:::rcpp_to_ndjson(lst, FALSE, -1L, TRUE, TRUE, "row") )
# cat( jsonify:::rcpp_to_ndjson(lst, FALSE, -1L, TRUE, TRUE, "column") )
#
# N <- 1e6
# data <- data.frame(
Expand Down
43 changes: 42 additions & 1 deletion R/to_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,47 @@ to_json <- function( x, unbox = FALSE, digits = NULL, numeric_dates = TRUE,
#'
#' Converts R objects to ndjson
#'
#' @inheritParams to_json
#'
#' @details
#'
#' Lists are converted to ndjson non-recursively. That is, each of the objects
#' in the list at the top level are converted to a new-line JSON object. Any nested
#' sub-elements are then contained within that JSON object. See examples
#'
#' @examples
#'
#' to_ndjson( 1:5 )
#' to_ndjson( letters )
#'
#' mat <- matrix(1:6, ncol = 2)
#'
#' to_ndjson( x = mat )
#' to_ndjson( x = mat, by = "col" )
#'
#' df <- data.frame(
#' x = 1:5
#' , y = letters[1:5]
#' , z = as.Date(seq(18262, 18262 + 4, by = 1 ), origin = "1970-01-01" )
#' )
#'
#' to_ndjson( x = df )
#' to_ndjson( x = df, numeric_dates = FALSE )
#' to_ndjson( x = df, factors_as_string = FALSE )
#' to_ndjson( x = df, by = "column" )
#'
#' ## Lists are non-recurisve; only elements `x` and `y` are converted to ndjson
#' lst <- list(
#' x = 1:5
#' , y = list(
#' a = letters[1:5]
#' , b = data.frame(i = 10:15, j = 20:25)
#' )
#' )
#'
#' to_ndjson( x = lst )
#' to_ndjson( x = lst, by = "column")
#'
#'
#' @export
to_ndjson <- function( x, unbox = FALSE, digits = NULL, numeric_dates = TRUE,
Expand All @@ -67,7 +108,7 @@ to_ndjson <- function( x, unbox = FALSE, digits = NULL, numeric_dates = TRUE,
}

handle_digits <- function( digits ) {
if( is.null( digits ) ) return(-1)
if( is.null( digits ) ) return(-1L)
return( as.integer( digits ) )
}

Expand Down
Loading

0 comments on commit 42c7a8d

Please sign in to comment.