Commit fd0a7501 authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

boxplot.R:

added title arguments of x and group_by;
small changes in validation steps and description;
naming the lists;

boxplot.Rmd:
adapted the part for statical report;
added download option;
combined reactives to one single reactive outputting a data frame

visualization_function:
adapted function to accept a dataframe as input and axestitles
parent 3c6e6321
# Generated by roxygen2: do not edit by hand
export("%<>%")
export("%>%")
export(.bar_plot)
export(.scatter_plot)
export(.violin_plot)
export(boxplot)
export(plotly_boxplot)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
#' Renders a boxplot for cluster characterization
#' Renders a boxplot
#'
#' @param object A \linkS4class{i2dash::i2dashboard} object.
#' @param x Numeric observations for the boxplot. In case of a named list, a dropdown menu will be provided in the interactive mode.
......@@ -6,21 +6,26 @@
#' @param title The title of the components junk.
#'
#' @return A string containing markdown code for the rendered component.
#'
#' @export
boxplot <- function(object, x, group_by, title = NULL) {
boxplot <- function(object, x, group_by = NULL, title = NULL, title_x = NULL, title_group_by = NULL) {
# Create random env id
env_id <- paste0("env_", stringi::stri_rand_strings(1, 6, pattern = "[A-Za-z0-9]"))
# Create list if element is not a list already
if(!is.list(x)) x <- list(x)
if(!is.list(group_by)) group_by <- list(group_by)
if(!is.list(group_by) & !is.null(group_by)) group_by <- list(group_by)
# name the lists
if(is.null(names(group_by)) & !is.null(group_by)) group_by %<>% magrittr::set_names("group")
if(is.null(names(x))) x %<>% magrittr::set_names("sample")
# Validate input
if(!all(sapply(x, is.numeric))) stop("'x' should only contain numerical values.")
if(!all(sapply(cluster, is.factor))) stop("'cluster' should only contain factorial values.")
if(!all(sapply(group_by, is.factor)) & !is.null(group_by)) stop("'group_by' should only contain factorial values.")
# Create component environment
env <- new.env()
env$x_selection <- FALSE
env$group_by_selection <- FALSE
......@@ -30,6 +35,9 @@ boxplot <- function(object, x, group_by, title = NULL) {
env$group_by <- group_by
env$group_by_selection <- length(env$group_by) > 1
env$title_x <- title_x
env$title_group_by <- title_group_by
# save environment object
saveRDS(env, file = file.path(object@workdir, "envs", paste0(env_id, ".rds")))
......
......@@ -9,6 +9,102 @@
is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```
### {{ 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}
# set variables
x <- {{ env_id }}$x[1]
group_by <- {{ env_id }}$group_by[1]
if(is.null(group_by)){
df <- data.frame(x)
} else {
df <- data.frame(x, group_by)
}
# Provide data download
i2dash::embed_var(df)
# Render plot
i2dash.scrnaseq::plotly_boxplot(df = df, group_by = {{ env_id }}$group_by, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
```
```{r, eval=is_shiny}
ui_list <- list()
# selection field for x
if ({{ env_id }}$x_selection) {
ui_list <- rlist::list.append(ui_list,
selectInput("select_x_{{ env_id }}", label = "Select observations:",
choices = names({{ env_id }}$x)))
}
# selection field for group_by
if ({{ env_id }}$group_by_selection) {
ui_list <- rlist::list.append(ui_list,
selectInput("select_group_by_{{ env_id }}", label = "Group observations by:",
choices = names({{ env_id }}$group_by)))
}
# download_link
ui_list <- rlist::list.append(ui_list, tags$div(tags$br(), downloadButton('downloadData_{{ env_id }}', 'Download data')))
#
# Create reactive dataframe
#
df_{{ env_id }} <- shiny::reactive({
if( !{{ env_id }}$x_selection ) {
x <- {{ env_id }}$x[1]
} else {
x <- {{ env_id }}$x[input$select_x_{{ env_id }}]
}
if( !{{ env_id }}$group_by_selection ) {
group_by <- {{ env_id }}$group_by[1]
} else {
group_by <- {{ env_id }}$group_by[input$select_group_by_{{ env_id }}]
}
if(is.null(group_by)){
df <- data.frame(x)
} else {
df <- data.frame(x, group_by)
}
return(df)
})
#
# Download
#
output$downloadData_{{ env_id }} <- downloadHandler(
filename = paste('data-', Sys.Date(), '.csv', sep=''),
content = function(file) {
write.csv(df_{{ env_id }}(), file)
}
)
#
# Output
#
output$plot_{{ env_id }} <- plotly::renderPlotly(
i2dash.scrnaseq::plotly_boxplot(df = df_{{ env_id }}(), group_by = {{ env_id }}$group_by, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
)
shiny::fillCol(flex = c(NA, 1),
do.call(shiny::inputPanel, ui_list),
plotly::plotlyOutput("plot_{{ env_id }}", height = "100%")
)
```
```{r, eval=!is_shiny}
# set variables
x <- {{ env_id }}$x[[1]]
......
......@@ -2,9 +2,10 @@
% Please edit documentation in R/boxplot.R
\name{boxplot}
\alias{boxplot}
\title{Renders a boxplot for cluster characterization}
\title{Renders a boxplot}
\usage{
boxplot(object, x, group_by, title = NULL)
boxplot(object, x, group_by = NULL, title = NULL, title_x = NULL,
title_group_by = NULL)
}
\arguments{
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
......@@ -19,5 +20,5 @@ boxplot(object, x, group_by, title = NULL)
A string containing markdown code for the rendered component.
}
\description{
Renders a boxplot for cluster characterization
Renders a boxplot
}
......@@ -2,7 +2,7 @@
% Please edit documentation in R/visualization_functions.R
\name{.violin_plot}
\alias{.violin_plot}
\title{Render a box plot}
\title{Render a violin plot}
\usage{
.violin_plot(x, y)
}
......@@ -15,5 +15,5 @@
A list with 1. the plotly object & 2. the data frame used in the plot
}
\description{
Render a box plot
Render a violin plot
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/reexports.R
\name{\%>\%}
\alias{\%>\%}
\title{magrittr forward-pipe operator}
\description{
See \code{\link[magrittr]{\%>\%}}.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/reexports.R
\name{\%<>\%}
\alias{\%<>\%}
\title{magrittr forward-backward-pipe operator}
\description{
See \code{\link[magrittr]{\%<>\%}}.
}
......@@ -4,16 +4,16 @@
\alias{multiplot}
\title{Renders a Sequence saturation plot}
\usage{
multiplot(object, plot_title, x, y, color_by)
multiplot(object, x, y, title = "Multiplot", colour_by = NULL)
}
\arguments{
\item{plot_title}{The title of the Component}
\item{x}{A list with the x-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{y}{A list with the y-axis values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{color_by}{A list with the color_by values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{colour_by}{A list with the color_by values. If it is a nested list, a dropdown-field will be provided in the interactive mode.}
\item{plot_title}{The title of the Component}
}
\value{
A string containing markdown code for the rendered textbox
......
......@@ -4,16 +4,17 @@
\alias{plotly_boxplot}
\title{Render a box plot with plotly.}
\usage{
plotly_boxplot(x, group_by = NULL, title = "", group_by_title = NULL)
plotly_boxplot(df, group_by = NULL, title_x = NULL,
title_group_by = NULL)
}
\arguments{
\item{x}{Numeric observations for the boxplot.}
\item{df}{A dataframe containing the data fo the boxplot.}
\item{group_by}{A factor, by which observations can optionally be grouped.}
\item{title}{A title that describes the observations.}
\item{title_x}{A title that describes the observations.}
\item{group_by_title}{A title that describes the grouping factor.}
\item{title_group_by}{A title that describes the grouping factor.}
}
\value{
An object of class \code{plotly}.
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sequence_saturation.R
% Please edit documentation in R/dimension_reduction.R, R/sequence_saturation.R
\name{sequence_saturation}
\alias{sequence_saturation}
\title{Renders a Sequence saturation plot}
\usage{
sequence_saturation(object, x, y, colour_by = NULL, labels = NULL,
title = "Sequencing saturation")
sequence_saturation(object, x, y, colour_by = NULL, labels = NULL,
title = "Sequencing saturation")
}
......@@ -18,11 +21,23 @@ sequence_saturation(object, x, y, colour_by = NULL, labels = NULL,
\item{labels}{A list with sample names, that should be of the same length as x and y.}
\item{title}{A title that will be displayed on top.}
\item{red_dim}{List with reduced dimensions as data.frames.}
\item{coulor_by}{A factor that will be mapped to colours.In case of a nested list, a dropdown menu will be provided in the interactive mode.}
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{title}{A title that will be displayed on top.}
}
\value{
A string containing markdown code for the rendered textbox
A string containing markdown code for the rendered textbox
}
\description{
Renders a Sequence saturation plot
Renders a Sequence saturation plot
}
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