diff --git a/R/writeImage.R b/R/writeImage.R index 0d6360ad..e30feb78 100755 --- a/R/writeImage.R +++ b/R/writeImage.R @@ -126,11 +126,11 @@ writeImageJaspResults <- function(plot, width = 320, height = 320, obj = TRUE, r if (image[["interactive"]] ) tryCatch( { - jsonOrTryError <- jaspGraphs::convertGgplotToPlotly(plot) + plotlyOrTryError <- jaspGraphs::convertGgplotToPlotly(plot, returnJSON = FALSE) if (exists(".fromRCPP")) { - if (isTryError(jsonOrTryError)) { - image[["interactiveConvertError"]] = gettextf("The following error occured while converting a ggplot to plotly: %s", .extractErrorMessage(jsonOrTryError)) + if (isTryError(plotlyOrTryError)) { + image[["interactiveConvertError"]] = gettextf("The following error occured while converting a ggplot to plotly: %s", .extractErrorMessage(plotlyOrTryError)) } else { if (!is.null(relativePathJson) && nzchar(relativePathJson)) { @@ -141,7 +141,7 @@ writeImageJaspResults <- function(plot, width = 320, height = 320, obj = TRUE, r fullPathPlotly <- paste(locationPlotly$root, locationPlotly$relativePath, sep="/") plotlyJsonFile <- file(fullPathPlotly) on.exit(close(plotlyJsonFile), add = TRUE) - writeLines(jsonOrTryError, plotlyJsonFile) + writeLines(.plotlyToJson(plotlyOrTryError), plotlyJsonFile) if(file.exists(fullPathPlotly)) { image[["interactiveJsonData"]] <- locationPlotly$relativePath @@ -161,6 +161,29 @@ writeImageJaspResults <- function(plot, width = 320, height = 320, obj = TRUE, r return(image) } +.plotlyToJson <- function(convertedPlotly) { + plotlyJson <- list( + data = convertedPlotly$plotly$x$data, + layout = convertedPlotly$plotly$x$layout, + hasRangeFrame = convertedPlotly$hasRangeFrame + ) + + toJSON(.decodeJsonLike(plotlyJson)) +} + +.decodeJsonLike <- function(x) { + if (is.character(x)) { + x <- decodeColNames(x) + } else if (is.list(x)) { + x[] <- lapply(x, .decodeJsonLike) + } + + if (!is.null(names(x))) + names(x) <- decodeColNames(names(x)) + + x +} + # intentionally not exported decodeplot <- function(x, ...) { UseMethod("decodeplot", x)