Skip to content

Commit 2a740c4

Browse files
committed
Text is converted to grid to simulate plot output
1 parent 160823b commit 2a740c4

File tree

1 file changed

+84
-54
lines changed

1 file changed

+84
-54
lines changed

R/tm_variable_browser.R

Lines changed: 84 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,11 @@ srv_variable_browser <- function(id,
263263
establish_updating_selection(datanames, input, plot_var, columns_names)
264264

265265
# validations
266-
validation_checks <- validate_input(input, plot_var, data)
266+
validation_checks <- validate_input(req(input), req(plot_var), req(data))
267267

268268
# data_for_analysis is a list with two elements: a column from a dataset and the column label
269269
plotted_data <- reactive({
270+
req(input, plot_var, data)
270271
validation_checks()
271272
get_plotted_data(input, plot_var, data)
272273
})
@@ -446,6 +447,7 @@ srv_variable_browser <- function(id,
446447

447448

448449
variable_plot_r <- reactive({
450+
req(plotted_data())
449451
display_density <- `if`(is.null(input$display_density), FALSE, req(input$display_density))
450452
remove_outliers <- `if`(is.null(input$remove_outliers), FALSE, req(input$remove_outliers))
451453

@@ -456,8 +458,6 @@ srv_variable_browser <- function(id,
456458
outlier_definition <- 0
457459
}
458460

459-
req(plotted_data())
460-
# browser()
461461
pvs <- plot_var_summary(
462462
qenv = plotted_data(),
463463
# var = plotted_data()$data,
@@ -473,15 +473,19 @@ srv_variable_browser <- function(id,
473473
pvs
474474
})
475475

476+
plot_r <- reactive({
477+
req(variable_plot_r())[["plot"]]
478+
})
479+
476480
pws <- teal.widgets::plot_with_settings_srv(
477481
id = "variable_plot",
478-
plot_r = req(variable_plot_r),
482+
plot_r = plot_r,
479483
height = c(500, 200, 2000)
480484
)
481485

482486
output$variable_summary_table <- DT::renderDataTable({
483487
var_summary_table(
484-
plotted_data()$ANL[, 1],
488+
plotted_data()$ANL[, 1, drop = TRUE],
485489
treat_numeric_as_factor(),
486490
input$variable_summary_table_rows,
487491
if (!is.null(input$remove_outliers) && input$remove_outliers) {
@@ -495,10 +499,8 @@ srv_variable_browser <- function(id,
495499

496500
output_q <- reactive({
497501
validation_checks()
498-
req(variable_plot_r())
499-
qenv <- teal.data::teal_data(plot = variable_plot_r()) |> teal.code::eval_code("plot")
500-
tc <- teal.reporter::teal_card(qenv)
501-
tc[length(tc)]
502+
qenv <- req(variable_plot_r())
503+
teal.reporter::as.teal_card(qenv)
502504
})
503505
set_chunk_dims(pws, output_q)
504506
})
@@ -646,30 +648,44 @@ plot_var_summary <- function(qenv,
646648
checkmate::assert_class(ggplot2_args, "ggplot2_args")
647649

648650
var_name <- names(qenv$ANL)
651+
652+
teal.reporter::teal_card(qenv) <- c(teal.reporter::teal_card(qenv), "### Plot")
653+
649654
var <- qenv$ANL[[var_name]]
650-
browser()
651-
plot_main <- if (is.factor(var) || is.character(var) || is.logical(var)) {
655+
qenv_plot <- if (is.factor(var) || is.character(var) || is.logical(var)) {
652656
groups <- unique(as.character(var))
653657
len_groups <- length(groups)
654658
if (len_groups >= records_for_factor) {
655-
grid::textGrob(
656-
sprintf(
659+
qenv_plot <- within(qenv, {
660+
groups <- unique(as.character(ANL[[var]]))
661+
len_groups <- length(groups)
662+
text <- sprintf(
657663
"%s unique values\n%s:\n %s\n ...\n %s",
658664
len_groups,
659-
teal.data::col_labels(qenv$ANL),
665+
teal.data::col_labels(ANL),
660666
paste(utils::head(groups), collapse = ",\n "),
661667
paste(utils::tail(groups), collapse = ",\n ")
662-
),
663-
x = grid::unit(1, "line"),
664-
y = grid::unit(1, "npc") - grid::unit(1, "line"),
665-
just = c("left", "top")
668+
)
669+
plot <- gridExtra::arrangeGrob(
670+
grid::textGrob(
671+
text,
672+
x = grid::unit(1, "line"),
673+
y = grid::unit(1, "npc") - grid::unit(1, "line"),
674+
just = c("left", "top")
675+
),
676+
ncol = 1
677+
)
678+
},
679+
var = var_name
666680
)
667681
} else {
668682
if (!is.null(wrap_character)) {
669683
qenv <- within(qenv, {
670-
ANL <- mutate(ANL, var := stringr::str_wrap(var, width = wrap_character))
684+
col_label <- attr(ANL[[var]], "label")
685+
ANL[[var]] <- stringr::str_wrap(ANL[[var]], width = wrap_character)
686+
attr(ANL[[var]], "label") <- col_label
671687
},
672-
var = as.name(var_name),
688+
var = var_name,
673689
wrap_character = wrap_character)
674690
}
675691

@@ -682,14 +698,13 @@ plot_var_summary <- function(qenv,
682698
}
683699
qenv_plot <- within(qenv, {
684700
plot <- ANL %>%
685-
mutate(var_name = as.factor(var_name)) %>%
686701
ggplot2::ggplot(ggplot2::aes(x = forcats::fct_infreq(var_name))) +
687702
ggplot2::geom_bar(
688703
stat = "count", ggplot2::aes(fill = ifelse(is.na(var_name), "withcolor", "")), show.legend = FALSE
689704
) +
690705
ggplot2::scale_fill_manual(values = c("gray50", "tan"))
691706
},
692-
var = var,
707+
var = var_name,
693708
var_name = as.name(var_name))
694709
}
695710
} else if (is.numeric(var)) {
@@ -701,11 +716,14 @@ plot_var_summary <- function(qenv,
701716
if (numeric_as_factor) {
702717
var <- factor(var)
703718
qenv_plot <- within(qenv, {
719+
col_label <- attr(ANL[[var]], "label")
720+
ANL[[var]] <- as.factor(ANL[[var]])
721+
attr(ANL[[var]], "label") <- col_label
704722
p <- ANL %>%
705-
transmute(var_name = as.factor(var_name)) %>%
706723
ggplot2::ggplot(ggplot2::aes(x = var_name)) +
707724
ggplot2::geom_histogram(stat = "count")
708725
},
726+
var = var_name,
709727
var_name = as.name(var_name))
710728
} else {
711729
# remove outliers
@@ -733,7 +751,7 @@ plot_var_summary <- function(qenv,
733751
## histogram
734752
binwidth <- get_bin_width(var)
735753
qenv_plot <- within(qenv, {
736-
p <- ggplot2::ggplot(data = ANL, ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
754+
plot <- ggplot2::ggplot(data = ANL, ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
737755
ggplot2::geom_histogram(binwidth = binwidth) +
738756
ggplot2::scale_y_continuous(
739757
sec.axis = ggplot2::sec_axis(
@@ -748,42 +766,57 @@ plot_var_summary <- function(qenv,
748766

749767
if (display_density) {
750768
qenv_plot <- within(qenv_plot, {
751-
p <- p + ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(count * binwidth)))
769+
plot <- plot + ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(count * binwidth)))
752770
})
753771
}
754-
qenv_plot <- within(qenv_plot, {
755-
p <- p + ggplot2::annotate(
756-
geom = "text",
757-
label = outlier_text,
758-
x = Inf, y = Inf,
759-
hjust = 1.02, vjust = 1.2,
760-
color = "black",
761-
# explicitly modify geom text size according
762-
size = size
763-
)
764-
},
765-
outlier_text = outlier_text,
766-
size = ggplot2_args[["theme"]][["text"]][["size"]] / 3.5)
772+
if (outlier_definition != 0) {
773+
qenv_plot <- within(qenv_plot, {
774+
plot <- plot + ggplot2::annotate(
775+
geom = "text",
776+
label = outlier_text,
777+
x = Inf, y = Inf,
778+
hjust = 1.02, vjust = 1.2,
779+
color = "black",
780+
# explicitly modify geom text size according
781+
size = size
782+
)
783+
},
784+
outlier_text = outlier_text,
785+
size = ggplot2_args[["theme"]][["text"]][["size"]] / 3.5)
786+
787+
}
767788
qenv_plot
768789
}
790+
qenv_plot
769791
} else if (inherits(var, "Date") || inherits(var, "POSIXct") || inherits(var, "POSIXlt")) {
770792
var_num <- as.numeric(var)
771793
binwidth <- get_bin_width(var_num, 1)
772-
qenv_plot <- within(qenv_plot, {
773-
p <- ANL %>%
794+
qenv_plot <- within(qenv, {
795+
col_label <- attr(ANL[[var]], "label")
796+
ANL[[var]] <- as.numeric(ANL[[var]])
797+
attr(ANL[[var]], "label") <- col_label
798+
plot <- ANL %>%
774799
mutate(var_name = as.numeric(var_name)) %>%
775800
ggplot2::ggplot(ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
776801
ggplot2::geom_histogram(binwidth = binwidth)
777802
},
778803
binwidth = binwidth,
804+
var = var_name,
779805
var_name = as.name(var_name))
780806
} else {
781-
grid::textGrob(
782-
paste(strwrap(
783-
utils::capture.output(utils::str(var)),
784-
width = .9 * grid::convertWidth(grid::unit(1, "npc"), "char", TRUE)
785-
), collapse = "\n"),
786-
x = grid::unit(1, "line"), y = grid::unit(1, "npc") - grid::unit(1, "line"), just = c("left", "top")
807+
qenv_plot <- within(qenv, {
808+
plot <- gridExtra::arrangeGrob(
809+
grid::textGrob(
810+
paste(strwrap(
811+
utils::capture.output(utils::str(ANL[[var]])),
812+
width = .9 * grid::convertWidth(grid::unit(1, "npc"), "char", TRUE)
813+
), collapse = "\n"),
814+
x = grid::unit(1, "line"), y = grid::unit(1, "npc") - grid::unit(1, "line"), just = c("left", "top")
815+
),
816+
ncol = 1
817+
)
818+
},
819+
var = var_name
787820
)
788821
}
789822

@@ -796,19 +829,16 @@ plot_var_summary <- function(qenv,
796829
module_plot = dev_ggplot2_args
797830
)
798831

799-
if (is.ggplot(plot_main$p)) {
832+
if (is.ggplot(qenv_plot$plot)) {
800833
qenv_plot <- within(qenv_plot, {
801-
p <- p +
834+
plot <- plot +
802835
theme_light() +
803-
labs +
804-
theme
836+
labs
805837
},
806-
labs = do.call("labs", all_ggplot2_args$labs),
807-
theme = do.call("theme", all_ggplot2_args$theme)
838+
labs = do.call("labs", all_ggplot2_args$labs)
808839
)
809840
}
810-
811-
qenv_plot
841+
qenv_plot <- within(qenv_plot, {plot})
812842
}
813843

814844
is_num_var_short <- function(.unique_records_for_factor, input, data_for_analysis) {

0 commit comments

Comments
 (0)