1
#' Buttons
2
#' 
3
#' Add buttons to your graph.
4
#' 
5
#' @inheritParams sg_nodes
6
#' @param event Event the button triggers, see valid events.
7
#' @param class Button \code{CSS} class, see note.
8
#' @param tag A Valid \code{htmltools} tags function.
9
#' @param id A valid CSS id.
10
#' @param position Position of button, \code{top} or \code{bottom}.
11
#' @param ... Content of the button, complient with \code{htmltools}.
12
#' 
13
#' @section Events:
14
#' \itemize{
15
#'   \item{\code{force_start}}
16
#'   \item{\code{force_stop}}
17
#'   \item{\code{noverlap}}
18
#'   \item{\code{drag_nodes}}
19
#'   \item{\code{relative_size}}
20
#'   \item{\code{add_nodes}}
21
#'   \item{\code{add_edges}}
22
#'   \item{\code{drop_nodes}}
23
#'   \item{\code{drop_edges}}
24
#'   \item{\code{animate}}
25
#'   \item{\code{export_svg}}
26
#'   \item{\code{export_img}}
27
#'   \item{\code{progress}}
28
#'   \item{\code{read_exec}}
29
#' }
30
#' 
31
#' @details You can pass multiple events as a vector, see examples. You can also pass multiple buttons.
32
#' 
33
#' @examples 
34
#' nodes <- sg_make_nodes() 
35
#' edges <- sg_make_edges(nodes)
36
#' 
37
#' # Button starts the layout and stops it after 3 seconds
38
#' sigmajs() %>% 
39
#'   sg_nodes(nodes, id, size) %>% 
40
#'   sg_edges(edges, id, source, target) %>% 
41
#'   sg_force_start() %>% 
42
#'   sg_force_stop(3000) %>% 
43
#'   sg_button(c("force_start", "force_stop"), "start layout")
44
#'   
45
#' # additional nodes
46
#' nodes2 <- sg_make_nodes()
47
#' nodes2$id <- as.character(seq(11, 20))
48
#' 
49
#' # add delay
50
#' nodes2$delay <- runif(nrow(nodes2), 500, 1000)
51
#' 
52
#' sigmajs() %>%
53
#'   sg_nodes(nodes, id, label, size, color) %>%
54
#'   sg_add_nodes(nodes2, delay, id, label, size, color) %>% 
55
#'   sg_force_start() %>% 
56
#'   sg_force_stop(3000) %>% 
57
#'   sg_button(c("force_start", "force_stop"), "start layout") %>% 
58
#'   sg_button("add_nodes", "add nodes")
59
#' 
60
#' @note The default class (\code{btn btn-default}) works with Bootstrap 3 (the default framework for Shiny and R markdown).
61
#' 
62
#' @return An object of class \code{htmlwidget} which renders the visualisation on print.
63
#' 
64
#' @export
65
sg_button <- function(sg, event, ..., position = "top", class = "btn btn-default", tag = htmltools::tags$button, id = NULL){
66
  
67 1
  if(missing(sg) ||  missing(event))
68 1
    stop("missing sg or event")
69
  
70 1
  .test_sg(sg)
71
  
72 1
  for(ev in event)
73 1
    if(!ev %in% .valid_events()) stop(paste(ev, "is not a known event"), call. = FALSE)
74
  
75 1
  if("add_nodes_edges" %in% event)
76 0
    warning("add_nodes_edges is deprecated, in favour of c('add_nodes', 'add_edges')", call. = FALSE)
77
  
78 1
  if(is.null(class)) 
79 0
    class <- ""
80
  
81 1
  if(is.null(id))
82 1
    id <- .make_rand_id()
83
  
84 1
  btn <- list(
85 1
    id = id,
86 1
    event = event
87
  )
88
  
89 1
  sg$x$button <- append(sg$x$button, list(btn))
90 1
  sg$x$buttonevent <- append(sg$x$buttonevent, event)
91
  
92 1
  if(position == "top"){
93 1
    sg %>% 
94 1
      htmlwidgets::prependContent(tag(id = id, class = paste("sigmajsbtn", class), ...))
95
  } else {
96 0
    sg %>% 
97 0
      htmlwidgets::appendContent(tag(id = id, class = paste("sigmajsbtn", class), ...))
98
  }
99
}

Read our documentation on viewing source code .

Loading