Skip to contents

Module conducts principal component analysis (PCA) on a given dataset and offers different ways of visualizing the outcomes, including elbow plot, circle plot, biplot, and eigenvector plot. Additionally, it enables dynamic customization of plot aesthetics, such as opacity, size, and font size, through UI inputs.

Usage

tm_a_pca(
  label = "Principal Component Analysis",
  dat,
  plot_height = c(600, 200, 2000),
  plot_width = NULL,
  ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
  ggplot2_args = teal.widgets::ggplot2_args(),
  rotate_xaxis_labels = FALSE,
  font_size = c(12, 8, 20),
  alpha = c(1, 0, 1),
  size = c(2, 1, 8),
  pre_output = NULL,
  post_output = NULL,
  decorators = NULL
)

Arguments

label

(character(1)) Label shown in the navigation item for the module or module group. For modules() defaults to "root". See Details.

dat

(data_extract_spec or list of multiple data_extract_spec) specifying columns used to compute PCA.

plot_height

(numeric) optional, specifies the plot height as a three-element vector of value, min, and max intended for use with a slider UI element.

plot_width

(numeric) optional, specifies the plot width as a three-element vector of value, min, and max for a slider encoding the plot width.

ggtheme

(character) optional, ggplot2 theme to be used by default. Defaults to "gray".

ggplot2_args

(ggplot2_args) optional, object created by teal.widgets::ggplot2_args() with settings for all the plots or named list of ggplot2_args objects for plot-specific settings. The argument is merged with options variable teal.ggplot2_args and default module setup.

List names should match the following: c("default", "Elbow plot", "Circle plot", "Biplot", "Eigenvector plot").

For more details see the vignette: vignette("custom-ggplot2-arguments", package = "teal.widgets").

rotate_xaxis_labels

(logical) optional, whether to rotate plot X axis labels. Does not rotate by default (FALSE).

font_size

(numeric) optional, specifies font size. It controls the font size for plot titles, axis labels, and legends.

  • If vector of length == 1 then the font sizes will have a fixed size.

  • while vector of value, min, and max allows dynamic adjustment.

alpha

(integer(1) or integer(3)) optional, specifies point opacity.

  • When the length of alpha is one: the plot points will have a fixed opacity.

  • When the length of alpha is three: the plot points opacity are dynamically adjusted based on vector of value, min, and max.

size

(integer(1) or integer(3)) optional, specifies point size.

  • When the length of size is one: the plot point sizes will have a fixed size.

  • When the length of size is three: the plot points size are dynamically adjusted based on vector of value, min, and max.

pre_output

(shiny.tag) optional, text or UI element to be displayed before the module's output, providing context or a title. with text placed before the output to put the output into context. For example a title.

post_output

(shiny.tag) optional, text or UI element to be displayed after the module's output, adding context or further instructions. Elements like shiny::helpText() are useful.

decorators

[Experimental] (list of teal_transform_module, named list of teal_transform_module or NULL) optional, if not NULL, decorator for tables or plots included in the module. When a named list of teal_transform_module, the decorators are applied to the respective output objects.

Otherwise, the decorators are applied to all objects, which is equivalent as using the name default.

See section "Decorating tm_a_pca" below for more details.

Value

Object of class teal_module to be used in teal applications.

Decorating tm_a_pca

This module generates the following objects, which can be modified in place using decorators:

  • elbow_plot (ggplot2)

  • circle_plot (ggplot2)

  • biplot (ggplot2)

  • eigenvector_plot (ggplot2)

Decorators can be applied to all outputs or only to specific objects using a named list of teal_transform_module objects. The "default" name is reserved for decorators that are applied to all outputs. See code snippet below:

tm_a_pca(
   ..., # arguments for module
   decorators = list(
     default = list(teal_transform_module(...)), # applied to all outputs
     elbow_plot = list(teal_transform_module(...)), # applied only to `elbow_plot` output
     circle_plot = list(teal_transform_module(...)) # applied only to `circle_plot` output
     biplot = list(teal_transform_module(...)) # applied only to `biplot` output
     eigenvector_plot = list(teal_transform_module(...)) # applied only to `eigenvector_plot` output
   )
)

For additional details and examples of decorators, refer to the vignette vignette("decorate-modules-output", package = "teal") or the teal::teal_transform_module() documentation.

Examples in Shinylive

example-1

Open in Shinylive

example-2

Open in Shinylive

Examples


# general data example
data <- teal_data()
data <- within(data, {
  require(nestcolor)
  USArrests <- USArrests
})

app <- init(
  data = data,
  modules = modules(
    tm_a_pca(
      "PCA",
      dat = data_extract_spec(
        dataname = "USArrests",
        select = select_spec(
          choices = variable_choices(
            data = data[["USArrests"]], c("Murder", "Assault", "UrbanPop", "Rape")
          ),
          selected = c("Murder", "Assault"),
          multiple = TRUE
        ),
        filter = NULL
      )
    )
  )
)
#> Initializing tm_a_pca
#> Initializing reporter_previewer_module
if (interactive()) {
  shinyApp(app$ui, app$server)
}


# CDISC data example
data <- teal_data()
data <- within(data, {
  require(nestcolor)
  ADSL <- teal.data::rADSL
})
join_keys(data) <- default_cdisc_join_keys[names(data)]

app <- init(
  data = data,
  modules = modules(
    tm_a_pca(
      "PCA",
      dat = data_extract_spec(
        dataname = "ADSL",
        select = select_spec(
          choices = variable_choices(
            data = data[["ADSL"]], c("BMRKR1", "AGE", "EOSDY")
          ),
          selected = c("BMRKR1", "AGE"),
          multiple = TRUE
        ),
        filter = NULL
      )
    )
  )
)
#> Initializing tm_a_pca
#> Initializing reporter_previewer_module
if (interactive()) {
  shinyApp(app$ui, app$server)
}