Skip to content

Commit 890d1c6

Browse files
[skip style] [skip vbump] Restyle files
1 parent 2ca227a commit 890d1c6

File tree

1 file changed

+154
-130
lines changed

1 file changed

+154
-130
lines changed

R/tm_variable_browser.R

Lines changed: 154 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -639,64 +639,72 @@ plot_var_summary <- function(qenv,
639639

640640
var_name <- names(qenv$ANL)
641641

642-
teal.reporter::teal_card(qenv) <- c(teal.reporter::teal_card(qenv),
643-
teal.reporter::teal_card("### Histogram plot"))
642+
teal.reporter::teal_card(qenv) <- c(
643+
teal.reporter::teal_card(qenv),
644+
teal.reporter::teal_card("### Histogram plot")
645+
)
644646

645647
var <- qenv$ANL[[var_name]]
646648
qenv_plot <- if (is.factor(var) || is.character(var) || is.logical(var)) {
647649
groups <- unique(as.character(var))
648650
len_groups <- length(groups)
649651
if (len_groups >= records_for_factor) {
650-
qenv_plot <- within(qenv, {
651-
groups <- unique(as.character(ANL[[var]]))
652-
len_groups <- length(groups)
653-
text <- sprintf(
654-
"%s unique values\n%s:\n %s\n ...\n %s",
655-
len_groups,
656-
teal.data::col_labels(ANL),
657-
paste(utils::head(groups), collapse = ",\n "),
658-
paste(utils::tail(groups), collapse = ",\n ")
659-
)
660-
plot <- gridExtra::arrangeGrob(
661-
grid::textGrob(
662-
text,
663-
x = grid::unit(1, "line"),
664-
y = grid::unit(1, "npc") - grid::unit(1, "line"),
665-
just = c("left", "top")
666-
),
667-
ncol = 1
668-
)
669-
},
670-
var = var_name
652+
qenv_plot <- within(qenv,
653+
{
654+
groups <- unique(as.character(ANL[[var]]))
655+
len_groups <- length(groups)
656+
text <- sprintf(
657+
"%s unique values\n%s:\n %s\n ...\n %s",
658+
len_groups,
659+
teal.data::col_labels(ANL),
660+
paste(utils::head(groups), collapse = ",\n "),
661+
paste(utils::tail(groups), collapse = ",\n ")
662+
)
663+
plot <- gridExtra::arrangeGrob(
664+
grid::textGrob(
665+
text,
666+
x = grid::unit(1, "line"),
667+
y = grid::unit(1, "npc") - grid::unit(1, "line"),
668+
just = c("left", "top")
669+
),
670+
ncol = 1
671+
)
672+
},
673+
var = var_name
671674
)
672675
} else {
673676
if (!is.null(wrap_character)) {
674-
qenv <- within(qenv, {
675-
col_label <- attr(ANL[[var]], "label")
676-
ANL[[var]] <- stringr::str_wrap(ANL[[var]], width = wrap_character)
677-
attr(ANL[[var]], "label") <- col_label
678-
},
679-
var = var_name,
680-
wrap_character = wrap_character)
677+
qenv <- within(qenv,
678+
{
679+
col_label <- attr(ANL[[var]], "label")
680+
ANL[[var]] <- stringr::str_wrap(ANL[[var]], width = wrap_character)
681+
attr(ANL[[var]], "label") <- col_label
682+
},
683+
var = var_name,
684+
wrap_character = wrap_character
685+
)
681686
}
682687

683688
if (isTRUE(remove_NA_hist)) {
684-
qenv <- within(qenv, {
685-
ANL <- filter(ANL, !is.na(var))
686-
},
687-
var = as.name(var_name))
688-
689+
qenv <- within(qenv,
690+
{
691+
ANL <- filter(ANL, !is.na(var))
692+
},
693+
var = as.name(var_name)
694+
)
689695
}
690-
qenv_plot <- within(qenv, {
691-
plot <- ANL %>%
692-
ggplot2::ggplot(ggplot2::aes(x = forcats::fct_infreq(var_name))) +
693-
ggplot2::geom_bar(
694-
stat = "count", ggplot2::aes(fill = ifelse(is.na(var_name), "withcolor", "")), show.legend = FALSE
695-
) +
696-
ggplot2::scale_fill_manual(values = c("gray50", "tan"))
697-
},
698-
var = var_name,
699-
var_name = as.name(var_name))
696+
qenv_plot <- within(qenv,
697+
{
698+
plot <- ANL %>%
699+
ggplot2::ggplot(ggplot2::aes(x = forcats::fct_infreq(var_name))) +
700+
ggplot2::geom_bar(
701+
stat = "count", ggplot2::aes(fill = ifelse(is.na(var_name), "withcolor", "")), show.legend = FALSE
702+
) +
703+
ggplot2::scale_fill_manual(values = c("gray50", "tan"))
704+
},
705+
var = var_name,
706+
var_name = as.name(var_name)
707+
)
700708
}
701709
} else if (is.numeric(var)) {
702710
# Validate input
@@ -706,16 +714,18 @@ plot_var_summary <- function(qenv,
706714

707715
if (numeric_as_factor) {
708716
var <- factor(var)
709-
qenv_plot <- within(qenv, {
710-
col_label <- attr(ANL[[var]], "label")
711-
ANL[[var]] <- as.factor(ANL[[var]])
712-
attr(ANL[[var]], "label") <- col_label
713-
p <- ANL %>%
714-
ggplot2::ggplot(ggplot2::aes(x = var_name)) +
715-
ggplot2::geom_histogram(stat = "count")
716-
},
717-
var = var_name,
718-
var_name = as.name(var_name))
717+
qenv_plot <- within(qenv,
718+
{
719+
col_label <- attr(ANL[[var]], "label")
720+
ANL[[var]] <- as.factor(ANL[[var]])
721+
attr(ANL[[var]], "label") <- col_label
722+
p <- ANL %>%
723+
ggplot2::ggplot(ggplot2::aes(x = var_name)) +
724+
ggplot2::geom_histogram(stat = "count")
725+
},
726+
var = var_name,
727+
var_name = as.name(var_name)
728+
)
719729
} else {
720730
# remove outliers
721731
if (outlier_definition != 0) {
@@ -731,84 +741,94 @@ plot_var_summary <- function(qenv,
731741
length(var) > 1,
732742
"At least two data points must remain after removing outliers for this graph to be displayed"
733743
))
734-
qenv <- within(qenv, {
735-
remove_outliers <- remove_outliers_from
736-
ANL <- filter(ANL, remove_outliers(var_name, outlier_definition))
737-
},
738-
remove_outliers_from = filter_outliers,
739-
var_name = as.name(var_name),
740-
outlier_definition = outlier_definition)
744+
qenv <- within(qenv,
745+
{
746+
remove_outliers <- remove_outliers_from
747+
ANL <- filter(ANL, remove_outliers(var_name, outlier_definition))
748+
},
749+
remove_outliers_from = filter_outliers,
750+
var_name = as.name(var_name),
751+
outlier_definition = outlier_definition
752+
)
741753
}
742754

743755
## histogram
744756
binwidth <- get_bin_width(var)
745-
qenv_plot <- within(qenv, {
746-
plot <- ggplot2::ggplot(data = ANL, ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
747-
ggplot2::geom_histogram(binwidth = binwidth) +
748-
ggplot2::scale_y_continuous(
749-
sec.axis = ggplot2::sec_axis(
750-
trans = ~ . / nrow(ANL),
751-
labels = scales::percent,
752-
name = "proportion (in %)"
757+
qenv_plot <- within(qenv,
758+
{
759+
plot <- ggplot2::ggplot(data = ANL, ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
760+
ggplot2::geom_histogram(binwidth = binwidth) +
761+
ggplot2::scale_y_continuous(
762+
sec.axis = ggplot2::sec_axis(
763+
trans = ~ . / nrow(ANL),
764+
labels = scales::percent,
765+
name = "proportion (in %)"
766+
)
753767
)
754-
)
755-
},
756-
var_name = as.name(var_name),
757-
binwidth = binwidth)
768+
},
769+
var_name = as.name(var_name),
770+
binwidth = binwidth
771+
)
758772

759773
if (display_density) {
760-
qenv_plot <- within(qenv_plot, {
761-
plot <- plot + ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(count * binwidth)))
762-
},
763-
binwidth = binwidth)
774+
qenv_plot <- within(qenv_plot,
775+
{
776+
plot <- plot + ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(count * binwidth)))
777+
},
778+
binwidth = binwidth
779+
)
764780
}
765781
if (outlier_definition != 0) {
766-
qenv_plot <- within(qenv_plot, {
767-
plot <- plot + ggplot2::annotate(
768-
geom = "text",
769-
label = outlier_text,
770-
x = Inf, y = Inf,
771-
hjust = 1.02, vjust = 1.2,
772-
color = "black",
773-
# explicitly modify geom text size according
774-
size = size
775-
)
776-
},
777-
outlier_text = outlier_text,
778-
size = ggplot2_args[["theme"]][["text"]][["size"]] / 3.5)
779-
782+
qenv_plot <- within(qenv_plot,
783+
{
784+
plot <- plot + ggplot2::annotate(
785+
geom = "text",
786+
label = outlier_text,
787+
x = Inf, y = Inf,
788+
hjust = 1.02, vjust = 1.2,
789+
color = "black",
790+
# explicitly modify geom text size according
791+
size = size
792+
)
793+
},
794+
outlier_text = outlier_text,
795+
size = ggplot2_args[["theme"]][["text"]][["size"]] / 3.5
796+
)
780797
}
781798
qenv_plot
782799
}
783800
qenv_plot
784801
} else if (inherits(var, "Date") || inherits(var, "POSIXct") || inherits(var, "POSIXlt")) {
785802
var_num <- as.numeric(var)
786803
binwidth <- get_bin_width(var_num, 1)
787-
qenv_plot <- within(qenv, {
788-
col_label <- attr(ANL[[var]], "label")
789-
ANL[[var]] <- as.numeric(ANL[[var]])
790-
attr(ANL[[var]], "label") <- col_label
791-
plot <- ANL %>%
792-
ggplot2::ggplot(ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
793-
ggplot2::geom_histogram(binwidth = binwidth)
794-
},
795-
binwidth = binwidth,
796-
var = var_name,
797-
var_name = as.name(var_name))
804+
qenv_plot <- within(qenv,
805+
{
806+
col_label <- attr(ANL[[var]], "label")
807+
ANL[[var]] <- as.numeric(ANL[[var]])
808+
attr(ANL[[var]], "label") <- col_label
809+
plot <- ANL %>%
810+
ggplot2::ggplot(ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
811+
ggplot2::geom_histogram(binwidth = binwidth)
812+
},
813+
binwidth = binwidth,
814+
var = var_name,
815+
var_name = as.name(var_name)
816+
)
798817
} else {
799-
qenv_plot <- within(qenv, {
800-
plot <- gridExtra::arrangeGrob(
801-
grid::textGrob(
802-
paste(strwrap(
803-
utils::capture.output(utils::str(ANL[[var]])),
804-
width = .9 * grid::convertWidth(grid::unit(1, "npc"), "char", TRUE)
805-
), collapse = "\n"),
806-
x = grid::unit(1, "line"), y = grid::unit(1, "npc") - grid::unit(1, "line"), just = c("left", "top")
807-
),
808-
ncol = 1
809-
)
810-
},
811-
var = var_name
818+
qenv_plot <- within(qenv,
819+
{
820+
plot <- gridExtra::arrangeGrob(
821+
grid::textGrob(
822+
paste(strwrap(
823+
utils::capture.output(utils::str(ANL[[var]])),
824+
width = .9 * grid::convertWidth(grid::unit(1, "npc"), "char", TRUE)
825+
), collapse = "\n"),
826+
x = grid::unit(1, "line"), y = grid::unit(1, "npc") - grid::unit(1, "line"), just = c("left", "top")
827+
),
828+
ncol = 1
829+
)
830+
},
831+
var = var_name
812832
)
813833
}
814834

@@ -822,15 +842,18 @@ plot_var_summary <- function(qenv,
822842
)
823843

824844
if (is.ggplot(qenv_plot$plot)) {
825-
qenv_plot <- within(qenv_plot, {
826-
plot <- plot +
827-
theme_light() +
828-
labs
829-
},
830-
labs = do.call("labs", all_ggplot2_args$labs)
845+
qenv_plot <- within(qenv_plot,
846+
{
847+
plot <- plot +
848+
theme_light() +
849+
labs
850+
},
851+
labs = do.call("labs", all_ggplot2_args$labs)
831852
)
832853
}
833-
qenv_plot <- within(qenv_plot, {plot})
854+
qenv_plot <- within(qenv_plot, {
855+
plot
856+
})
834857
}
835858

836859
is_num_var_short <- function(.unique_records_for_factor, input, data_for_analysis) {
@@ -869,13 +892,14 @@ get_plotted_data <- function(input, plot_var, data) {
869892
teal.reporter::teal_card(obj),
870893
teal.reporter::teal_card("## Module's output(s)")
871894
)
872-
within(obj, {
873-
library(dplyr)
874-
library(ggplot2)
875-
ANL <- select(dataset_name, varname)
876-
},
877-
dataset_name = as.name(dataset_name),
878-
varname = as.name(varname)
895+
within(obj,
896+
{
897+
library(dplyr)
898+
library(ggplot2)
899+
ANL <- select(dataset_name, varname)
900+
},
901+
dataset_name = as.name(dataset_name),
902+
varname = as.name(varname)
879903
)
880904
}
881905

0 commit comments

Comments
 (0)