Commit 4a2cbc02 authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

.box_plot() method with template and validation

parent d6ef1127
# Generated by roxygen2: do not edit by hand
export(.bar_plot)
export(.box_plot)
export(.scatter_plot)
#' Renders a boxplot for cluster characterization
#'
#' @param object A \linkS4class{i2dash::i2dashboard} object.
#' @param cluster Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param x Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode. If x is NULL then a barplot for "Number of cells" per cluster will be created. If x is not NULL a barplot for "Fraction per cell" per cluster will be created.
#' @param title A title that will be displayed on top.
#'
#' @return A string containing markdown code for the rendered textbox
cluster_characterization_boxplot <- function(object, x, cluster, title = "Characterization of clusters") {
# Create random env id
env_id <- paste0("env_", stringi::stri_rand_strings(1, 6, pattern = "[A-Za-z0-9]"))
# validate input, create environment variables, save environment object
.validate_input_cluster_characterizatio_boxplot(object@workdir, env_id, x, cluster)
timestamp <- Sys.time()
expanded_component <- knitr::knit_expand(file = system.file("templates", "cluster_characterization_boxplot_template.Rmd", package = "i2dash.scrnaseq"), title = title, env_id = env_id, date = timestamp)
return(expanded_component)
}
.validate_input_cluster_characterizatio_boxplot <- function(workdir, env_id, x, cluster) {
env <- new.env()
env$x_selection <- FALSE
env$cluster_selection <- FALSE
# Check existence of cluster
if(is.null(cluster)) stop("cluster is required.")
if(is.null(x)) stop("x is required.")
# Create lists if needed
if(!is.list(x)) x <- list(x)
if(!is.list(cluster)) cluster <- list(cluster)
# should I use magrittr::%<>% ?
# name the lists
library(magrittr)
if(is.null(names(cluster))) x %<>% magrittr::set_names("clustering.1")
if(is.null(names(x))) x %<>% magrittr::set_names("x")
# Check validity
if(!all(sapply(cluster, is.factor))) stop("'cluster' should only contain factorial values.")
if(!all(sapply(x, is.numeric))) stop("'x' should only contain numerical values.")
# Check if lengths in a list are the same and if x and y and label and color_by are the same length
#if(length(unique(sapply(x, length))) != 1) stop("list x should contain elements with the same length.")
#if(length(unique(sapply(y, length))) != 1) stop("list y should contain elements with the same length.")
#if(!identical(length(x[[1]]), length(y[[1]]))) stop("all arguments should be of the the same length.")
#if(!identical(length(x[[1]]), length(colour_by[[1]])) & !is.null(colour_by)) stop("all arguments should be of the the same length.")
#if(!identical(length(x[[1]]), length(labels[[1]])) & !is.null(labels)) stop("all arguments should be of the the same length.")
# Add objects to env
env$x <- x
env$x_selection <- length(env$x) > 1
env$cluster <- cluster
env$cluster_selection <- length(env$cluster) > 1
# save environment as rds-object
saveRDS(env, file = file.path(workdir, "envs", paste0(env_id, ".rds")))
print("validation TRUE")
}
#' Renders a scatter plot
#' Render a scatter plot
#'
#' @param labels A list with sample names, that should be of the same length as x and y.
#' @param x Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.
......@@ -7,7 +7,6 @@
#'
#' @return A list with 1. the plotly object & 2. the data frame used in the plot
#' @export
.scatter_plot <- function(labels = NULL, x, y, colour_by = NULL, checkbox = FALSE, selected_label = NULL){
# create data.frame for plot (fill colour_by & labels with dummy data if NULL)
dummy_values <- FALSE
......@@ -78,14 +77,13 @@
return(p)
}
#' Renders a bar plot
#' Render a bar plot
#'
#' @param cluster Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param x Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#'
#' @return A list with 1. the plotly object & 2. the data frame used in the plot
#' @export
.bar_plot <- function(cluster, x = NULL){
#if x = NULL -> plot for "Number of cells"
......@@ -102,7 +100,6 @@
p <- plotly::layout(p,
xaxis = list(title=title, showline = T),
yaxis = list(title="Cluster", showline = T, showticklabels = T),
showlegend = F
)
return(list("plot" = p, "df" = tab_df))
......@@ -128,3 +125,24 @@
}
}
#' Render a box plot
#'
#' @param cluster Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#' @param x Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.
#'
#' @return A list with 1. the plotly object & 2. the data frame used in the plot
#' @export
.box_plot <- function(cluster, x){
df <- data.frame(cluster, x)
# plotly
title <- "Detected genes"
p <- plotly::plot_ly(df, y = df[[1]], x = df[[2]], type = "box", name = df[[1]])
p <- plotly::layout(p,
xaxis = list(title=title, showline = T),
yaxis = list(title="Cluster", showline = T, showticklabels = T),
showlegend = T
)
return(list("plot" = p, "df" = df))
}
### {{ title }}
<!-- Component created on {{ date }} -->
```{r}
{{ env_id }} = readRDS("envs/{{ env_id }}.rds")
is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```
```{r, eval=!is_shiny}
```
```{r, eval=is_shiny}
df <- data.frame({{ env_id }}$cluster[1], {{ env_id }}$x)
#print(df)
title <- "Detected genes"
p <- plotly::plot_ly(df, y = df[[1]], x = df[[2]], type = "box", name = df[[1]])
p <- plotly::layout(p,
xaxis = list(title=title, showline = T),
yaxis = list(title="Cluster", showline = T, showticklabels = T),
showlegend = T
)
p
```
***
Plot description:
Sequencing is called *saturated* when generating more sequencing output from a cDNA library does not substantially increase the number of detected features in a sample. Since the number of detected features can act as a technical confounder, and thereby drive substructure in the data, it is advisable to aim for a saturated sequencing by either adding more sequencing output or decreasing the number of samples until saturation is achieved. [@zhang_one_2018] gives advise on how to choose the optimal cell number given a fixed sequencing budget
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/visualization_functions.R
\name{.box_plot}
\alias{.box_plot}
\title{Render a bar plot}
\usage{
.box_plot(cluster, x)
}
\arguments{
\item{cluster}{Values for the membership to clusters. In case of a nested list, a dropdown menu will be provided in the interactive mode.}
\item{x}{Numeric values mapped to the x-axis. In case of a nested list, a dropdown menu will be provided in the interactive mode.}
}
\value{
A list with 1. the plotly object & 2. the data frame used in the plot
}
\description{
Render a bar plot
}
Supports Markdown
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