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

added title arguments of x and group_by;

small changes in validation steps and description;
naming the lists;

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

adapted function to accept a dataframe as input and axestitles

added reexport of magrittr pipes
parent f6639ede
# Generated by roxygen2: do not edit by hand
export("%<>%")
export("%>%")
export(.bar_plot)
export(.scatter_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")))
......
#' magrittr forward-pipe operator
#'
#' See \code{\link[magrittr]{\%>\%}}.
#' @name %>%
#' @importFrom magrittr %>%
#' @export %>%
NULL
#' magrittr forward-backward-pipe operator
#'
#' See \code{\link[magrittr]{\%<>\%}}.
#' @name %<>%
#' @importFrom magrittr %<>%
#' @export %<>%
NULL
......@@ -128,16 +128,24 @@
#' Render a box plot with plotly.
#'
#' @param x Numeric observations for the boxplot.
#' @param df A dataframe containing the data fo the boxplot.
#' @param group_by A factor, by which observations can optionally be grouped.
#' @param title A title that describes the observations.
#' @param group_by_title A title that describes the grouping factor.
#' @param title_x A title that describes the observations.
#' @param title_group_by A title that describes the grouping factor.
#'
#' @return An object of class \code{plotly}.
#' @export
plotly_boxplot <- function(x, group_by = NULL, title = "", group_by_title = NULL){
plotly::plot_ly(x = x, y = group_by, type = "box", name = title) %>%
plotly::layout(xaxis = list(title = title, showline = T),
yaxis = list(title = group_by_title, showline = T, showticklabels = T),
showlegend = T)
plotly_boxplot <- function(df, group_by = NULL, title_x = NULL, title_group_by = NULL){
if (is.null(group_by)) {
y <- NULL
} else {
y <- df[[2]]
}
x <- df[[1]]
plotly::plot_ly(x = x, y = y, type = "box", name = names(df[1])) %>%
plotly::layout(xaxis = list(title = title_x, showline = T),
yaxis = list(title = title_group_by, showline = T, showticklabels = T),
showlegend = F)
}
......@@ -11,15 +11,19 @@ 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]]
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
# data.frame(x, group_by) %>%
# i2dash::embed_var()
i2dash::embed_var(df)
# Render plot
i2dash.scrnaseq::plotly_box_plot(x = x, group_by = group_by)
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}
......@@ -39,34 +43,49 @@ if ({{ env_id }}$group_by_selection) {
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')))
#
# Handle inputs
# Create reactive dataframe
#
if( !{{ env_id }}$x_selection ) {
x_{{ env_id }} <- shiny::reactive({
{{ env_id }}$x[[1]]
})
} else {
x_{{ env_id }} <- shiny::reactive({
{{ env_id }}$x[[input$select_x_{{ env_id }}]]
})
}
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 }} <- shiny::reactive({
{{ env_id }}$group_by[[1]]
})
} else {
group_by_{{ env_id }} <- shiny::reactive({
{{ env_id }}$group_by[[input$select_group_by_{{ 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_box_plot(x = x_{{ env_id }}(), group_by = group_by_{{ env_id }}())
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),
......
......@@ -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
}
% 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,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}.
......
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