Commit 6782cbf0 authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

Changed the manpage-description of plotly_barplot(), barplot(), create_barplot_df();

outsourced create_barplot_df to visualization_functions.R;
Extended the flexibility with "...";
added conversion of character values to factorial;
redone the reactive method
parent 79d3068a
......@@ -5,6 +5,7 @@ export("%>%")
export(.scatter_plot)
export(barplot)
export(boxplot)
export(create_barplot_df)
export(plotly_barplot)
export(plotly_boxplot)
importFrom(magrittr,"%<>%")
......
#' Renders a horizontal barplot
#'
#' @param object A \linkS4class{i2dash::i2dashboard} object.
#' @param group_by A factor, by which observations are grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param x Numeric observations for the boxplot. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param title_x A title of the y-axis that describes the observations. In case of a named list this parameter is not needed because the names of the list will be used as title of the y axis.
#' @param title_group_by A title of the x-axis that describes the grouping factor. In case of a named list this parameter is not needed because the names of the list will be used as title of the x axis.
#' @param title The title of the components junk.
#' @param group_by 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. Note: If parameter 'x' is not provided (NULL), a barplot with the number of each 'level' is used as observation.
#' @param x (Optional) A vector with numerical values or a named list, which represents the observations for the horizontal barplot (x-axis). 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 textbox
#' @return A string containing markdown code for the rendered component.
#' @export
barplot <- function(object, group_by, x = NULL, title = NULL, title_group_by = NULL, title_x = NULL) {
barplot <- function(object, group_by, x = 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]"))
......@@ -22,6 +22,12 @@ barplot <- function(object, group_by, x = NULL, title = NULL, title_group_by = N
if(is.null(names(x)) & !is.null(x)) x %<>% magrittr::set_names("values")
# Validate input
if(any(sapply(group_by, is.character))){
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))) stop("'group_by' should only contain factorial values.")
if(!all(sapply(x, is.factor)) & !is.null(x)) stop("'x' should only contain factorial values.")
......
......@@ -77,19 +77,45 @@
return(p)
}
# Function to create a dataframe for plotly_barplot.
#' @param group_by A list with factorial values, by which observations can optionally be grouped.
#' @param x (Optional) A named list with the observations for the barplot.
#'
#' @return An object of class \code{list} containig the dataframe 'df', the vector 'x' with values for the x-axis, the vector 'y' with values for the y-axis, the vector 'names' (can be NULL), the boolean value 'showlegend'.
#' @export
create_barplot_df <- function(group_by, x = NULL){
if(is.null(x)){
tab <- table(group_by)
df <- as.data.frame(tab)
x <- df[2]
y <- df[1]
names <- NULL
showlegend <- F
return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
} else {
tab <- table(group_by, x)
ptab <- prop.table(tab, margin = 1)
df <- as.data.frame(ptab)
x <- df[3]
y <- df[1]
names <- df[2]
showlegend <- T
return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
}
}
#' Render a bar plot with plotly.
#'
#' @param x Numeric observations for the boxplot.
#' @param group_by A factor, by which observations can optionally be grouped.
#' @param ... these arguments are of either the form value or tag = value and should be valid for the 'plotly::plot_ly()' method.
#' @param showlegend Boolean value that describes if the legend should be shown.
#' @param title_x A title that describes the observations.
#' @param title_group_by A title that describes the grouping factor.
#' @param showlegend Boolean that describes if the legend is shown
#' @param names Names of the observationable categories
#'
#' @return An object of class \code{plotly}.
#' @export
plotly_barplot <- function(group_by, x = NULL, names = NULL, showlegend = NULL, title_x = NULL, title_group_by = NULL){
p <- plotly::plot_ly(type = "bar", orientation = "h", opacity = 0.7, x = x[[1]], y = group_by[[1]], name = names[[1]]) %>%
plotly_barplot <- function(..., showlegend = NULL, title_x = NULL, title_group_by = NULL){
p <- plotly::plot_ly(..., type = "bar", orientation = "h", opacity = 0.7) %>%
plotly::layout(xaxis = list(title = title_x, showline = T),
yaxis = list(title = title_group_by, showline = T, showticklabels = T),
barmode = 'stack',
......
......@@ -10,36 +10,14 @@ is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```
```{r, eval=!is_shiny}
# Function to create a dataframe for bar plot
.create_barplot_df <- function(group_by, x = NULL){
if(is.null(x)){
tab <- table(group_by)
df <- as.data.frame(tab)
x <- df[[2]]
y <- df[[1]]
names <- NULL
showlegend <- F
return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
} else {
tab <- table(group_by, x)
ptab <- prop.table(tab, margin = 1)
df <- as.data.frame(ptab)
x <- df[[3]]
y <- df[[1]]
names <- df[[2]]
showlegend <- T
return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
}
}
output_list <- .create_barplot_df(group_by = {{ env_id }}$group_by[[1]], x = {{ env_id }}$x[[1]])
output_list <- i2dash.scrnaseq::create_barplot_df(group_by = {{ env_id }}$group_by[[1]], x = {{ env_id }}$x[[1]])
# Provide data download
i2dash::embed_var(output_list$df)
# Render plot
i2dash.scrnaseq::plotly_barplot(x = output_list$x, group_by = output_list$y, names = output_list$names, showlegend = output_list$showlegend, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
i2dash.scrnaseq::plotly_barplot(x = df_{{ env_id }}()$x[[1]], y = df_{{ env_id }}()$y[[1]], name = df_{{ env_id }}()$names[[1]], showlegend = df_{{ env_id }}()$showlegend, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
```
```{r, eval=is_shiny}
......@@ -62,45 +40,23 @@ if ({{ env_id }}$group_by_selection){
# download_link
ui_list <- rlist::list.append(ui_list, tags$div(tags$br(), downloadButton('downloadData_{{ env_id }}', 'Download data')))
# Function to create a dataframe for bar plot
.create_barplot_df <- function(group_by, x = NULL){
if(is.null(x)){
tab <- table(group_by)
df <- as.data.frame(tab)
x <- df[2]
y <- df[1]
names <- NULL
showlegend <- F
return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
} else {
tab <- table(group_by, x)
ptab <- prop.table(tab, margin = 1)
df <- as.data.frame(ptab)
x <- df[3]
y <- df[1]
names <- df[2]
showlegend <- T
return(list("df" = df, "x" = x, "y" = y, "names" = names, "showlegend" = showlegend))
}
}
#
# Create reactive dataframe
#
df_{{ env_id }} <- shiny::reactive({
if({{ env_id }}$group_by_selection & {{ env_id }}$x_selection){
.create_barplot_df(group_by = {{ env_id }}$group_by[[input$select_group_by_{{ env_id }}]], x = {{ env_id }}$x[[input$select_x_{{ env_id }}]])
} else if (!{{ env_id }}$group_by_selection & {{ env_id }}$x_selection){
.create_barplot_df(group_by = {{ env_id }}$group_by[[1]], x = {{ env_id }}$x[[input$select_x_{{ env_id }}]])
} else if ({{ env_id }}$group_by_selection & !{{ env_id }}$x_selection){
.create_barplot_df(group_by = {{ env_id }}$group_by[[input$select_group_by_{{ env_id }}]], x = {{ env_id }}$x[[1]])
} else if (!{{ env_id }}$group_by_selection & !{{ env_id }}$x_selection){
.create_barplot_df(group_by = {{ env_id }}$group_by[[1]], x = {{ env_id }}$x[[1]])
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(!{{ env_id }}$x_selection){
x = {{ env_id }}$x[[1]]
} else {
x = {{ env_id }}$x[[input$select_x_{{ env_id }}]]
}
return(i2dash.scrnaseq::create_barplot_df(group_by = group_by, x = x))
})
#
......@@ -117,7 +73,7 @@ output$downloadData_{{ env_id }} <- downloadHandler(
# Output
#
output$plot_{{ env_id }} <- plotly::renderPlotly({
i2dash.scrnaseq::plotly_barplot(x = df_{{ env_id }}()$x, group_by = df_{{ env_id }}()$y, names = df_{{ env_id }}()$names, showlegend = df_{{ env_id }}()$showlegend, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
i2dash.scrnaseq::plotly_barplot(x = df_{{ env_id }}()$x[[1]], y = df_{{ env_id }}()$y[[1]], name = df_{{ env_id }}()$names[[1]], showlegend = df_{{ env_id }}()$showlegend, title_x = {{ env_id }}$title_x, title_group_by = {{ env_id }}$title_group_by)
})
shiny::fillCol(flex = c(NA, 1),
......
......@@ -4,24 +4,24 @@
\alias{barplot}
\title{Renders a horizontal barplot}
\usage{
barplot(object, group_by, x = NULL, title = NULL,
title_group_by = NULL, title_x = NULL)
barplot(object, group_by, x = NULL, title = NULL, title_x = NULL,
title_group_by = NULL)
}
\arguments{
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{group_by}{A factor, by which observations are grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{group_by}{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. Note: If parameter 'x' is not provided (NULL), a barplot with the number of each 'level' is used as observation.}
\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}{(Optional) A vector with numerical values or a named list, which represents the observations for the horizontal barplot (x-axis). 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_group_by}{A title of the x-axis that describes the grouping factor. In case of a named list this parameter is not needed because the names of the list will be used as title of the x axis.}
\item{title_x}{(Optional) The title of the x-axis.}
\item{title_x}{A title of the y-axis that describes the observations. In case of a named list this parameter is not needed because the names of the list will be used as title of the y axis.}
\item{title_group_by}{(Optional) The title of the y-axis.}
}
\value{
A string containing markdown code for the rendered textbox
A string containing markdown code for the rendered component.
}
\description{
Renders a horizontal barplot
......
......@@ -4,21 +4,19 @@
\alias{plotly_barplot}
\title{Render a bar plot with plotly.}
\usage{
plotly_barplot(group_by, x = NULL, names = NULL, showlegend = NULL,
title_x = NULL, title_group_by = NULL)
plotly_barplot(..., showlegend = NULL, title_x = NULL,
title_group_by = NULL)
}
\arguments{
\item{group_by}{A factor, by which observations can optionally be grouped.}
\item{x}{Numeric observations for the boxplot.}
\item{names}{Names of the observationable categories}
\item{showlegend}{Boolean that describes if the legend is shown}
\item{title_x}{A title that describes the observations.}
\item{title_group_by}{A title that describes the grouping factor.}
\item{group_by}{A factor, by which observations can optionally be grouped.}
\item{x}{Numeric observations for the boxplot.}
}
\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