Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions R/writeImage.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand All @@ -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)
Expand Down
Loading