Skip to content

Commit

Permalink
[force] add an interactive force_network() explorer (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman committed Dec 25, 2023
1 parent 4b187e6 commit bd002d4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ S3method(as_tree_data,tbl_graph)
export(as_force_data)
export(as_sankey_data)
export(as_tree_data)
export(force_explorer)
export(force_network)
export(sankey_network)
export(save_as_png)
Expand Down
150 changes: 150 additions & 0 deletions R/force_explorer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#' Interactive `force_network` options explorer
#'
#' @param data a network description in one of numerous forms (see details)
#'
#' @description
#' An interactive shiny widget to explore the `force_network` options.
#'
#' @md
#'
#' @export

force_explorer <- function(data) {
if (!requireNamespace("shiny", quietly = TRUE)) {
stop("You must have {shiny} installed to use `sankey_explorer()`")
}

obj_name <- deparse(substitute(data))
data <- as_force_data(data)

ui <- shiny::fluidPage(
shiny::inputPanel(
shiny::numericInput(
inputId = "width",
label = "width:",
value = NULL,
min = 1,
max = 952,
step = 1
),
shiny::numericInput(
inputId = "height",
label = "height:",
value = NULL,
min = 1,
max = 5000,
step = 1
),
shiny::numericInput(
inputId = "node_size",
label = "node_size:",
value = 4,
min = 1,
max = 80,
step = 1
),
shiny::numericInput(
inputId = "strength",
label = "strength:",
value = -300,
min = -500,
max = 80,
step = 1
),
shiny::numericInput(
inputId = "distanceMin",
label = "distanceMin:",
value = 1,
min = 1,
max = 100,
step = 1
),
shiny::numericInput(
inputId = "distanceMax",
label = "distanceMax:",
value = "Infinity",
min = 1,
max = 8000,
step = 1
),
shiny::checkboxInput(
inputId = "draw_arrows",
label = "draw_arrows:",
value = FALSE
),
shiny::checkboxInput(
inputId = "solid_arrows",
label = "solid_arrows:",
value = TRUE
),
shiny::numericInput(
inputId = "arrow_length",
label = "arrow_length:",
value = 10,
min = 1,
max = 30,
step = 1
),
shiny::numericInput(
inputId = "zoom_scale",
label = "zoom_scale:",
value = 0.5,
min = 0,
max = 10,
step = 0.1
),
shiny::checkboxInput(
inputId = "plot_static",
label = "plot_static:",
value = FALSE
),
shiny::downloadButton("download_png", "save PNG")
),
r2d3::d3Output("d3")
)

server <- function(input, output) {
output$d3 <- r2d3::renderD3({
force_network(
data = data,
width = input$width,
height = input$height,
node_size = input$node_size,
strength = input$strength,
distanceMin = input$distanceMin,
distanceMax = input$distanceMax,
draw_arrows = input$draw_arrows,
solid_arrows = input$solid_arrows,
arrow_length = input$arrow_length,
zoom_scale = input$zoom_scale,
plot_static = input$plot_static
)
})

output$download_png <- shiny::downloadHandler(
filename = function() {
paste0(obj_name, ".png")
},
content = function(file) {
warning(input$width)
plot <- force_network(
data = data,
width = input$width,
height = input$height,
node_size = input$node_size,
strength = input$strength,
distanceMin = input$distanceMin,
distanceMax = input$distanceMax,
draw_arrows = input$draw_arrows,
solid_arrows = input$solid_arrows,
arrow_length = input$arrow_length,
zoom_scale = input$zoom_scale,
plot_static = input$plot_static
)
save_as_png(plot, file)
}
)
}

shiny::shinyApp(ui = ui, server = server)
}
14 changes: 14 additions & 0 deletions man/force_explorer.Rd

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

0 comments on commit bd002d4

Please sign in to comment.