Skip to content

Commit 0be199b

Browse files
committed
fix: retain the labels for grouped vars and color var
1 parent 4889032 commit 0be199b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

R/tm_p_swimlane.R

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ srv_p_swimlane <- function(id,
184184

185185
plotly_q <- reactive({
186186
req(data(), input$time_var, input$subject_var, input$color_var, input$group_var, input$sort_var, color_inputs())
187-
print(input$subject_var)
188187
adjusted_symbols <- .shape_palette_discrete(
189188
levels = unique(data()[[plot_dataname]][[input$color_var]]),
190189
symbol = point_symbols
@@ -237,7 +236,15 @@ srv_p_swimlane <- function(id,
237236

238237
p <- plot_data %>%
239238
dplyr::mutate(
240-
!!as.name(color_var) := factor(!!as.name(color_var), levels = names(colors)),
239+
!!as.name(color_var) := {
240+
# Store the original label
241+
original_label <- attr(.data[[color_var]], "label")
242+
# Create the factor
243+
new_factor <- factor(.data[[color_var]], levels = names(colors))
244+
# Restore the label
245+
attr(new_factor, "label") <- original_label
246+
new_factor
247+
}
241248
) %>%
242249
dplyr::group_by(!!as.name(subject_var), !!as.name(time_var)) %>%
243250
dplyr::mutate(
@@ -259,14 +266,25 @@ srv_p_swimlane <- function(id,
259266
if (is.null(tooltip_vars)) {
260267
default_tip
261268
} else {
262-
cur_data <- dplyr::pick(dplyr::everything())
269+
cur_data <- dplyr::cur_data()
270+
grouping_vars <- list()
271+
grouping_vars[[subject_var]] <- dplyr::cur_group()[[subject_var]]
272+
grouping_vars[[time_var]] <- dplyr::cur_group()[[time_var]]
273+
cur_data <- c(cur_data, grouping_vars)
274+
263275
cols <- intersect(tooltip_vars, names(cur_data))
264276
if (!length(cols)) {
265277
default_tip
266278
} else {
267279
sub <- cur_data[cols]
268280
labels <- vapply(cols, function(cn) {
269-
lb <- attr(sub[[cn]], "label")
281+
if (cn == subject_var) {
282+
lb <- subject_var_label
283+
} else if (cn == time_var) {
284+
lb <- time_var_label
285+
} else {
286+
lb <- attr(sub[[cn]], "label")
287+
}
270288
if (length(lb) && !is.null(lb) && !is.na(lb)) as.character(lb) else cn
271289
}, character(1))
272290
values <- lapply(sub, as.character)

0 commit comments

Comments
 (0)