-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Highlight and Enlarge points #123
Open
janithwanni
wants to merge
30
commits into
casperhart:main
Choose a base branch
from
janithwanni:feat/highlight-enlarge-points
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
29731d3
:bug: extend getpointsbuffer
janithwanni 33319c8
:sparkles: add proxy and shiny callback
janithwanni 2918ec2
:sparkles: register shiny handlers on js entrypoint
janithwanni d425cae
:bug: refactor out the point creation function
janithwanni d8c42f1
:sparkles: add and animate random point on click
janithwanni 1231e5c
:sparkles: show points on click
janithwanni 1fda5f8
:sparkles: implemented add-edges function
janithwanni 9d2b65c
:rocket: build artifacts
janithwanni 7961dd6
:bug: fix edges being kept in the scene
janithwanni 323ebe8
:sparkles: give show_scatter_2d shiny events
janithwanni d1cbe9b
:loud_sound: add debug logs
janithwanni cf03db8
:memo: remove outdated demo
janithwanni fe64a40
:mute: remove console logs
janithwanni ae2eeb5
:sparkles: Add points and edges function
janithwanni c3bbfbc
:memo: Add demo Shiny app
janithwanni 1b2bda3
:sparkles: Add message handler
janithwanni dbfb556
:rocket: Build detourr
janithwanni 465f36d
:sparkles: points with more aes and highlighted
janithwanni 60ac581
:bug: fix box initial render glitch
janithwanni 096459a
:bug: fix box enlarge points function
janithwanni 040e8a1
:sparkles: add functions and button to clear additions
janithwanni 407ea71
:bug: Add bug fixes for null checks
janithwanni b393221
:memo: Document new functions
janithwanni 10bdad3
:lipstick: move position of the clear button
janithwanni 6bb6e2c
:memo: Update documentation for missing parameters
janithwanni 59e2528
:memo: Add demo of aesthetics
janithwanni 3cb7cdb
:rocket: Build widget
janithwanni 1e07024
:lipstick: Change icon hover text
janithwanni 33052de
:goal_net: Add exception handling scatter_3d
janithwanni 91cb06f
:rocket: Build widget
janithwanni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,185 @@ shinyRenderDisplayScatter3d <- function(expr, env = parent.frame(), quoted = FAL | |
} | ||
htmlwidgets::shinyRenderWidget(expr, displayScatter3dOutput, quoted = TRUE, env = env) | ||
} | ||
|
||
#' Send commands to a detourr instance in a Shiny app | ||
#' | ||
#' Creates a proxy object that can be used to add | ||
#' or remove points to a detour instance that has | ||
#' already being rendered using \code{\link{shinyRenderDisplayScatter3d}}. | ||
#' To be used in Shiny apps only. | ||
#' @param id output id of the detourr instance | ||
#' @param session the Shiny session object used in the app. | ||
#' Default should work for most cases | ||
#' | ||
#' @rdname detour-shiny | ||
#' @export | ||
display_scatter_proxy <- function(id, session = shiny::getDefaultReactiveDomain()) { #nolint | ||
structure(list(id = id, session = session), class = "detourr_proxy") | ||
} | ||
|
||
#' @title Add a set of points to an existing detourr instance in Shiny | ||
#' @param proxy Proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @param points Data.frame of points | ||
#' @param .data Original dataset used in creating the detourr instance | ||
#' @param .col_means Vector of column means of the original dataset. | ||
#' Defaults to the result of `attributes(scale(.data))[["scaled:center"]]` | ||
#' @param .scale_factor Numeric value to multiply the centered data. | ||
#' Defaults to `1 / max(sqrt(rowSums(scale(.data)^2)))` | ||
#' @param colour Vector or single value containing hex values of colors (or web colors) | ||
#' @param size Numeric value for the size of the added points | ||
#' @param alpha Transparency of the added points | ||
#' @return Proxy object to be used for piping | ||
#' @rdname detour-shiny | ||
#' @export | ||
add_points <- function( | ||
proxy, | ||
points, | ||
.data = NULL, | ||
.col_means = NULL, | ||
.scale_factor = NULL, | ||
colour = "black", | ||
size = 1, | ||
alpha = 1 | ||
) { | ||
if (is.null(.data)) { | ||
if (is.null(.col_means) || is.null(.scale_factor)) { | ||
cli::cli_abort(c( | ||
"Either {.var .data} or both {.var .col_means} and {.var .scale_factor} should be given", | ||
"i" = "Pass the data used to create the detourr instance as {.var .data}" | ||
)) | ||
} | ||
} else { | ||
scaled_data <- scale(.data, scale = FALSE) | ||
.col_means <- attributes(scaled_data)[["scaled:center"]] | ||
.scale_factor <- 1 / max(sqrt(rowSums(scaled_data^2))) | ||
} | ||
points <- unname(as.matrix(points)) |> | ||
scale( | ||
center = .col_means, | ||
scale = FALSE | ||
) | ||
points <- points * .scale_factor | ||
message <- list( | ||
id = proxy$id, | ||
data = apply(points, 1, as.list), | ||
config = list( | ||
colour = colour, | ||
size = size, | ||
alpha = alpha | ||
) | ||
) | ||
if (!is.null(proxy$message)) { | ||
# previous proxy message exists | ||
proxy$message$data <- message$data | ||
proxy$message$config <- message$config | ||
} else { | ||
proxy$message <- message | ||
} | ||
proxy$session$sendCustomMessage("add-points", proxy$message) | ||
return(proxy) | ||
} | ||
|
||
#' @title Function to add a bunch of lines to existing shiny instance | ||
#' | ||
#' @param proxy Proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @param edge_list Data.frame with two columns with the `from` node at first. | ||
#' The indexing of points starts with the original dataset. | ||
#' If \code{\link{add_points}} has been called before hand, | ||
#' the indexing of these points starts from the end of the original dataset. | ||
#' @return Proxy object to be used for piping | ||
#' @rdname detour-shiny | ||
#' @export | ||
add_edges <- function(proxy, edge_list) { | ||
edge_list <- edge_list |> as.matrix() |> unname() | ||
proxy$message$edges <- apply(edge_list, 1, as.list) | ||
proxy$session$sendCustomMessage("add-edges", proxy$message) | ||
return(proxy) | ||
} | ||
|
||
#' Function to highlight a given set of points | ||
#' | ||
#' The given points will have the original opacity while the other points | ||
#' will have reduced opacity | ||
#' | ||
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @param point_list Numeric vector. indexes to highlight in the prinary dataset | ||
#' @param alpha The transparency value of the points outside of the point_list | ||
#' @rdname detour-shiny | ||
#' @export | ||
highlight_points <- function(proxy, point_list, alpha = 0.3) { | ||
if (length(point_list) == 1) { | ||
point_list <- list(point_list) | ||
} | ||
proxy$message$point_list <- point_list | ||
proxy$session$sendCustomMessage("highlight-points", proxy$message) | ||
return(proxy) | ||
} | ||
|
||
#' Function to enlarge a given set of points | ||
#' | ||
#' The given points will have a larger size while the rest | ||
#' remains the same | ||
#' | ||
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @param point_list Numeric vector. indexes to enlarge in the prinary dataset | ||
#' @param size the size of the points to be enlarged | ||
#' @rdname detour-shiny | ||
#' @export | ||
enlarge_points <- function(proxy, point_list, size = 2) { | ||
if (length(point_list) == 1) { | ||
point_list <- list(point_list) | ||
} | ||
proxy$message$enlarge_point_list <- point_list | ||
proxy$message$size <- size | ||
proxy$session$sendCustomMessage("enlarge-points", proxy$message) | ||
return(proxy) | ||
} | ||
|
||
|
||
#' Function to clear added points | ||
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @rdname detour-shiny | ||
#' @export | ||
clear_points <- function(proxy) { | ||
proxy$session$sendCustomMessage("clear-points", list(id = proxy$id)) | ||
return(proxy) | ||
} | ||
|
||
#' Function to clear added edges | ||
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @rdname detour-shiny | ||
#' @export | ||
clear_edges <- function(proxy) { | ||
proxy$session$sendCustomMessage("clear-edges", list(id = proxy$id)) | ||
return(proxy) | ||
} | ||
|
||
#' Function to clear highlighted points | ||
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @rdname detour-shiny | ||
#' @export | ||
clear_highlight <- function(proxy) { | ||
proxy$session$sendCustomMessage("clear-highlight", list(id = proxy$id)) | ||
return(proxy) | ||
} | ||
|
||
#' Function to clear enlarged points | ||
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @rdname detour-shiny | ||
#' @export | ||
clear_enlarge <- function(proxy) { | ||
proxy$session$sendCustomMessage("clear-enlarge", list(id = proxy$id)) | ||
return(proxy) | ||
} | ||
|
||
#' Function to force rerender of detourr | ||
#' | ||
#' Useful when detourr will not update unless put on focus | ||
#' @param proxy proxy object created by \code{\link{display_scatter_proxy}} | ||
#' @rdname detour-shiny | ||
#' @export | ||
force_rerender <- function(proxy) { | ||
proxy$session$sendCustomMessage("clear-enlarge", list(id = proxy$id)) | ||
return(proxy) | ||
} | ||
Comment on lines
+244
to
+247
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additional function to call render on the scene |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
library(shiny) | ||
library(detourr) | ||
|
||
dataset <- tourr::flea |> | ||
dplyr::mutate(id = dplyr::row_number()) | ||
|
||
create_fake_box <- function(datum) { | ||
# expected tibble needs to have two rows with ncols(datum) + 1 columns | ||
box_dist <- rnorm(1, mean = 3) | ||
bounds_list <- rbind(datum + box_dist, datum - box_dist) |> | ||
as.list() | ||
do.call(tidyr::expand_grid, bounds_list) | ||
} | ||
|
||
main_ui <- function(id) { | ||
ns <- NS(id) | ||
fluidPage( | ||
column(6, | ||
displayScatter2dOutput( | ||
ns("detourr_out"), width = "100%", height = "400px" | ||
), | ||
textOutput(ns("detour_click_output")) | ||
), | ||
column(6, | ||
h1("Adding points and edges to detourr through Shiny"), | ||
p( | ||
"In this demonstration,", | ||
"clicking on points on the detourr instance to the left", | ||
"adds a box around the point.", | ||
"Click on the play button to view the box in different projections" | ||
) | ||
) | ||
) | ||
} | ||
|
||
main_server <- function(id) { | ||
moduleServer(id, function(input, output, session){ | ||
output$detourr_out <- shinyRenderDisplayScatter2d({ | ||
detour(dataset, | ||
tour_aes(projection = -c(id, species), colour = species, label = id) | ||
) |> | ||
tour_path(grand_tour(2), fps = 60) |> | ||
show_scatter( | ||
alpha = 0.7, | ||
axes = TRUE | ||
) | ||
}) | ||
|
||
output$detour_click_output <- renderText({ | ||
input$detourr_out_detour_click | ||
}) | ||
|
||
observeEvent(input$detourr_out_detour_click, { | ||
req( | ||
!is.null(input$detourr_out_detour_click), | ||
input$detourr_out_detour_click != -1 | ||
) | ||
data_to_send <- dataset |> | ||
dplyr::select(-species) |> | ||
dplyr::filter(id == input$detourr_out_detour_click) |> | ||
dplyr::select(-id) | ||
|
||
box_to_send <- data_to_send |> create_fake_box() | ||
|
||
cube_box <- geozoo::cube.iterate(p = ncol(data_to_send)) | ||
|
||
display_scatter_proxy(session$ns("detourr_out")) |> | ||
add_points( | ||
box_to_send, | ||
.data = dataset |> dplyr::select(-c(id, species)) | ||
) |> | ||
add_edges( | ||
edge_list = cube_box$edges | ||
) | ||
}) | ||
|
||
}) | ||
} | ||
|
||
ui <- function() { | ||
main_ui("main") | ||
} | ||
|
||
server <- function(input, output, session) { | ||
main_server("main") | ||
} | ||
|
||
shinyApp(ui, server, options = list(port = 5534)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
alpha
parameter points towards the alpha of the points outside because the points given by the id list will have an alpha of 1