Skip to content

Commit 4126c6b

Browse files
committed
feat: implement the subject selection for spiider plot
1 parent 723c084 commit 4126c6b

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

R/tm_g_spiderplot.R

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ ui_g_spiderplot <- function(id, height) {
154154
selectInput(ns("filter_event_var"), label = "Event variable:", choices = NULL, selected = NULL, multiple = FALSE),
155155
selectInput(ns("filter_event_var_level"), label = "Select an event:", choices = NULL, selected = NULL, multiple = FALSE),
156156
colour_picker_ui(ns("colors")),
157-
sliderInput(ns("plot_height"), "Plot Height (px)", height[2], height[3], height[1])
157+
sliderInput(ns("plot_height"), "Plot Height (px)", height[2], height[3], height[1]),
158+
selectInput(ns("subjects"), "Subjects", choices = NULL, selected = NULL, multiple = TRUE),
159+
actionButton(ns("subject_tooltips"), "Show Subject Tooltips")
158160
),
159161
tags$div(
160162
bslib::card(
@@ -269,13 +271,54 @@ srv_g_spiderplot <- function(id,
269271
output$plot <- output$plot <- plotly::renderPlotly(plotly::event_register(
270272
{
271273
plotly_q()$p |>
274+
set_plot_data(session$ns("plot_data")) |>
272275
setup_trigger_tooltips(session$ns)
273276
},
274277
"plotly_selected"
275278
))
276279

280+
observeEvent(data(), {
281+
if (class(subject_var) == "choices_selected") {
282+
subject_col <- subject_var$selected
283+
} else {
284+
subject_col <- subject_var
285+
}
286+
updateSelectInput(
287+
inputId = "subjects",
288+
choices = data()[[plot_dataname]][[subject_col]]
289+
)
290+
})
291+
292+
plotly_data <- reactive({
293+
data.frame(
294+
x = unlist(input$plot_data$x),
295+
y = unlist(input$plot_data$y),
296+
customdata = unlist(input$plot_data$customdata),
297+
curve = unlist(input$plot_data$curveNumber),
298+
index = unlist(input$plot_data$pointNumber)
299+
)
300+
})
301+
277302
plotly_selected <- reactive(plotly::event_data("plotly_selected", source = "spiderplot"))
278303

304+
observeEvent(input$subject_tooltips, {
305+
hovervalues <- data()[[plot_dataname]] |>
306+
dplyr::mutate(customdata = dplyr::row_number()) |>
307+
dplyr::filter(!!rlang::sym(input$subject_var) %in% input$subjects) |>
308+
dplyr::pull(customdata)
309+
310+
hovertips <- plotly_data() |>
311+
dplyr::filter(customdata %in% hovervalues)
312+
313+
session$sendCustomMessage(
314+
"triggerTooltips",
315+
list(
316+
plotID = session$ns("plot"),
317+
tooltipPoints = jsonlite::toJSON(hovertips)
318+
)
319+
)
320+
})
321+
279322
tables_selected_q <- .plotly_selected_filter_children(
280323
data = plotly_q,
281324
plot_dataname = plot_dataname,
@@ -302,6 +345,8 @@ spiderplotly <- function(
302345
subject_var_label <- .get_column_label(data, subject_var)
303346
time_var_label <- .get_column_label(data, time_var)
304347
value_var_label <- .get_column_label(data, value_var)
348+
data <- data |>
349+
dplyr::mutate(customdata = dplyr::row_number())
305350

306351
if (is.null(size_var)) {
307352
size <- point_size
@@ -340,15 +385,17 @@ spiderplotly <- function(
340385
x = ~x,
341386
y = ~y,
342387
xend = stats::as.formula(sprintf("~%s", time_var)),
343-
yend = stats::as.formula(sprintf("~%s", value_var))
388+
yend = stats::as.formula(sprintf("~%s", value_var)),
389+
customdata = NULL
344390
) %>%
345391
plotly::add_markers(
346392
x = stats::as.formula(sprintf("~%s", time_var)),
347393
y = stats::as.formula(sprintf("~%s", value_var)),
348394
symbol = stats::as.formula(sprintf("~%s", color_var)),
349395
size = size,
350396
text = ~tooltip,
351-
hoverinfo = "text"
397+
hoverinfo = "text",
398+
customdata = ~customdata
352399
) %>%
353400
plotly::layout(
354401
xaxis = list(title = time_var_label),

R/tm_t_reactable.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ srv_t_reactable <- function(id, data, filter_panel_api, dataname, colnames, deco
224224
.make_reactable_call <- function(dataset, dataname, args) {
225225
columns <- .make_reactable_columns_call(dataset = dataset, col_defs = args$columns)
226226
call_args <- utils::modifyList(
227-
list(columns = columns, onClick = "select"),
227+
list(columns = columns, onClick = "select", selection = "multiple"),
228228
args[!names(args) %in% "columns"]
229229
)
230230
as.call(

R/utils.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,6 @@ setup_trigger_tooltips <- function(plot, ns) {
512512
513513
const icon = document.createElement('i');
514514
icon.className = 'fas fa-message';
515-
icon.setAttribute('role', 'presentation');
516-
icon.setAttribute('aria-label', 'info icon');
517515
518516
const tooltip = document.createElement('span');
519517
tooltip.className = 'plotly-icon-tooltip';
@@ -536,7 +534,7 @@ set_plot_data <- function(plot, data_id) {
536534
paste0(
537535
"
538536
function(el) {
539-
slicedData = el.data.slice(0, -1).map(({ x, y, customdata }) => ({ x, y, customdata }));
537+
slicedData = el.data.slice(0, -1).map(({ x, y, customdata, mode }) => ({ x, y, customdata, mode }));
540538
plotData = {
541539
x: [],
542540
y: [],
@@ -546,12 +544,14 @@ set_plot_data <- function(plot, data_id) {
546544
};
547545
548546
slicedData.forEach((item, curveNumber) => {
549-
for (let i = 0; i < item.x.length; i++) {
550-
plotData.pointNumber.push(i);
551-
plotData.x.push(item.x[i]);
552-
plotData.y.push(item.y[i]);
553-
plotData.customdata.push(item.customdata[i]);
554-
plotData.curveNumber.push(curveNumber);
547+
if (item.mode === 'markers') {
548+
for (let i = 0; i < item.x.length; i++) {
549+
plotData.pointNumber.push(i);
550+
plotData.x.push(item.x[i]);
551+
plotData.y.push(item.y[i]);
552+
plotData.customdata.push(item.customdata[i]);
553+
plotData.curveNumber.push(curveNumber);
554+
}
555555
}
556556
});
557557
Shiny.setInputValue('", data_id, "', plotData);

inst/triggerTooltips/triggerTooltips.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ function triggerSelectedTooltips(plotID) {
1717
if (trace.selectedpoints && Array.isArray(trace.selectedpoints)) {
1818
trace.selectedpoints.forEach((pointIndex) => {
1919
tooltipPoints.push({
20-
x: trace.x[pointIndex],
21-
y: trace.y[pointIndex],
2220
curve: curveIndex,
2321
index: pointIndex,
2422
});

0 commit comments

Comments
 (0)