#' Export a \code{predx} data frame as a \code{predx}-formatted JSON file
#'
#' @param forecast A \code{predx} data frame.
#' @param filename character. Filename to write JSON file. If NA, JSON is returned.
#' @param overwrite boolean. Overwrite the file if it already exists.
#'
#' @return Silently saves a \code{predx}-formatted JSON if \code{filename} is specified. Otherwise returns a string in the format of the \code{predx} JSON.
#'
#' @export
#'
#' @examples
#' predx_demo <- as.predx_df(list(
#' location = c('Mercury', 'Venus', 'Earth'),
#' target = 'habitability',
#' predx = list(Binary(1e-4), Binary(1e-4), Binary(1))
#' ))
#' json_tempfile <- tempfile()
#' export_json(predx_demo, json_tempfile)
#' import_json(json_tempfile)
export_json <- function(x, filename = NA, overwrite = F) {
x <- as.predx_df(x)
# convert predx objects for efficient storage
x$predx <- predx_to_json(x$predx)
out <- jsonlite::toJSON(x, na='string', digits=NA, auto_unbox=T)
if (!is.na(filename)) {
if (!overwrite & file.exists(filename)) {
stop(paste0('"', filename, '" already exists. Use "overwrite = T" to replace.'))
} else {
writeLines(out, filename)
}
} else {
return(out)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.