Commit 0dec47dc authored by wuzekai's avatar wuzekai

修改了部分功能的描述

parent 8427b521
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
r_url_list <- getOption("radiant.url.list") r_url_list <- getOption("radiant.url.list")
r_url_list[["Single mean"]] <- r_url_list[["Single mean"]] <-
list("tabs_single_mean" = list("Summary" = "basics/single-mean/", "Plot" = "basics/single-mean/plot/")) list("tabs_single_mean" = list("Summary" = "basics/single-mean/", "Plot" = "basics/single-mean/plot/"))
r_url_list[["Compare means"]] <- r_url_list[["Compare means(t-test/Wilcoxon rank-sum test)"]] <-
list("tabs_compare_means" = list("Summary" = "basics/compare-means/", "Plot" = "basics/compare-means/plot/")) list("tabs_compare_means" = list("Summary" = "basics/compare-means/", "Plot" = "basics/compare-means/plot/"))
r_url_list[["Single proportion"]] <- r_url_list[["Single proportion"]] <-
list("tabs_single_prop" = list("Summary" = "basics/single-prop/", "Plot" = "basics/single-prop/plot/")) list("tabs_single_prop" = list("Summary" = "basics/single-prop/", "Plot" = "basics/single-prop/plot/"))
...@@ -10,7 +10,7 @@ r_url_list[["Compare proportions"]] <- ...@@ -10,7 +10,7 @@ r_url_list[["Compare proportions"]] <-
list("tabs_compare_props" = list("Summary" = "basics/compare-props/", "Plot" = "basics/compare-props/plot/")) list("tabs_compare_props" = list("Summary" = "basics/compare-props/", "Plot" = "basics/compare-props/plot/"))
r_url_list[["Goodness of fit"]] <- r_url_list[["Goodness of fit"]] <-
list("tabs_goodness" = list("Summary" = "basics/goodness/", "Plot" = "basics/goodness/plot/")) list("tabs_goodness" = list("Summary" = "basics/goodness/", "Plot" = "basics/goodness/plot/"))
r_url_list[["Cross-tabs"]] <- r_url_list[["Cross-tabs(Chi-square test, etc)"]] <-
list("tabs_cross_tabs" = list("Summary" = "basics/cross-tabs/", "Plot" = "basics/cross-tabs/plot/")) list("tabs_cross_tabs" = list("Summary" = "basics/cross-tabs/", "Plot" = "basics/cross-tabs/plot/"))
r_url_list[["Correlation"]] <- r_url_list[["Correlation"]] <-
list("tabs_correlation" = list("Summary" = "basics/correlation/", "Plot" = "basics`/correlation/plot/")) list("tabs_correlation" = list("Summary" = "basics/correlation/", "Plot" = "basics`/correlation/plot/"))
...@@ -34,7 +34,7 @@ options( ...@@ -34,7 +34,7 @@ options(
tabPanel(i18n$t("Central Limit Theorem"), uiOutput("clt")), tabPanel(i18n$t("Central Limit Theorem"), uiOutput("clt")),
"----", i18n$t("Means"), "----", i18n$t("Means"),
tabPanel(i18n$t("Single mean"), uiOutput("single_mean")), tabPanel(i18n$t("Single mean"), uiOutput("single_mean")),
tabPanel(i18n$t("Compare means"), uiOutput("compare_means")), tabPanel(i18n$t("Compare means(t-test/Wilcoxon rank-sum test)"), uiOutput("compare_means")),
tabPanel(i18n$t("Normality test"),uiOutput("normality_test")), tabPanel(i18n$t("Normality test"),uiOutput("normality_test")),
tabPanel(i18n$t("Homogeneity of variance test"),uiOutput("homo_variance_test")), tabPanel(i18n$t("Homogeneity of variance test"),uiOutput("homo_variance_test")),
"----", i18n$t("Proportions"), "----", i18n$t("Proportions"),
...@@ -42,7 +42,7 @@ options( ...@@ -42,7 +42,7 @@ options(
tabPanel(i18n$t("Compare proportions"), uiOutput("compare_props")), tabPanel(i18n$t("Compare proportions"), uiOutput("compare_props")),
"----", i18n$t("Tables"), "----", i18n$t("Tables"),
tabPanel(i18n$t("Goodness of fit"), uiOutput("goodness")), tabPanel(i18n$t("Goodness of fit"), uiOutput("goodness")),
tabPanel(i18n$t("Cross-tabs"), uiOutput("cross_tabs")), tabPanel(i18n$t("Cross-tabs(Chi-square test, etc)"), uiOutput("cross_tabs")),
tabPanel(i18n$t("Correlation"), uiOutput("correlation")) tabPanel(i18n$t("Correlation"), uiOutput("correlation"))
) )
) )
......
## choice lists for compare means ## choice lists for compare means
cm_alt <- c( cm_alt <- c(
"two.sided", "two.sided",
"less", "less",
"greater" "greater"
) %>% setNames(c( ) %>% setNames(c(
i18n$t("Two sided"), i18n$t("Two sided"),
i18n$t("Less than"), i18n$t("Less than"),
i18n$t("Greater than") i18n$t("Greater than")
)) ))
cm_samples <- c( cm_samples <- c(
"independent", "independent",
"paired" "paired"
) %>% setNames(c( ) %>% setNames(c(
i18n$t("independent"), i18n$t("independent"),
i18n$t("paired") i18n$t("paired")
)) ))
cm_adjust <- c( cm_adjust <- c(
"none", "none",
"bonf" "bonf"
) %>% setNames(c( ) %>% setNames(c(
i18n$t("None"), i18n$t("None"),
i18n$t("Bonferroni") i18n$t("Bonferroni")
)) ))
cm_plots <- c( cm_plots <- c(
"scatter", "scatter",
"box", "box",
"density", "density",
"bar" "bar"
) %>% setNames(c( ) %>% setNames(c(
i18n$t("Scatter"), i18n$t("Scatter"),
i18n$t("Box"), i18n$t("Box"),
i18n$t("Density"), i18n$t("Density"),
i18n$t("Bar") i18n$t("Bar")
)) ))
## list of function arguments ## list of function arguments
cm_args <- as.list(formals(compare_means)) cm_args <- as.list(formals(compare_means))
## list of function inputs selected by user ## list of function inputs selected by user
cm_inputs <- reactive({ cm_inputs <- reactive({
## loop needed because reactive values don't allow single bracket indexing ## loop needed because reactive values don't allow single bracket indexing
cm_args$data_filter <- if (input$show_filter) input$data_filter else "" cm_args$data_filter <- if (input$show_filter) input$data_filter else ""
cm_args$dataset <- input$dataset cm_args$dataset <- input$dataset
for (i in r_drop(names(cm_args))) { for (i in r_drop(names(cm_args))) {
cm_args[[i]] <- input[[paste0("cm_", i)]] cm_args[[i]] <- input[[paste0("cm_", i)]]
} }
cm_args cm_args
}) })
############################### ###############################
# Compare means # Compare means
############################### ###############################
output$ui_cm_var1 <- renderUI({ output$ui_cm_var1 <- renderUI({
vars <- c("None" = "", groupable_vars()) vars <- c("None" = "", groupable_vars())
isNum <- .get_class() %in% c("integer", "numeric", "ts") isNum <- .get_class() %in% c("integer", "numeric", "ts")
## can't use unique here - removes variable type information ## can't use unique here - removes variable type information
vars <- c(vars, varnames()[isNum]) %>% .[!duplicated(.)] vars <- c(vars, varnames()[isNum]) %>% .[!duplicated(.)]
selectInput( selectInput(
inputId = "cm_var1", inputId = "cm_var1",
label = i18n$t("Select a factor or numeric variable:"), label = i18n$t("Select a factor or numeric variable:"),
choices = vars, choices = vars,
selected = state_single("cm_var1", vars), selected = state_single("cm_var1", vars),
multiple = FALSE multiple = FALSE
) )
}) })
output$ui_cm_var2 <- renderUI({ output$ui_cm_var2 <- renderUI({
if (not_available(input$cm_var1)) { if (not_available(input$cm_var1)) {
return() return()
} }
isNum <- .get_class() %in% c("integer", "numeric", "ts") isNum <- .get_class() %in% c("integer", "numeric", "ts")
vars <- varnames()[isNum] vars <- varnames()[isNum]
if (input$cm_var1 %in% vars) { if (input$cm_var1 %in% vars) {
## when cm_var1 is numeric comparisons for multiple variables are possible ## when cm_var1 is numeric comparisons for multiple variables are possible
vars <- vars[-which(vars == input$cm_var1)] vars <- vars[-which(vars == input$cm_var1)]
if (length(vars) == 0) { if (length(vars) == 0) {
return() return()
} }
selectizeInput( selectizeInput(
inputId = "cm_var2", label = i18n$t("Numeric variable(s):"), inputId = "cm_var2", label = i18n$t("Numeric variable(s):"),
selected = state_multiple("cm_var2", vars, isolate(input$cm_var2)), selected = state_multiple("cm_var2", vars, isolate(input$cm_var2)),
choices = vars, multiple = TRUE, choices = vars, multiple = TRUE,
options = list(placeholder = "None", plugins = list("remove_button", "drag_drop")) options = list(placeholder = "None", plugins = list("remove_button", "drag_drop"))
) )
} else { } else {
## when cm_var1 is not numeric comparisons are across levels/groups ## when cm_var1 is not numeric comparisons are across levels/groups
vars <- c("None" = "", vars) vars <- c("None" = "", vars)
selectInput( selectInput(
"cm_var2", i18n$t("Numeric variable:"), "cm_var2", i18n$t("Numeric variable:"),
selected = state_single("cm_var2", vars), selected = state_single("cm_var2", vars),
choices = vars, choices = vars,
multiple = FALSE multiple = FALSE
) )
} }
}) })
output$ui_cm_comb <- renderUI({ output$ui_cm_comb <- renderUI({
if (not_available(input$cm_var1)) { if (not_available(input$cm_var1)) {
return() return()
} }
if (.get_class()[[input$cm_var1]] == "factor") { if (.get_class()[[input$cm_var1]] == "factor") {
levs <- .get_data()[[input$cm_var1]] %>% levels() levs <- .get_data()[[input$cm_var1]] %>% levels()
} else { } else {
levs <- c(input$cm_var1, input$cm_var2) levs <- c(input$cm_var1, input$cm_var2)
} }
if (length(levs) > 2) { if (length(levs) > 2) {
cmb <- combn(levs, 2) %>% apply(2, paste, collapse = ":") cmb <- combn(levs, 2) %>% apply(2, paste, collapse = ":")
} else { } else {
return() return()
} }
selectizeInput( selectizeInput(
"cm_comb", "cm_comb",
label = i18n$t("Choose combinations:"), label = i18n$t("Choose combinations:"),
choices = cmb, choices = cmb,
selected = state_multiple("cm_comb", cmb, cmb[1]), selected = state_multiple("cm_comb", cmb, cmb[1]),
multiple = TRUE, multiple = TRUE,
options = list(placeholder = i18n$t("Evaluate all combinations"), plugins = list("remove_button", "drag_drop")) options = list(placeholder = i18n$t("Evaluate all combinations"), plugins = list("remove_button", "drag_drop"))
) )
}) })
output$ui_compare_means <- renderUI({ output$ui_compare_means <- renderUI({
req(input$dataset) req(input$dataset)
tagList( tagList(
wellPanel( wellPanel(
conditionalPanel( conditionalPanel(
uiOutput("ui_cm_var1"), uiOutput("ui_cm_var1"),
uiOutput("ui_cm_var2"), uiOutput("ui_cm_var2"),
condition = "input.tabs_compare_means == 'Summary'", condition = "input.tabs_compare_means == 'Summary'",
uiOutput("ui_cm_comb"), uiOutput("ui_cm_comb"),
selectInput( selectInput(
inputId = "cm_alternative", label = i18n$t("Alternative hypothesis:"), inputId = "cm_alternative", label = i18n$t("Alternative hypothesis:"),
choices = cm_alt, choices = cm_alt,
selected = state_single("cm_alternative", cm_alt, cm_args$alternative) selected = state_single("cm_alternative", cm_alt, cm_args$alternative)
), ),
sliderInput( sliderInput(
"cm_conf_lev", i18n$t("Confidence level:"), "cm_conf_lev", i18n$t("Confidence level:"),
min = 0.85, max = 0.99, step = 0.01, min = 0.85, max = 0.99, step = 0.01,
value = state_init("cm_conf_lev", cm_args$conf_lev) value = state_init("cm_conf_lev", cm_args$conf_lev)
), ),
checkboxInput("cm_show", i18n$t("Show additional statistics"), value = state_init("cm_show", FALSE)), checkboxInput("cm_show", i18n$t("Show additional statistics"), value = state_init("cm_show", FALSE)),
radioButtons( radioButtons(
inputId = "cm_samples", label = i18n$t("Sample type:"), cm_samples, inputId = "cm_samples", label = i18n$t("Sample type:"), cm_samples,
selected = state_init("cm_samples", cm_args$samples), selected = state_init("cm_samples", cm_args$samples),
inline = TRUE inline = TRUE
), ),
radioButtons( radioButtons(
inputId = "cm_adjust", label = i18n$t("Multiple comp. adjustment:"), cm_adjust, inputId = "cm_adjust", label = i18n$t("Multiple comp. adjustment:"), cm_adjust,
selected = state_init("cm_adjust", cm_args$adjust), selected = state_init("cm_adjust", cm_args$adjust),
inline = TRUE inline = TRUE
), ),
radioButtons( radioButtons(
inputId = "cm_test", label = i18n$t("Test type:"), inputId = "cm_test", label = i18n$t("Test type:"),
choices = c( choices = c(
"t", "t",
"wilcox" "wilcox"
) %>% setNames(c( ) %>% setNames(c(
i18n$t("t-test"), i18n$t("t-test"),
i18n$t("Wilcox") i18n$t("Wilcox")
)), )),
selected = state_init("cm_test", cm_args$test), selected = state_init("cm_test", cm_args$test),
inline = TRUE inline = TRUE
) )
), ),
conditionalPanel( conditionalPanel(
condition = "input.tabs_compare_means == 'Plot'", condition = "input.tabs_compare_means == 'Plot'",
selectizeInput( selectizeInput(
inputId = "cm_plots", label = i18n$t("Select plots:"), inputId = "cm_plots", label = i18n$t("Select plots:"),
choices = cm_plots, choices = cm_plots,
selected = state_multiple("cm_plots", cm_plots, "scatter"), selected = state_multiple("cm_plots", cm_plots, "scatter"),
multiple = TRUE, multiple = TRUE,
options = list(placeholder = i18n$t("Select plots"), plugins = list("remove_button", "drag_drop")) options = list(placeholder = i18n$t("Select plots"), plugins = list("remove_button", "drag_drop"))
) )
) )
), ),
help_and_report( help_and_report(
modal_title = i18n$t("Compare means"), modal_title = i18n$t("Compare means"),
fun_name = "compare_means", fun_name = "compare_means",
help_file = inclMD(file.path(getOption("radiant.path.basics"), "app/tools/help/compare_means.md")) help_file = inclMD(file.path(getOption("radiant.path.basics"), "app/tools/help/compare_means.md"))
) )
) )
}) })
cm_plot <- reactive({ cm_plot <- reactive({
list(plot_width = 650, plot_height = 400 * max(length(input$cm_plots), 1)) list(plot_width = 650, plot_height = 400 * max(length(input$cm_plots), 1))
}) })
cm_plot_width <- function() { cm_plot_width <- function() {
cm_plot() %>% cm_plot() %>%
(function(x) if (is.list(x)) x$plot_width else 650) (function(x) if (is.list(x)) x$plot_width else 650)
} }
cm_plot_height <- function() { cm_plot_height <- function() {
cm_plot() %>% cm_plot() %>%
(function(x) if (is.list(x)) x$plot_height else 400) (function(x) if (is.list(x)) x$plot_height else 400)
} }
# output is called from the main radiant ui.R # output is called from the main radiant ui.R
output$compare_means <- renderUI({ output$compare_means <- renderUI({
register_print_output("summary_compare_means", ".summary_compare_means", ) register_print_output("summary_compare_means", ".summary_compare_means", )
register_plot_output( register_plot_output(
"plot_compare_means", ".plot_compare_means", "plot_compare_means", ".plot_compare_means",
height_fun = "cm_plot_height" height_fun = "cm_plot_height"
) )
# two separate tabs # two separate tabs
cm_output_panels <- tabsetPanel( cm_output_panels <- tabsetPanel(
id = "tabs_compare_means", id = "tabs_compare_means",
tabPanel(i18n$t("Summary"), value = "Summary", verbatimTextOutput("summary_compare_means")), tabPanel(i18n$t("Summary"), value = "Summary", verbatimTextOutput("summary_compare_means")),
tabPanel( tabPanel(
i18n$t("Plot"), value = "Plot", i18n$t("Plot"), value = "Plot",
download_link("dlp_compare_means"), download_link("dlp_compare_means"),
plotOutput("plot_compare_means", height = "100%") plotOutput("plot_compare_means", height = "100%")
) )
) )
stat_tab_panel( stat_tab_panel(
menu = i18n$t("Basics > Means"), menu = i18n$t("Basics > Means"),
tool = i18n$t("Compare means"), tool = i18n$t("T-test/Wilcoxon rank-sum test"),
tool_ui = "ui_compare_means", tool_ui = "ui_compare_means",
output_panels = cm_output_panels output_panels = cm_output_panels
) )
}) })
cm_available <- reactive({ cm_available <- reactive({
if (not_available(input$cm_var1) || not_available(input$cm_var2)) { if (not_available(input$cm_var1) || not_available(input$cm_var2)) {
return(i18n$t("This analysis requires at least two variables. The first can be of type\nfactor, numeric, or interval. The second must be of type numeric or interval.\nIf these variable types are not available please select another dataset.\n\n") %>% suggest_data("salary")) return(i18n$t("This analysis requires at least two variables. The first can be of type\nfactor, numeric, or interval. The second must be of type numeric or interval.\nIf these variable types are not available please select another dataset.\n\n") %>% suggest_data("salary"))
} else if (length(input$cm_var2) > 1 && .get_class()[input$cm_var1] == "factor") { } else if (length(input$cm_var2) > 1 && .get_class()[input$cm_var1] == "factor") {
" " " "
} else if (input$cm_var1 %in% input$cm_var2) { } else if (input$cm_var1 %in% input$cm_var2) {
" " " "
} else { } else {
"available" "available"
} }
}) })
.compare_means <- reactive({ .compare_means <- reactive({
cmi <- cm_inputs() cmi <- cm_inputs()
cmi$envir <- r_data cmi$envir <- r_data
do.call(compare_means, cmi) do.call(compare_means, cmi)
}) })
.summary_compare_means <- reactive({ .summary_compare_means <- reactive({
if (cm_available() != "available") { if (cm_available() != "available") {
return(cm_available()) return(cm_available())
} }
if (input$cm_show) summary(.compare_means(), show = TRUE) else summary(.compare_means()) if (input$cm_show) summary(.compare_means(), show = TRUE) else summary(.compare_means())
}) })
.plot_compare_means <- reactive({ .plot_compare_means <- reactive({
if (cm_available() != "available") { if (cm_available() != "available") {
return(cm_available()) return(cm_available())
} }
validate(need(input$cm_plots, i18n$t("Nothing to plot. Please select a plot type"))) validate(need(input$cm_plots, i18n$t("Nothing to plot. Please select a plot type")))
withProgress(message = i18n$t("Generating plots"), value = 1, { withProgress(message = i18n$t("Generating plots"), value = 1, {
plot(.compare_means(), plots = input$cm_plots, shiny = TRUE) plot(.compare_means(), plots = input$cm_plots, shiny = TRUE)
}) })
}) })
compare_means_report <- function() { compare_means_report <- function() {
if (is.empty(input$cm_var1) || is.empty(input$cm_var2)) { if (is.empty(input$cm_var1) || is.empty(input$cm_var2)) {
return(invisible()) return(invisible())
} }
figs <- FALSE figs <- FALSE
outputs <- c("summary") outputs <- c("summary")
inp_out <- list(list(show = input$cm_show), "") inp_out <- list(list(show = input$cm_show), "")
if (length(input$cm_plots) > 0) { if (length(input$cm_plots) > 0) {
outputs <- c("summary", "plot") outputs <- c("summary", "plot")
inp_out[[2]] <- list(plots = input$cm_plots, custom = FALSE) inp_out[[2]] <- list(plots = input$cm_plots, custom = FALSE)
figs <- TRUE figs <- TRUE
} }
update_report( update_report(
inp_main = clean_args(cm_inputs(), cm_args), inp_main = clean_args(cm_inputs(), cm_args),
fun_name = "compare_means", fun_name = "compare_means",
inp_out = inp_out, inp_out = inp_out,
outputs = outputs, outputs = outputs,
figs = figs, figs = figs,
fig.width = cm_plot_width(), fig.width = cm_plot_width(),
fig.height = cm_plot_height() fig.height = cm_plot_height()
) )
} }
download_handler( download_handler(
id = "dlp_compare_means", id = "dlp_compare_means",
fun = download_handler_plot, fun = download_handler_plot,
fn = function() paste0(input$dataset, "_compare_means"), fn = function() paste0(input$dataset, "_compare_means"),
type = "png", type = "png",
caption = i18n$t("Save compare means plot"), caption = i18n$t("Save compare means plot"),
plot = .plot_compare_means, plot = .plot_compare_means,
width = cm_plot_width, width = cm_plot_width,
height = cm_plot_height height = cm_plot_height
) )
observeEvent(input$compare_means_report, { observeEvent(input$compare_means_report, {
r_info[["latest_screenshot"]] <- NULL r_info[["latest_screenshot"]] <- NULL
compare_means_report() compare_means_report()
}) })
observeEvent(input$compare_means_screenshot, { observeEvent(input$compare_means_screenshot, {
r_info[["latest_screenshot"]] <- NULL r_info[["latest_screenshot"]] <- NULL
radiant_screenshot_modal("modal_compare_means_screenshot") radiant_screenshot_modal("modal_compare_means_screenshot")
}) })
observeEvent(input$modal_compare_means_screenshot, { observeEvent(input$modal_compare_means_screenshot, {
compare_means_report() compare_means_report()
removeModal() ## remove shiny modal after save removeModal() ## remove shiny modal after save
}) })
## alternative hypothesis options ## alternative hypothesis options
ct_check <- c( ct_check <- c(
"observed", "observed",
"expected", "expected",
"chi_sq", "chi_sq",
"dev_std", "dev_std",
"row_perc", "row_perc",
"col_perc", "col_perc",
"perc" "perc"
) )
names(ct_check) <- c( names(ct_check) <- c(
i18n$t("Observed"), i18n$t("Observed"),
i18n$t("Expected"), i18n$t("Expected"),
i18n$t("Chi-squared"), i18n$t("Chi-squared"),
i18n$t("Deviation std."), i18n$t("Deviation std."),
i18n$t("Row percentages"), i18n$t("Row percentages"),
i18n$t("Column percentages"), i18n$t("Column percentages"),
i18n$t("Table percentages") i18n$t("Table percentages")
) )
## list of function arguments ## list of function arguments
ct_args <- as.list(formals(cross_tabs)) ct_args <- as.list(formals(cross_tabs))
## list of function inputs selected by user ## list of function inputs selected by user
ct_inputs <- reactive({ ct_inputs <- reactive({
## loop needed because reactive values don't allow single bracket indexing ## loop needed because reactive values don't allow single bracket indexing
ct_args$data_filter <- if (input$show_filter) input$data_filter else "" ct_args$data_filter <- if (input$show_filter) input$data_filter else ""
ct_args$dataset <- input$dataset ct_args$dataset <- input$dataset
for (i in r_drop(names(ct_args))) { for (i in r_drop(names(ct_args))) {
ct_args[[i]] <- input[[paste0("ct_", i)]] ct_args[[i]] <- input[[paste0("ct_", i)]]
} }
ct_args ct_args
}) })
############################### ###############################
# Cross-tabs # Cross-tabs
############################### ###############################
output$ui_ct_var1 <- renderUI({ output$ui_ct_var1 <- renderUI({
vars <- c("None" = "", groupable_vars()) vars <- c("None" = "", groupable_vars())
selectInput( selectInput(
inputId = "ct_var1", label = i18n$t("Select a categorical variable:"), inputId = "ct_var1", label = i18n$t("Select a categorical variable:"),
choices = vars, selected = state_single("ct_var1", vars), multiple = FALSE choices = vars, selected = state_single("ct_var1", vars), multiple = FALSE
) )
}) })
output$ui_ct_var2 <- renderUI({ output$ui_ct_var2 <- renderUI({
if (not_available(input$ct_var1)) { if (not_available(input$ct_var1)) {
return() return()
} }
vars <- c("None" = "", groupable_vars()) vars <- c("None" = "", groupable_vars())
if (length(vars) > 0) vars <- vars[-which(vars == input$ct_var1)] if (length(vars) > 0) vars <- vars[-which(vars == input$ct_var1)]
selectInput( selectInput(
inputId = "ct_var2", label = i18n$t("Select a categorical variable:"), inputId = "ct_var2", label = i18n$t("Select a categorical variable:"),
selected = state_single("ct_var2", vars), selected = state_single("ct_var2", vars),
choices = vars, multiple = FALSE choices = vars, multiple = FALSE
) )
}) })
output$ui_cross_tabs <- renderUI({ output$ui_cross_tabs <- renderUI({
req(input$dataset) req(input$dataset)
tagList( tagList(
wellPanel( wellPanel(
conditionalPanel( conditionalPanel(
condition = "input.tabs_cross_tabs == 'Summary'", condition = "input.tabs_cross_tabs == 'Summary'",
uiOutput("ui_ct_var1"), uiOutput("ui_ct_var1"),
uiOutput("ui_ct_var2") uiOutput("ui_ct_var2")
), ),
br(), br(),
checkboxGroupInput( checkboxGroupInput(
"ct_check", NULL, "ct_check", NULL,
choices = ct_check, choices = ct_check,
selected = state_group("ct_check"), selected = state_group("ct_check"),
inline = FALSE inline = FALSE
) )
), ),
help_and_report( help_and_report(
modal_title = i18n$t("Cross-tabs"), modal_title = i18n$t("Cross-tabs"),
fun_name = "cross_tabs", fun_name = "cross_tabs",
help_file = inclMD(file.path(getOption("radiant.path.basics"), "app/tools/help/cross_tabs.md")) help_file = inclMD(file.path(getOption("radiant.path.basics"), "app/tools/help/cross_tabs.md"))
) )
) )
}) })
ct_plot <- reactive({ ct_plot <- reactive({
list(plot_width = 650, plot_height = 400 * max(length(input$ct_check), 1)) list(plot_width = 650, plot_height = 400 * max(length(input$ct_check), 1))
}) })
ct_plot_width <- function() { ct_plot_width <- function() {
ct_plot() %>% ct_plot() %>%
(function(x) if (is.list(x)) x$plot_width else 650) (function(x) if (is.list(x)) x$plot_width else 650)
} }
ct_plot_height <- function() { ct_plot_height <- function() {
ct_plot() %>% ct_plot() %>%
(function(x) if (is.list(x)) x$plot_height else 400) (function(x) if (is.list(x)) x$plot_height else 400)
} }
## output is called from the main radiant ui.R ## output is called from the main radiant ui.R
output$cross_tabs <- renderUI({ output$cross_tabs <- renderUI({
register_print_output("summary_cross_tabs", ".summary_cross_tabs") register_print_output("summary_cross_tabs", ".summary_cross_tabs")
register_plot_output( register_plot_output(
"plot_cross_tabs", ".plot_cross_tabs", "plot_cross_tabs", ".plot_cross_tabs",
height_fun = "ct_plot_height", height_fun = "ct_plot_height",
width_fun = "ct_plot_width" width_fun = "ct_plot_width"
) )
## two separate tabs ## two separate tabs
ct_output_panels <- tabsetPanel( ct_output_panels <- tabsetPanel(
id = "tabs_cross_tabs", id = "tabs_cross_tabs",
tabPanel(i18n$t("Summary"), value = "Summary", verbatimTextOutput("summary_cross_tabs")), tabPanel(i18n$t("Summary"), value = "Summary", verbatimTextOutput("summary_cross_tabs")),
tabPanel( tabPanel(
i18n$t("Plot"), value = "Plot", i18n$t("Plot"), value = "Plot",
download_link("dlp_cross_tabs"), download_link("dlp_cross_tabs"),
plotOutput("plot_cross_tabs", width = "100%", height = "100%") plotOutput("plot_cross_tabs", width = "100%", height = "100%")
) )
) )
stat_tab_panel( stat_tab_panel(
menu = i18n$t("Basics > Tables"), menu = i18n$t("Basics > Tables"),
tool = i18n$t("Cross-tabs"), tool = i18n$t("Cross-tabs(Chi-square test, etc)"),
tool_ui = "ui_cross_tabs", tool_ui = "ui_cross_tabs",
output_panels = ct_output_panels output_panels = ct_output_panels
) )
}) })
ct_available <- reactive({ ct_available <- reactive({
if (not_available(input$ct_var1) || not_available(input$ct_var2)) { if (not_available(input$ct_var1) || not_available(input$ct_var2)) {
i18n$t("This analysis requires two categorical variables. Both must have two or more levels.\nIf these variable types are not available please select another dataset.\n\n") %>% i18n$t("This analysis requires two categorical variables. Both must have two or more levels.\nIf these variable types are not available please select another dataset.\n\n") %>%
suggest_data("newspaper") suggest_data("newspaper")
} else { } else {
"available" "available"
} }
}) })
.cross_tabs <- reactive({ .cross_tabs <- reactive({
cti <- ct_inputs() cti <- ct_inputs()
cti$envir <- r_data cti$envir <- r_data
do.call(cross_tabs, cti) do.call(cross_tabs, cti)
}) })
.summary_cross_tabs <- reactive({ .summary_cross_tabs <- reactive({
if (ct_available() != "available") { if (ct_available() != "available") {
return(ct_available()) return(ct_available())
} }
summary(.cross_tabs(), check = input$ct_check) summary(.cross_tabs(), check = input$ct_check)
}) })
.plot_cross_tabs <- reactive({ .plot_cross_tabs <- reactive({
if (ct_available() != "available") { if (ct_available() != "available") {
return(ct_available()) return(ct_available())
} }
validate(need(input$ct_check, i18n$t("Nothing to plot. Please select a plot type"))) validate(need(input$ct_check, i18n$t("Nothing to plot. Please select a plot type")))
withProgress(message = i18n$t("Generating plots"), value = 1, { withProgress(message = i18n$t("Generating plots"), value = 1, {
plot(.cross_tabs(), check = input$ct_check, shiny = TRUE) plot(.cross_tabs(), check = input$ct_check, shiny = TRUE)
}) })
}) })
cross_tabs_report <- function() { cross_tabs_report <- function() {
if (is.empty(input$ct_var1) || is.empty(input$ct_var2)) { if (is.empty(input$ct_var1) || is.empty(input$ct_var2)) {
return(invisible()) return(invisible())
} }
inp_out <- list("", "") inp_out <- list("", "")
if (length(input$ct_check) > 0) { if (length(input$ct_check) > 0) {
outputs <- c("summary", "plot") outputs <- c("summary", "plot")
inp_out[[1]] <- list(check = input$ct_check) inp_out[[1]] <- list(check = input$ct_check)
inp_out[[2]] <- list(check = input$ct_check, custom = FALSE) inp_out[[2]] <- list(check = input$ct_check, custom = FALSE)
figs <- TRUE figs <- TRUE
} else { } else {
outputs <- "summary" outputs <- "summary"
inp_out[[1]] <- list(check = "") inp_out[[1]] <- list(check = "")
figs <- FALSE figs <- FALSE
} }
update_report( update_report(
inp_main = clean_args(ct_inputs(), ct_args), inp_main = clean_args(ct_inputs(), ct_args),
inp_out = inp_out, inp_out = inp_out,
fun_name = "cross_tabs", fun_name = "cross_tabs",
outputs = outputs, outputs = outputs,
figs = figs, figs = figs,
fig.width = ct_plot_width(), fig.width = ct_plot_width(),
fig.height = ct_plot_height() fig.height = ct_plot_height()
) )
} }
download_handler( download_handler(
id = "dlp_cross_tabs", id = "dlp_cross_tabs",
fun = download_handler_plot, fun = download_handler_plot,
fn = function() paste0(input$dataset, "_cross_tabs"), fn = function() paste0(input$dataset, "_cross_tabs"),
type = "png", type = "png",
caption = i18n$t("Save cross-tabs plot"), caption = i18n$t("Save cross-tabs plot"),
plot = .plot_cross_tabs, plot = .plot_cross_tabs,
width = ct_plot_width, width = ct_plot_width,
height = ct_plot_height height = ct_plot_height
) )
observeEvent(input$cross_tabs_report, { observeEvent(input$cross_tabs_report, {
r_info[["latest_screenshot"]] <- NULL r_info[["latest_screenshot"]] <- NULL
cross_tabs_report() cross_tabs_report()
}) })
observeEvent(input$cross_tabs_screenshot, { observeEvent(input$cross_tabs_screenshot, {
r_info[["latest_screenshot"]] <- NULL r_info[["latest_screenshot"]] <- NULL
radiant_screenshot_modal("modal_cross_tabs_screenshot") radiant_screenshot_modal("modal_cross_tabs_screenshot")
}) })
observeEvent(input$modal_cross_tabs_screenshot, { observeEvent(input$modal_cross_tabs_screenshot, {
cross_tabs_report() cross_tabs_report()
removeModal() ## remove shiny modal after save removeModal() ## remove shiny modal after save
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment