Commit 44c6b64b authored by Ustjanzew's avatar Ustjanzew
Browse files

resolved merge conflict

Merge branch 'new_boxplot_2' into demo
parents 76eed2a7 8c0a6f89
......@@ -2,6 +2,10 @@
export("%<>%")
export("%>%")
<<<<<<< HEAD
=======
export(.bar_plot)
>>>>>>> new_boxplot_2
export(.scatter_plot)
export(barplot)
export(boxplot)
......
#' 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.
#' @param group_by A factor, by which observations can optionally be grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param title The title of the components junk.
#' @param x A vector with numerical values or a named list, which represents the observations for the boxplot (x-axis). In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param group_by (Optional) A vector with factorial values or characters or a named list, which will be used for grouping the observations. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param title (Optional) The title of the components junk.
#' @param title_x (Optional) The title of the x-axis.
#' @param title_group_by (Optional) The title of the y-axis.
#'
#' @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(any(sapply(group_by, is.character)) & !is.null(group_by)){
clust_names <- names(group_by[sapply(group_by, class) == 'character'])
for (name in clust_names){
group_by[[name]] <- as.factor(group_by[[name]])
}
}
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 +44,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")))
......
......@@ -125,16 +125,24 @@ plotly_barplot <- function(..., showlegend = NULL, title_x = NULL, title_group_b
#' Render a box plot with plotly.
#'
#' @param x Numeric observations for 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 df A dataframe containing the data fo the boxplot.
#' @param ... these arguments are of either the form value or tag = value and should be valid for the 'plotly::plot_ly()'
#' @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, ..., title_x = NULL, title_group_by = NULL){
# if (is.null(group_by)) {
# y <- NULL
# } else {
# y <- group_by[[1]]
# }
# x <- x[[1]]
plotly::plot_ly(..., type = "box") %>%
plotly::layout(xaxis = list(title = title_x, showline = T),
yaxis = list(title = title_group_by, showline = T, showticklabels = T),
showlegend = F)
}
......@@ -11,15 +11,23 @@ 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)
if(is.null(group_by)){
i2dash.scrnaseq::plotly_boxplot(df = df, x = df[[1]], y = NULL, name = names(df[1]), title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
} else {
i2dash.scrnaseq::plotly_boxplot(df = df, x = df[[1]], y = df[[2]], name = names(df[1]), title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
}
```
```{r, eval=is_shiny}
......@@ -39,34 +47,53 @@ 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 }}())
if(is.null({{ env_id }}$group_by)){
i2dash.scrnaseq::plotly_boxplot(df = df_{{ env_id }}(), x = df_{{ env_id }}()[[1]], y = NULL, name = names(df_{{ env_id }}()[1]), title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
} else {
i2dash.scrnaseq::plotly_boxplot(df = df_{{ env_id }}(), x = df_{{ env_id }}()[[1]], y = df_{{ env_id }}()[[2]], name = names(df_{{ env_id }}()[1]), title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
}
)
shiny::fillCol(flex = c(NA, 1),
......@@ -74,3 +101,5 @@ shiny::fillCol(flex = c(NA, 1),
plotly::plotlyOutput("plot_{{ env_id }}", height = "100%")
)
```
......@@ -2,22 +2,27 @@
% 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.}
\item{x}{Numeric observations for the boxplot. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{x}{A vector with numerical values or a named list, which represents the observations for the boxplot (x-axis). In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{group_by}{A factor, by which observations can optionally be grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{group_by}{(Optional) A vector with factorial values or characters or a named list, which will be used for grouping the observations. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{title}{The title of the components junk.}
\item{title}{(Optional) The title of the components junk.}
\item{title_x}{(Optional) The title of the x-axis.}
\item{title_group_by}{(Optional) The title of the y-axis.}
}
\value{
A string containing markdown code for the rendered component.
}
\description{
Renders a boxplot for cluster characterization
Renders a boxplot
}
......@@ -4,16 +4,18 @@
\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, ..., 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_x}{A title that describes the observations.}
\item{title}{A title that describes the observations.}
\item{title_group_by}{A title that describes the grouping factor.}
\item{group_by_title}{A title that describes the grouping factor.}
\item{x}{A named list with the observations for the boxplot.}
\item{group_by}{A list with factorial values, by which observations can optionally be grouped.}
}
\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