Skip to content

Commit 08608dc

Browse files
committed
show/hide
1 parent 2ced63c commit 08608dc

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export(g6_add_edges)
3030
export(g6_add_nodes)
3131
export(g6_behaviors)
3232
export(g6_canvas_resize)
33-
export(g6_focus_element)
33+
export(g6_focus_elements)
3434
export(g6_options)
3535
export(g6_plugins)
3636
export(g6_proxy)

R/proxy.R

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,21 @@ g6_canvas_resize <- function(graph, width, height) {
335335
graph
336336
}
337337

338+
#' @keywords internal
339+
g6_element_action <- function(graph, ids, animation = NULL, action) {
340+
if (!any(class(graph) %in% "g6_proxy")) {
341+
stop(
342+
"Can't use g6_focus_element with g6 object. Only within shiny and using g6_proxy"
343+
)
344+
}
345+
346+
graph$session$sendCustomMessage(
347+
sprintf("%s_g6-element-action", graph$id),
348+
list(ids = ids, animation = animation, action = action)
349+
)
350+
graph
351+
}
352+
338353
#' Focus on specific elements in a g6 graph via proxy
339354
#'
340355
#' This function focuses on one or more elements (nodes/edges) in an existing g6 graph instance
@@ -361,16 +376,14 @@ g6_canvas_resize <- function(graph, width, height) {
361376
#'
362377
#' @seealso \code{\link{g6_proxy}}
363378
#' @export
364-
g6_focus_element <- function(graph, ids, animation = NULL) {
365-
if (!any(class(graph) %in% "g6_proxy")) {
366-
stop(
367-
"Can't use g6_focus_element with g6 object. Only within shiny and using g6_proxy"
368-
)
369-
}
379+
g6_focus_elements <- function(graph, ids, animation = NULL) {
380+
g6_element_action(graph, ids, animation, action = "focus")
381+
}
370382

371-
graph$session$sendCustomMessage(
372-
sprintf("%s_g6-focus-element", graph$id),
373-
list(ids = ids, animation = animation)
374-
)
375-
graph
383+
g6_hide_elements <- function(graph, ids, animation = NULL) {
384+
g6_element_action(graph, ids, animation, action = "hide")
385+
}
386+
387+
g6_show_elements <- function(graph, ids, animation = NULL) {
388+
g6_element_action(graph, ids, animation, action = "show")
376389
}

inst/demo/app.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ ui <- page_fluid(
9090
div(
9191
class = "d-flex align-items-center",
9292
actionButton("canvas_resize", "Resize canvas"),
93-
actionButton("focus", "Focus node 1")
93+
actionButton("focus", "Focus node 1"),
94+
actionButton("show", "Show node 1"),
95+
actionButton("hide", "Hide node 1")
9496
)
9597
)
9698

@@ -184,7 +186,17 @@ server <- function(input, output, session) {
184186

185187
observeEvent(input$focus, {
186188
g6_proxy("graph") |>
187-
g6_focus_element("node1", animation = list(duration = 2000))
189+
g6_focus_elements("node1", animation = list(duration = 2000))
190+
})
191+
192+
observeEvent(input$show, {
193+
g6_proxy("graph") |>
194+
g6_show_elements("node1")
195+
})
196+
197+
observeEvent(input$hide, {
198+
g6_proxy("graph") |>
199+
g6_hide_elements("node1")
188200
})
189201

190202
observe({

inst/htmlwidgets/g6.js

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

man/g6_focus_element.Rd renamed to man/g6_focus_elements.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcjs/widgets/g6.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ HTMLWidgets.widget({
6868
}
6969
})
7070

71-
// Focus element
72-
Shiny.addCustomMessageHandler(el.id + '_g6-focus-element', (m) => {
71+
// Focus/hide/show element
72+
Shiny.addCustomMessageHandler(el.id + '_g6-element-action', (m) => {
7373
try {
7474
if (m.animation !== undefined) {
75-
graph.focusElement(m.ids, m.animation);
75+
graph[`${m.action}Element`](m.ids, m.animation);
7676
} else {
77-
graph.focusElement(m.ids);
77+
graph[`${m.action}Element`](m.ids);
7878
}
7979
} catch (error) {
8080
Shiny.notifications.show({ html: error, type: 'error' })

0 commit comments

Comments
 (0)