sg_events: Events

Description Usage Arguments Details Value See Also Examples

View source: R/events.R

Description

React to user-interaction events on the server-side in Shiny.

Usage

1
sg_events(sg, events)

Arguments

sg

An object of class sigmajs as instantiated by sigmajs.

events

A vector or list of valid events (see section below).

Details

The parameter events is either a simple vector with the valid names of events (see below), e.g. c("clickNode", "overNode").

An alternative possibility for events is to pass a list of named lists, where each named list has an entry "event" with the valid event name and optionally an entry "priority" specifying the priority of the event, e.g. list(list(event = "clickNode"), list(event = "overNode", priority = "event")).

A priority of mode "event" means that the event is dispatched every time, not only when its returned value changes. Shiny's default priority "immediate" (also used when no priority is specified) would only dispatch when e.g. the clicked or hovered node is different from before. See https://shiny.rstudio.com/articles/communicating-with-js.html for more information.

Events: Valid event names to pass to events.

The corresponding Shiny events to observe have the same name, only written in lowercase, words separated with underscores, and prefixed with the outputId of the sigmajsOutput(). For example, when outputId is "graph": the clickNode event in Shiny becomes input$graph_click_node, the overNode event in Shiny becomes input$graph_over_node, and so on.

Value

An object of class htmlwidget which renders the visualisation on print.

See Also

official sigmajs documentation, Shiny article about communicating with JavaScript.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
library(shiny)

nodes <- sg_make_nodes()
edges <- sg_make_edges(nodes)

ui <- fluidPage(
  sigmajsOutput("graph"),
  p("Click on a node"),
  verbatimTextOutput("clicked")
)

server <- function(input, output){
  output$graph <- renderSigmajs({
    sigmajs() %>%
      sg_nodes(nodes, id, size, color) %>%
      sg_edges(edges, id, source, target) %>%
      sg_events("clickNode")
  })

  # capture node clicked (only fires when a new node is clicked)
  output$clicked <- renderPrint({
    c(list(clickTime = Sys.time()), input$graph_click_node)
  })
}

## Not run: shinyApp(ui, server)

server2 <- function(input, output){
  output$graph <- renderSigmajs({
    sigmajs() %>%
      sg_nodes(nodes, id, size, color) %>%
      sg_edges(edges, id, source, target) %>%
      sg_events(list(list(event = "clickNode", priority = "event")))
  })

  # capture node clicked (every time, also when clicking the same node again)
  output$clicked <- renderPrint({
    c(list(clickTime = Sys.time()), input$graph_click_node)
  })
}

## Not run: shinyApp(ui, server2)

JohnCoene/sigmajs documentation built on Feb. 1, 2021, 12:12 p.m.