Skip to content

Commit

Permalink
[sankey] enable export of SVG or PNG from sankey_explorer() (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman committed Dec 25, 2023
1 parent fe21335 commit 4b187e6
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion R/sankey_explorer.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sankey_explorer <- function(data) {
stop("You must have {shiny} installed to use `sankey_explorer()`")
}

obj_name <- deparse(substitute(data))
data <- as_sankey_data(data)

ui <- shiny::fluidPage(
Expand Down Expand Up @@ -70,7 +71,9 @@ sankey_explorer <- function(data) {
label = "tooltipLinkText:",
value = 'd.source[nodeLabel] + " \u2192 " + d.target[nodeLabel] + "<br/>" + format(d.value)',
placeholder = '(default) "d.source[nodeLabel] + " \u2192 " + d.target[nodeLabel] + "<br/>" + format(d.value)"'
)
),
shiny::downloadButton("download_svg", "save SVG"),
shiny::downloadButton("download_png", "save PNG")
),
r2d3::d3Output("d3")
)
Expand All @@ -92,6 +95,52 @@ sankey_explorer <- function(data) {
tooltipLinkText = input$tooltipLinkText
)
})

output$download_svg <- shiny::downloadHandler(
filename = function() {
paste0(obj_name, ".svg")
},
content = function(file) {
sn <- sankey_network(
data = data,
nodeId = input$nodeId,
nodeGroup = input$nodeGroup,
nodeLabel = input$nodeLabel,
nodeLabelFontFamily = input$nodeLabelFontFamily,
nodeLabelFontSize = input$nodeLabelFontSize,
linkPath = input$linkPath,
linkColor = input$linkColor,
colorScheme = input$colorScheme,
iterations = input$iterations,
nodeAlign = input$nodeAlign,
tooltipLinkText = input$tooltipLinkText
)
save_as_svg(sn, file)
}
)

output$download_png <- shiny::downloadHandler(
filename = function() {
paste0(obj_name, ".png")
},
content = function(file) {
sn <- sankey_network(
data = data,
nodeId = input$nodeId,
nodeGroup = input$nodeGroup,
nodeLabel = input$nodeLabel,
nodeLabelFontFamily = input$nodeLabelFontFamily,
nodeLabelFontSize = input$nodeLabelFontSize,
linkPath = input$linkPath,
linkColor = input$linkColor,
colorScheme = input$colorScheme,
iterations = input$iterations,
nodeAlign = input$nodeAlign,
tooltipLinkText = input$tooltipLinkText
)
save_as_png(sn, file)
}
)
}

shiny::shinyApp(ui = ui, server = server)
Expand Down

0 comments on commit 4b187e6

Please sign in to comment.