Skip to content

Commit

Permalink
init read
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Mar 8, 2019
1 parent 958a770 commit b67893d
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 25 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export(sg_nodes2)
export(sg_noverlap)
export(sg_noverlap_p)
export(sg_progress)
export(sg_read_edges_p)
export(sg_read_exec_p)
export(sg_read_nodes_p)
export(sg_refresh_p)
export(sg_relative_size)
export(sg_scale_color)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* `sg_clear` added to clear the graph.
* `sg_change_*_p` family added to change nodes and edges attributes on the fly.
* Event improved.
* `sg_read_*_p` family of functions to easily add nodes and edges in bulk.

# sigmajs 0.1.2

Expand Down
114 changes: 114 additions & 0 deletions R/graph_proxies.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,118 @@ sg_drop_edges_delay_p <- function(proxy, data, ids, delay, refresh = TRUE, cumsu
proxy$session$sendCustomMessage("sg_drop_edges_delay_p", message)

return(proxy)
}

#' Read
#'
#' Read nodes and edges to add to the graph. Other proxy methods to add data to a graph have to add nodes and edges one by one,
#' thereby draining the browser, this method will add multiple nodes and edges more efficiently.
#'
#' @param proxy An object of class \code{sigmajsProxy} as returned by \code{\link{sigmajsProxy}}.
#' @param data A \code{data.frame} of _one_ node or edge.
#' @param ... any column.
#'
#' @section Functions:
#' \itemize{
#' \item{\code{sg_read_nodes_p} read nodes.}
#' \item{\code{sg_read_edges_p} read edges.}
#' \item{\code{sg_read_exec_p} send read nodes and edges to JavaScript front end.}
#' }
#'
#' @examples
#' library(shiny)
#'
#' ui <- fluidPage(
#' actionButton("add", "add nodes & edges"),
#' sigmajsOutput("sg")
#' )
#'
#' server <- function(input, output, session){
#'
#' nodes <- sg_make_nodes()
#' edges <- sg_make_edges(nodes)
#'
#' output$sg <- renderSigmajs({
#' sigmajs() %>%
#' sg_nodes(nodes, id, label, color, size) %>%
#' sg_edges(edges, id, source, target) %>%
#' sg_layout()
#' })
#'
#' i <- 10
#'
#' observeEvent(input$add, {
#' new_nodes <- sg_make_nodes()
#' new_nodes$id <- as.character(as.numeric(new_nodes$id) + i)
#' i <<- i + 10
#' ids <- 1:(i)
#' new_edges <- data.frame(
#' id = as.character((i * 2 + 15):(i * 2 + 29)),
#' source = as.character(sample(ids, 15)),
#' target = as.character(sample(ids, 15))
#' )
#'
#' sigmajsProxy("sg") %>%
#' sg_force_kill_p() %>%
#' sg_read_nodes_p(new_nodes, id, label, color, size) %>%
#' sg_read_edges_p(new_edges, id, source, target) %>%
#' sg_read_exec_p() %>%
#' sg_force_start_p() %>%
#' sg_refresh_p()
#' })
#'
#' }
#'
#' if(interactive()) shinyApp(ui, server)
#'
#' @name read
#' @export
sg_read_nodes_p <- function(proxy, data, ...){

.test_proxy(proxy)

# build data
nodes <- data %>%
.build_data(...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()

proxy$message$data$nodes <- nodes

return(proxy)
}

#' @rdname read
#' @export
sg_read_edges_p <- function(proxy, data, ...){
.test_proxy(proxy)

# build data
edges <- data %>%
.build_data(...) %>%
.check_ids() %>%
.check_x_y() %>%
.as_list()

proxy$message$data$edges <- edges

return(proxy)
}

#' @rdname read
#' @export
sg_read_exec_p <- function(proxy){
.test_proxy(proxy)

proxy$message$id <- proxy$id

if(is.null(proxy$message$data$edges))
proxy$message$data$edges <- list()

if(is.null(proxy$message$data$nodes))
proxy$message$data$nodes <- list()

proxy$session$sendCustomMessage("sg_read_exec_p", proxy$message)
return(proxy)
}
10 changes: 0 additions & 10 deletions R/sigmajs.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,3 @@ sigmajsProxy <- function(id, session = shiny::getDefaultReactiveDomain()) {

return(proxy)
}

# sigmajs_html <- function(id, style, class, ...){
# htmltools::tags$div(
# id = id, class = class, style = style,
# htmltools::tags$button(
# type = "button",
# style = "display:block;"
# )
# )
# }
32 changes: 21 additions & 11 deletions inst/htmlwidgets/sigmajs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,19 @@ HTMLWidgets.widget({
);
} else {

if (HTMLWidgets.shinyMode) { // If in Shiny app
// Remove previous occurences of plots in the <div>
sigmaID = document.getElementById(el.id)
while (sigmaID.firstChild) {
//The list is LIVE so it will re-index each call
sigmaID.removeChild(sigmaID.firstChild);
if (HTMLWidgets.shinyMode) {
sigInst = document.getElementById(el.id)
while (sigInst.firstChild) {
sigInst.removeChild(sigInst.firstChild);
}
s = new sigma({
graph: x.data,
settings: x.settings
});
s.refresh();
}

s = new sigma({
graph: x.data,
settings: x.settings
});
s.refresh();

renderer = s.addRenderer({
container: el.id,
type: x.type
Expand Down Expand Up @@ -1028,6 +1027,17 @@ if (HTMLWidgets.shinyMode) {
}
);

// read data
Shiny.addCustomMessageHandler('sg_read_exec_p',
function (message) {
var s = get_sigma_graph(message.id);
if (typeof s != 'undefined') {
s.graph.read(message.data)
s.refresh();
}
}
);

// noverlap
Shiny.addCustomMessageHandler('sg_noverlap_p',
function (message) {
Expand Down
82 changes: 82 additions & 0 deletions man/read.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions vignettes/shiny.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,14 @@ Easy! Just use your plot as an `input` that points to the graph id (`sg`) follow

Now that you are familiar with proxies, we can add a filter to filter nodes, edges or both.

* `sg_filter_gt_p` - filter greater than
* `sg_filter_lt_p` - filter less than
* `sg_filter_gt_p` - filter greater than $x$
* `sg_filter_lt_p` - filter less than $x$
* `sg_filter_eq_p` - filter equal to $x$
* `sg_filter_not_eq_p` - filter not equal to $x$
* `sg_filter_not_eq_p` - undo filter
* `sg_filter_undo_p` - undo filter

Let's take our basic Shiny app and add our filter.
Let's take our basic Shiny app and add our filter. Note that we pass a name to the filter, this allows referencing the filter in `sg_filter_undo_p` function.
Every time the user moves the slider the filter is actually undone and re-applied.

```{r, eval = FALSE}
Expand Down

0 comments on commit b67893d

Please sign in to comment.