R/json.R

Defines functions d3_json

Documented in d3_json

### convenience functions to use arguments to jsonlite::toJSON
###  that play nicely with d3.js

#' Create 'JSON' that 'd3.js' Expects
#'
#' @param x data, usually in the form of \code{data.frame} or \code{list}
#' @param strip \code{logical} to remove outer array.  Use \code{strip=TRUE}
#'           for hierarchies from \code{d3_nest}
#'
#' @return \code{string} of 'JSON' data
#' @export
d3_json <- function(x=NULL, strip=TRUE){
  xj <- jsonlite::toJSON(x, auto_unbox=TRUE, dataframe="rows")
  # remove array so only get first element as object
  if(strip){
    substr(xj,2,nchar(xj)-1)
  } else {
    xj
  }
}

Try the d3r package in your browser

Any scripts or data that you put into this service are public.

d3r documentation built on Oct. 2, 2023, 5:08 p.m.