Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-release tm_g_bivariate document update #660

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 69 additions & 86 deletions R/tm_g_bivariate.R
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
#' Univariate and bivariate visualizations
#' @md
#' Univariate and bivariate visualizations module
#'
#' Module enables the creation of univariate and bivariate plots,
#' facilitating the exploration of data distributions and relationships between two variables.
#'
#' This is a general module to visualize 1 & 2 dimensional data.
#'
#' @note
#' For more examples, please see the vignette "Using bivariate plot" via
#' `vignette("using-bivariate-plot", package = "teal.modules.general")`.
#'
#' @inheritParams teal::module
#' @inheritParams shared_params
#' @param x (`data_extract_spec` or `list` of multiple `data_extract_spec`)
#' Variable names selected to plot along the x-axis by default. Variable can be numeric, factor or character.
#' No empty selections are allowed!
#' Variable names selected to plot along the x-axis by default.
#' Can be numeric, factor or character.
#' No empty selections are allowed.
#' @param y (`data_extract_spec` or `list` of multiple `data_extract_spec`)
#' Variable names selected to plot along the y-axis by default. Variable can be numeric, factor or character.
#' @param use_density optional, (`logical`) value for whether density (`TRUE`) is plotted or
#' frequency (`FALSE`). Defaults to frequency (`FALSE`).
#' @param row_facet optional, (`data_extract_spec` or `list` of multiple `data_extract_spec`)
#' Variables for row facetting.
#' @param col_facet optional, (`data_extract_spec` or `list` of multiple `data_extract_spec`)
#' Variables for col facetting.
#' @param facet optional, (`logical`) to specify whether the facet encodings `ui` elements are toggled
#' on and shown to the user by default. Defaults to `TRUE` if either `row_facet` or `column_facet`
#' are supplied.
#' Variable names selected to plot along the y-axis by default.
#' Can be numeric, factor or character.
#' @param use_density (`logical`, optional) Indicates whether to plot density (`TRUE`) or frequency (`FALSE`).
#' Defaults to frequency (`FALSE`).
#' @param row_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`, optional)
#' Variables for row facetting.
#' @param col_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`, optional)
#' Variables for column facetting.
#' @param facet (`logical`, optional) to specify whether the facet encodings `ui` elements are toggled
#' on and shown to the user by default. Defaults to `TRUE` if either `row_facet` or `column_facet`
#' are supplied.
#' @param color_settings (`logical`) Whether coloring, filling and size should be applied
#' and `UI` tool offered to the user.
#' @param color optional, (`data_extract_spec` or `list` of multiple `data_extract_spec`)
#' Variables selected for the outline color inside the coloring settings.
#' It will be applied when `color_settings` is set to `TRUE`.
#' @param fill optional, (`data_extract_spec` or `list` of multiple `data_extract_spec`)
#' Variables selected for the fill color inside the coloring settings.
#' It will be applied when `color_settings` is set to `TRUE`.
#' @param size optional, (`data_extract_spec` or `list` of multiple `data_extract_spec`)
#' Variables selected for the size of `geom_point` plots inside the coloring settings.
#' It will be applied when `color_settings` is set to `TRUE`.
#' @param free_x_scales optional, (`logical`) Whether X scaling shall be changeable.
#' Does not allow scaling to be changed by default (`FALSE`).
#' @param free_y_scales optional, (`logical`) Whether Y scaling shall be changeable.
#' Does not allow scaling to be changed by default (`FALSE`).
#' @param swap_axes optional, (`logical`) Whether to swap X and Y axes. Defaults to `FALSE`.
#' @param color (`data_extract_spec` or `list` of multiple `data_extract_spec`, optional)
#' Variables selected for the outline color inside the coloring settings.
#' It will be applied when `color_settings` is set to `TRUE`.
#' @param fill (`data_extract_spec` or `list` of multiple `data_extract_spec`, optional)
#' Variables selected for the fill color inside the coloring settings.
#' It will be applied when `color_settings` is set to `TRUE`.
#' @param size (`data_extract_spec` or `list` of multiple `data_extract_spec`, optional)
#' Variables selected for the size of `geom_point` plots inside the coloring settings.
#' It will be applied when `color_settings` is set to `TRUE`.
#' @param free_x_scales (`logical`, optional) Whether X scaling shall be changeable.
#' Does not allow scaling to be changed by default (`FALSE`).
#' @param free_y_scales (`logical`, optional) Whether Y scaling shall be changeable.
#' Does not allow scaling to be changed by default (`FALSE`).
#' @param swap_axes (`logical`, optional) Whether to swap X and Y axes. Defaults to `FALSE`.
#'
#' @details
#' This is a general module to visualize 1 & 2 dimensional data.
#'
#' @note
#' For more examples, please see the vignette "Using bivariate plot" via
#' `vignette("using-bivariate-plot", package = "teal.modules.general")`.
#'
#' @export
#' @return Object of class `teal_module` to be used in `teal` applications.
#'
#' @examples
#' # Bivariate plot of selected variable (AGE) against selected (SEX)
#' library(teal.widgets)
#'
#' data <- teal_data()
#' data <- within(data, {
#' library(nestcolor)
Expand All @@ -54,48 +59,48 @@
#' datanames(data) <- datanames
#' join_keys(data) <- default_cdisc_join_keys[datanames]
#'
#' app <- teal::init(
#' app <- init(
#' data = data,
#' modules = teal::modules(
#' teal.modules.general::tm_g_bivariate(
#' x = teal.transform::data_extract_spec(
#' modules = modules(
#' tm_g_bivariate(
#' x = data_extract_spec(
#' dataname = "ADSL",
#' select = teal.transform::select_spec(
#' select = select_spec(
#' label = "Select variable:",
#' choices = teal.transform::variable_choices(data[["ADSL"]]),
#' choices = variable_choices(data[["ADSL"]]),
#' selected = "AGE",
#' fixed = FALSE
#' )
#' ),
#' y = teal.transform::data_extract_spec(
#' y = data_extract_spec(
#' dataname = "ADSL",
#' select = teal.transform::select_spec(
#' select = select_spec(
#' label = "Select variable:",
#' choices = teal.transform::variable_choices(data[["ADSL"]]),
#' choices = variable_choices(data[["ADSL"]]),
#' selected = "SEX",
#' multiple = FALSE,
#' fixed = FALSE
#' )
#' ),
#' row_facet = teal.transform::data_extract_spec(
#' row_facet = data_extract_spec(
#' dataname = "ADSL",
#' select = teal.transform::select_spec(
#' select = select_spec(
#' label = "Select variable:",
#' choices = teal.transform::variable_choices(data[["ADSL"]]),
#' choices = variable_choices(data[["ADSL"]]),
#' selected = "ARM",
#' fixed = FALSE
#' )
#' ),
#' col_facet = teal.transform::data_extract_spec(
#' col_facet = data_extract_spec(
#' dataname = "ADSL",
#' select = teal.transform::select_spec(
#' select = select_spec(
#' label = "Select variable:",
#' choices = teal.transform::variable_choices(data[["ADSL"]]),
#' choices = variable_choices(data[["ADSL"]]),
#' selected = "COUNTRY",
#' fixed = FALSE
#' )
#' ),
#' ggplot2_args = teal.widgets::ggplot2_args(
#' ggplot2_args = ggplot2_args(
#' labs = list(subtitle = "Plot generated by Bivariate Module")
#' )
#' )
Expand All @@ -104,6 +109,10 @@
#' if (interactive()) {
#' shinyApp(app$ui, app$server)
#' }
#'
#'
#' @export
#'
kartikeyakirar marked this conversation as resolved.
Show resolved Hide resolved
tm_g_bivariate <- function(label = "Bivariate Plots",
x,
y,
Expand Down Expand Up @@ -225,6 +234,7 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
)
}

# UI function for the bivariate module.
ui_g_bivariate <- function(id, ...) {
args <- list(...)
is_single_dataset_value <- teal.transform::is_single_dataset(
Expand Down Expand Up @@ -365,7 +375,7 @@ ui_g_bivariate <- function(id, ...) {
)
}


# Server function for the bivariate module.
srv_g_bivariate <- function(id,
data,
reporter,
Expand Down Expand Up @@ -646,15 +656,10 @@ srv_g_bivariate <- function(id,
})
}


#' Get Substituted ggplot call
#'
#' @noRd
#'
#' @examples
#'
#' bivariate_plot_call("ANL", "BAGE", "RACE", "numeric", "factor")
#' bivariate_plot_call("ANL", "BAGE", character(0), "numeric", "NULL")
#' @keywords internal
bivariate_plot_call <- function(data_name,
x = character(0),
y = character(0),
Expand Down Expand Up @@ -711,28 +716,10 @@ substitute_q <- function(x, env) {


#' Create ggplot part of plot call
#'
#' Due to the type of the x and y variable the plot type is chosen
#'
#' @noRd
#'
#' @examples
#' bivariate_ggplot_call("numeric", "NULL")
#' bivariate_ggplot_call("numeric", "NULL", freq = FALSE)
#'
#' bivariate_ggplot_call("NULL", "numeric")
#' bivariate_ggplot_call("NULL", "numeric", freq = FALSE)
#'
#' bivariate_ggplot_call("NULL", "factor")
#' bivariate_ggplot_call("NULL", "factor", freq = FALSE)
#'
#' bivariate_ggplot_call("factor", "NULL")
#' bivariate_ggplot_call("factor", "NULL", freq = FALSE)
#'
#' bivariate_ggplot_call("numeric", "numeric")
#' bivariate_ggplot_call("numeric", "factor")
#' bivariate_ggplot_call("factor", "numeric")
#' bivariate_ggplot_call("factor", "factor")
#' @keywords internal
bivariate_ggplot_call <- function(x_class = c("NULL", "numeric", "integer", "factor", "character", "logical"),
y_class = c("NULL", "numeric", "integer", "factor", "character", "logical"),
freq = TRUE,
Expand Down Expand Up @@ -913,19 +900,14 @@ bivariate_ggplot_call <- function(x_class = c("NULL", "numeric", "integer", "fac
plot_call <- reduce_plot_call(plot_call, quote(coord_flip()))
}

return(plot_call)
plot_call
}


#' Create facet call
#'
#' @noRd
#'
#' @examples
#'
#' facet_ggplot_call(LETTERS[1:3])
#' facet_ggplot_call(NULL, LETTERS[23:26])
#' facet_ggplot_call(LETTERS[1:3], LETTERS[23:26])
#' @keywords internal
facet_ggplot_call <- function(row_facet = character(0),
col_facet = character(0),
free_x_scales = FALSE,
Expand Down Expand Up @@ -955,7 +937,8 @@ facet_ggplot_call <- function(row_facet = character(0),
call("facet_grid", rows = call_fun_dots("vars", row_facet), scales = scales)
}
}

#' @noRd
#' @keywords internal
coloring_ggplot_call <- function(colour,
fill,
size,
Expand Down
Loading