Skip to content

Commit

Permalink
Optional visNetwork physics
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Oct 11, 2023
1 parent b6c8ad8 commit eb2951d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Avoid newly deprecated arguments and functions in `crew` 0.5.0.9003 (https://github.com/wlnadau/crew/issues/131).
* Allow `tar_read()` etc. inside a pipeline whenever it uses a different data store (#1158, @MilesMcBain).
* Set `seed = FALSE` in `future::future()` (#1166, @svraka).
* Add a new `physics` argument to `tar_visnetwork()` and `tar_glimpse()` (#925, @Bdblodgett-usgs).

# targets 1.3.0

Expand Down
21 changes: 14 additions & 7 deletions R/class_visnetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ visnetwork_init <- function(
level_separation = NULL,
degree_from = 1L,
degree_to = 1L,
zoom_speed = 1
zoom_speed = 1,
physics = FALSE
) {
visnetwork_new(
network = network,
Expand All @@ -14,7 +15,8 @@ visnetwork_init <- function(
level_separation = level_separation,
degree_from = degree_from,
degree_to = degree_to,
zoom_speed = zoom_speed
zoom_speed = zoom_speed,
physics = physics
)
}

Expand All @@ -25,7 +27,8 @@ visnetwork_new <- function(
level_separation = NULL,
degree_from = NULL,
degree_to = NULL,
zoom_speed = NULL
zoom_speed = NULL,
physics = NULL
) {
visnetwork_class$new(
network = network,
Expand All @@ -34,7 +37,8 @@ visnetwork_new <- function(
level_separation = level_separation,
degree_from = degree_from,
degree_to = degree_to,
zoom_speed = zoom_speed
zoom_speed = zoom_speed,
physics = physics
)
}

Expand All @@ -49,14 +53,16 @@ visnetwork_class <- R6::R6Class(
degree_from = NULL,
degree_to = NULL,
zoom_speed = NULL,
physics = NULL,
initialize = function(
network = NULL,
label = NULL,
label_break = NULL,
level_separation = NULL,
degree_from = NULL,
degree_to = NULL,
zoom_speed = NULL
zoom_speed = NULL,
physics = NULL
) {
super$initialize(
network = network,
Expand All @@ -65,8 +71,9 @@ visnetwork_class <- R6::R6Class(
)
self$level_separation <- level_separation
self$degree_from <- degree_from
self$zoom_speed <- zoom_speed
self$degree_to <- degree_to
self$zoom_speed <- zoom_speed
self$physics <- physics
},
produce_shapes = function(type) {
shapes <- c(
Expand Down Expand Up @@ -98,7 +105,7 @@ visnetwork_class <- R6::R6Class(
vertices <- self$network$vertices
edges <- self$network$edges
out <- visNetwork::visNetwork(nodes = vertices, edges = edges, main = "")
out <- visNetwork::visNodes(out, physics = FALSE)
out <- visNetwork::visNodes(out, physics = self$physics)
out <- visNetwork::visEdges(
out,
smooth = list(type = "cubicBezier", forceDirection = "horizontal")
Expand Down
14 changes: 11 additions & 3 deletions R/tar_glimpse.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#' @param zoom_speed Positive numeric of length 1, scaling factor on the
#' zoom speed. Above 1 zooms faster than default, below 1 zooms
#' lower than default.
#' @param physics Logical of length 1, whether to implement interactive
#' physics in the graph, e.g. edge elasticity.
#' @examples
#' if (identical(Sys.getenv("TAR_INTERACTIVE_EXAMPLES"), "true")) {
#' tar_dir({ # tar_dir() runs code from a temp dir for CRAN.
Expand All @@ -55,6 +57,7 @@ tar_glimpse <- function(
degree_from = 1L,
degree_to = 1L,
zoom_speed = 1,
physics = FALSE,
callr_function = callr::r,
callr_arguments = targets::tar_callr_args_default(callr_function),
envir = parent.frame(),
Expand All @@ -74,6 +77,8 @@ tar_glimpse <- function(
tar_assert_scalar(zoom_speed)
tar_assert_dbl(zoom_speed)
tar_assert_positive(zoom_speed)
tar_assert_lgl(physics)
tar_assert_scalar(physics)
tar_assert_callr_function(callr_function)
tar_assert_list(callr_arguments)
targets_arguments <- list(
Expand All @@ -86,7 +91,8 @@ tar_glimpse <- function(
level_separation = level_separation,
degree_from = degree_from,
degree_to = degree_to,
zoom_speed = zoom_speed
zoom_speed = zoom_speed,
physics = physics
)
callr_outer(
targets_function = tar_glimpse_inner,
Expand All @@ -111,7 +117,8 @@ tar_glimpse_inner <- function(
level_separation,
degree_from,
degree_to,
zoom_speed
zoom_speed,
physics
) {
meta <- meta_init(path_store = path_store)
progress <- progress_init(path_store = path_store)
Expand All @@ -131,7 +138,8 @@ tar_glimpse_inner <- function(
level_separation = level_separation,
degree_from = degree_from,
degree_to = degree_to,
zoom_speed = zoom_speed
zoom_speed = zoom_speed,
physics = physics
)
visual$update()
visual$visual
Expand Down
8 changes: 7 additions & 1 deletion R/tar_visnetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tar_visnetwork <- function(
degree_from = 1L,
degree_to = 1L,
zoom_speed = 1,
physics = FALSE,
reporter = targets::tar_config_get("reporter_outdated"),
seconds_reporter = targets::tar_config_get("seconds_reporter"),
callr_function = callr::r,
Expand All @@ -64,6 +65,8 @@ tar_visnetwork <- function(
tar_assert_scalar(zoom_speed)
tar_assert_dbl(zoom_speed)
tar_assert_positive(zoom_speed)
tar_assert_lgl(physics)
tar_assert_scalar(physics)
tar_config_assert_reporter_outdated(reporter)
tar_assert_dbl(seconds_reporter)
tar_assert_scalar(seconds_reporter)
Expand All @@ -84,6 +87,7 @@ tar_visnetwork <- function(
degree_from = degree_from,
degree_to = degree_to,
zoom_speed = zoom_speed,
physics = physics,
reporter = reporter,
seconds_reporter = seconds_reporter
)
Expand Down Expand Up @@ -113,6 +117,7 @@ tar_visnetwork_inner <- function(
degree_from,
degree_to,
zoom_speed,
physics,
reporter,
seconds_reporter
) {
Expand All @@ -136,7 +141,8 @@ tar_visnetwork_inner <- function(
level_separation = level_separation,
degree_from = degree_from,
degree_to = degree_to,
zoom_speed = zoom_speed
zoom_speed = zoom_speed,
physics = physics
)
visual$update()
visual$visual
Expand Down
4 changes: 4 additions & 0 deletions man/tar_glimpse.Rd

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

4 changes: 4 additions & 0 deletions man/tar_visnetwork.Rd

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

0 comments on commit eb2951d

Please sign in to comment.