1
sigmajs_render <- function(sg){
2 0
  assign("igraph", NULL, envir = storage_env)
3 0
  return(sg)
4
}
5

6
#' Initialise
7
#'
8
#' Initialise a graph.
9
#' 
10
#' @param width,height Dimensions of graph.
11
#' @param elementId Id of elment.
12
#' @param kill Whether to kill the graph, set to \code{FALSE} 
13
#' if using \code{\link{sigmajsProxy}}, else set to \code{TRUE}. Only useful in Shiny.
14
#' @param type Renderer type, one of \code{canvas}, \code{webgl} or \code{svg}.
15
#' 
16
#' @examples 
17
#' nodes <- sg_make_nodes()
18
#' edges <- sg_make_edges(nodes)
19
#'
20
#' sigmajs("svg") %>%
21
#'   sg_nodes(nodes, id, label, size, color) %>%
22
#'   sg_edges(edges, id, source, target) 
23
#'
24
#' @import htmlwidgets
25
#' @importFrom stats runif
26
#' @importFrom htmltools tags
27
#' 
28
#' @note Keep \code{width} at \code{100\%} for a responsive visualisation.
29
#' 
30
#' @seealso \code{\link{sg_kill}}.
31
#' 
32
#' @return An object of class \code{htmlwidget} which renders the visualisation on print.
33
#' 
34
#' @export
35
sigmajs <- function(type = NULL, width = "100%", kill = FALSE, height = NULL, elementId = NULL) {
36
  
37 1
  assign("igraph", NULL, envir = storage_env)
38

39 1
  if(!is.null(type))
40 0
    cat("Argument `type` is no longer in use.\n")
41

42
  # forward options using x
43 1
  x = list(
44 1
    events = list(),
45 1
    kill = kill,
46 1
    data = list(),
47 1
		type = type,
48 1
		button = list(),
49 1
		buttonevent = list(),
50 1
		crosstalk = list(
51 1
		  crosstalk_key = NULL,
52 1
		  crosstalk_group = NULL
53
		)
54
  )
55

56
  # create widget
57 1
  htmlwidgets::createWidget(
58 1
    name = 'sigmajs',
59 1
    x,
60 1
    width = width,
61 1
    height = height,
62 1
    package = 'sigmajs',
63 1
    elementId = elementId,
64 1
    sizingPolicy = htmlwidgets::sizingPolicy(
65 1
      browser.fill = TRUE,
66 1
      viewer.fill = TRUE,
67 1
      padding = 20
68
    ),
69 1
    preRenderHook = sigmajs_render,
70 1
    dependencies = crosstalk::crosstalkLibs()
71
  )
72
}
73

74
#' Shiny bindings for sigmajs
75
#'
76
#' Output and render functions for using sigmajs within Shiny
77
#' applications and interactive Rmd documents.
78
#'
79
#' @param outputId,id output variable to read from
80
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
81
#'   \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
82
#'   string and have \code{'px'} appended.
83
#' @param expr An expression that generates a sigmajs
84
#' @param env The environment in which to evaluate \code{expr}.
85
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
86
#'   is useful if you want to save an expression in a variable.
87
#' @param session A valid shiny session.
88
#'
89
#' @name sigmajs-shiny
90
#'
91
#' @export
92
sigmajsOutput <- function(outputId, width = '100%', height = '400px'){
93 0
  htmlwidgets::shinyWidgetOutput(outputId, 'sigmajs', width, height, package = 'sigmajs')
94
}
95

96
#' @rdname sigmajs-shiny
97
#' @export
98
renderSigmajs <- function(expr, env = parent.frame(), quoted = FALSE) {
99 0
  if (!quoted) { expr <- substitute(expr) } # force quoted
100 0
  htmlwidgets::shinyRenderWidget(expr, sigmajsOutput, env, quoted = TRUE)
101
}
102

103
#' @rdname sigmajs-shiny
104
#' @export
105
sigmajsProxy <- function(id, session = shiny::getDefaultReactiveDomain()) {
106

107 0
	proxy <- list(id = id, session = session)
108 0
	class(proxy) <- "sigmajsProxy"
109

110 0
	return(proxy)
111
}

Read our documentation on viewing source code .

Loading