Skip to content

Commit d6e5ea1

Browse files
committed
Fix style and lintr
1 parent 2ca227a commit d6e5ea1

File tree

1 file changed

+155
-133
lines changed

1 file changed

+155
-133
lines changed

R/tm_variable_browser.R

Lines changed: 155 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,6 @@ srv_variable_browser <- function(id,
472472

473473
plot_var_summary(
474474
qenv = req(plotted_data()),
475-
# var = plotted_data()$data,
476-
# var_lab = plotted_data()$var_description,
477475
wrap_character = 15,
478476
numeric_as_factor = treat_numeric_as_factor(),
479477
remove_NA_hist = input$remove_NA_hist,
@@ -639,64 +637,72 @@ plot_var_summary <- function(qenv,
639637

640638
var_name <- names(qenv$ANL)
641639

642-
teal.reporter::teal_card(qenv) <- c(teal.reporter::teal_card(qenv),
643-
teal.reporter::teal_card("### Histogram plot"))
640+
teal.reporter::teal_card(qenv) <- c(
641+
teal.reporter::teal_card(qenv),
642+
teal.reporter::teal_card("### Histogram plot")
643+
)
644644

645645
var <- qenv$ANL[[var_name]]
646646
qenv_plot <- if (is.factor(var) || is.character(var) || is.logical(var)) {
647647
groups <- unique(as.character(var))
648648
len_groups <- length(groups)
649649
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
650+
qenv_plot <- within(qenv,
651+
{
652+
groups <- unique(as.character(ANL[[var]]))
653+
len_groups <- length(groups)
654+
text <- sprintf(
655+
"%s unique values\n%s:\n %s\n ...\n %s",
656+
len_groups,
657+
teal.data::col_labels(ANL),
658+
paste(utils::head(groups), collapse = ",\n "),
659+
paste(utils::tail(groups), collapse = ",\n ")
660+
)
661+
plot <- gridExtra::arrangeGrob(
662+
grid::textGrob(
663+
text,
664+
x = grid::unit(1, "line"),
665+
y = grid::unit(1, "npc") - grid::unit(1, "line"),
666+
just = c("left", "top")
667+
),
668+
ncol = 1
669+
)
670+
},
671+
var = var_name
671672
)
672673
} else {
673674
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)
675+
qenv <- within(qenv,
676+
{
677+
col_label <- attr(ANL[[var]], "label")
678+
ANL[[var]] <- stringr::str_wrap(ANL[[var]], width = wrap_character)
679+
attr(ANL[[var]], "label") <- col_label
680+
},
681+
var = var_name,
682+
wrap_character = wrap_character
683+
)
681684
}
682685

683686
if (isTRUE(remove_NA_hist)) {
684-
qenv <- within(qenv, {
685-
ANL <- filter(ANL, !is.na(var))
686-
},
687-
var = as.name(var_name))
688-
687+
qenv <- within(qenv,
688+
{
689+
ANL <- filter(ANL, !is.na(var))
690+
},
691+
var = as.name(var_name)
692+
)
689693
}
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))
694+
qenv_plot <- within(qenv,
695+
{
696+
plot <- ANL %>%
697+
ggplot2::ggplot(ggplot2::aes(x = forcats::fct_infreq(var_name))) +
698+
ggplot2::geom_bar(
699+
stat = "count", ggplot2::aes(fill = ifelse(is.na(var_name), "withcolor", "")), show.legend = FALSE
700+
) +
701+
ggplot2::scale_fill_manual(values = c("gray50", "tan"))
702+
},
703+
var = var_name,
704+
var_name = as.name(var_name)
705+
)
700706
}
701707
} else if (is.numeric(var)) {
702708
# Validate input
@@ -706,16 +712,18 @@ plot_var_summary <- function(qenv,
706712

707713
if (numeric_as_factor) {
708714
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))
715+
qenv_plot <- within(qenv,
716+
{
717+
col_label <- attr(ANL[[var]], "label")
718+
ANL[[var]] <- as.factor(ANL[[var]])
719+
attr(ANL[[var]], "label") <- col_label
720+
p <- ANL %>%
721+
ggplot2::ggplot(ggplot2::aes(x = var_name)) +
722+
ggplot2::geom_histogram(stat = "count")
723+
},
724+
var = var_name,
725+
var_name = as.name(var_name)
726+
)
719727
} else {
720728
# remove outliers
721729
if (outlier_definition != 0) {
@@ -731,106 +739,119 @@ plot_var_summary <- function(qenv,
731739
length(var) > 1,
732740
"At least two data points must remain after removing outliers for this graph to be displayed"
733741
))
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)
742+
qenv <- within(qenv,
743+
{
744+
remove_outliers <- remove_outliers_from
745+
ANL <- filter(ANL, remove_outliers(var_name, outlier_definition))
746+
},
747+
remove_outliers_from = filter_outliers,
748+
var_name = as.name(var_name),
749+
outlier_definition = outlier_definition
750+
)
741751
}
742752

743753
## histogram
744754
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 %)"
755+
qenv_plot <- within(qenv,
756+
{
757+
plot <- ggplot2::ggplot(data = ANL, ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
758+
ggplot2::geom_histogram(binwidth = binwidth) +
759+
ggplot2::scale_y_continuous(
760+
sec.axis = ggplot2::sec_axis(
761+
trans = ~ . / nrow(ANL),
762+
labels = scales::percent,
763+
name = "proportion (in %)"
764+
)
753765
)
754-
)
755-
},
756-
var_name = as.name(var_name),
757-
binwidth = binwidth)
766+
},
767+
var_name = as.name(var_name),
768+
binwidth = binwidth
769+
)
758770

759771
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)
772+
qenv_plot <- within(qenv_plot,
773+
{
774+
plot <- plot + ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(count * binwidth)))
775+
},
776+
binwidth = binwidth
777+
)
764778
}
765779
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-
780+
qenv_plot <- within(qenv_plot,
781+
{
782+
plot <- plot + ggplot2::annotate(
783+
geom = "text",
784+
label = outlier_text,
785+
x = Inf, y = Inf,
786+
hjust = 1.02, vjust = 1.2,
787+
color = "black",
788+
# explicitly modify geom text size according
789+
size = size
790+
)
791+
},
792+
outlier_text = outlier_text,
793+
size = ggplot2_args[["theme"]][["text"]][["size"]] / 3.5
794+
)
780795
}
781796
qenv_plot
782797
}
783798
qenv_plot
784799
} else if (inherits(var, "Date") || inherits(var, "POSIXct") || inherits(var, "POSIXlt")) {
785800
var_num <- as.numeric(var)
786801
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))
802+
qenv_plot <- within(qenv,
803+
{
804+
col_label <- attr(ANL[[var]], "label")
805+
ANL[[var]] <- as.numeric(ANL[[var]])
806+
attr(ANL[[var]], "label") <- col_label
807+
plot <- ANL %>%
808+
ggplot2::ggplot(ggplot2::aes(x = var_name, y = ggplot2::after_stat(count))) +
809+
ggplot2::geom_histogram(binwidth = binwidth)
810+
},
811+
binwidth = binwidth,
812+
var = var_name,
813+
var_name = as.name(var_name)
814+
)
798815
} 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
816+
qenv_plot <- within(qenv,
817+
{
818+
plot <- gridExtra::arrangeGrob(
819+
grid::textGrob(
820+
paste(strwrap(
821+
utils::capture.output(utils::str(ANL[[var]])),
822+
width = .9 * grid::convertWidth(grid::unit(1, "npc"), "char", TRUE)
823+
), collapse = "\n"),
824+
x = grid::unit(1, "line"), y = grid::unit(1, "npc") - grid::unit(1, "line"), just = c("left", "top")
825+
),
826+
ncol = 1
827+
)
828+
},
829+
var = var_name
812830
)
813831
}
814832

815833
dev_ggplot2_args <- teal.widgets::ggplot2_args(
816834
labs = list(x = teal.data::col_labels(qenv$ANL))
817835
)
818-
###
836+
819837
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args(
820838
ggplot2_args,
821839
module_plot = dev_ggplot2_args
822840
)
823841

824842
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)
843+
qenv_plot <- within(qenv_plot,
844+
{
845+
plot <- plot +
846+
theme_light() +
847+
labs
848+
},
849+
labs = do.call("labs", all_ggplot2_args$labs)
831850
)
832851
}
833-
qenv_plot <- within(qenv_plot, {plot})
852+
qenv_plot <- within(qenv_plot, {
853+
plot
854+
})
834855
}
835856

836857
is_num_var_short <- function(.unique_records_for_factor, input, data_for_analysis) {
@@ -869,13 +890,14 @@ get_plotted_data <- function(input, plot_var, data) {
869890
teal.reporter::teal_card(obj),
870891
teal.reporter::teal_card("## Module's output(s)")
871892
)
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)
893+
within(obj,
894+
{
895+
library(dplyr)
896+
library(ggplot2)
897+
ANL <- select(dataset_name, varname)
898+
},
899+
dataset_name = as.name(dataset_name),
900+
varname = as.name(varname)
879901
)
880902
}
881903

0 commit comments

Comments
 (0)