Commit 9b50b56c authored by arsenij.ustjanzew's avatar arsenij.ustjanzew
Browse files

Changed the manpage description;

added conversion of character to factorial as input values;
Created a method for dataframe creation and extended the flexibility with "...";
Changed the Rmd file for "..."-mechanism and small bugfix in selection box.
parent a937cadc
......@@ -5,6 +5,7 @@ export("%>%")
export(.bar_plot)
export(.scatter_plot)
export(boxplot)
export(create_violinplot_df)
export(plotly_boxplot)
export(plotly_violinplot)
importFrom(magrittr,"%<>%")
......
#' Renders a vertical violin plot
#'
#' @param object A \linkS4class{i2dash::i2dashboard} object.
#' @param y Numeric observations for the violinplot. 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 be grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param title_y 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 y A vector with numerical values or a named list, which represents the observations for the vertical violinplot (y-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_y (Optional) The 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 (Optional) The 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.
#'
#' @return A string containing markdown code for the rendered component
violinplot <- function(object, y, group_by = NULL, title = NULL, title_y = NULL, title_group_by = NULL) {
......@@ -18,6 +18,13 @@ violinplot <- function(object, y, group_by = NULL, title = NULL, title_y = NULL
# Validate input
if(!all(sapply(y, is.numeric))) stop("'y' should only contain numerical 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
......
......@@ -142,16 +142,23 @@ plotly_boxplot <- function(x, group_by = NULL, title = "", group_by_title = NULL
showlegend = T)
}
#' Render a vertical violin plot with plotly.
#' Function to create a dataframe for plotly_violinplot.
#'
#' @param df A dataframe containing the data for the violinplot.
#' @param group_by A factor, by which observations can be grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.
#' @param title_y 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 y A list with the observations for the violinplot.
#' @param group_by (Optional) A list with factorial values, by which observations can optionally be grouped.
#' @param title_y (Optional) The title of the y-axis that describes the observations.
#' @param title_group_by (Optional) The title of the x-axis that describes the grouping factor.
#'
#' @return An object of class \code{plotly}.
#' @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 'split', the boolean value 'showlegend', the character string "title_y", the character string "title_group_by".
#' @export
plotly_violinplot <- function(df, group_by = NULL, title_y = NULL, title_group_by = NULL){
create_violinplot_df <- function(y, group_by = NULL, title_y = NULL, title_group_by = NULL){
# create data_frame
if(is.null(group_by)){
df <- data.frame(y)
} else {
df <- data.frame(y, group_by)
}
# manage the titles of axis
if(is.null(title_y)) title_y <- names(df[1]) else title_y <- title_y
if(is.null(title_group_by) & !is.null(group_by)) title_group_by <- names(df[2]) else title_group_by <- title_group_by
......@@ -164,9 +171,21 @@ plotly_violinplot <- function(df, group_by = NULL, title_y = NULL, title_group_b
x <- df[[2]]
showlegend <- T
}
y <- df[[1]]
plotly::plot_ly(x = x, y = y, split = x, type = 'violin',
return(list("df"=df, "x"=x, "y"=df[[1]], "split"=x, "showlegend"=showlegend, "title_y"=title_y, "title_group_by"=title_group_by))
}
#' Render a vertical violin plot with plotly.
#'
#' @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_y (Optional) The title of the y-axis that describes the observations.
#' @param title_group_by (Optional) The title of the x-axis that describes the grouping factor.
#'
#' @return An object of class \code{plotly}.
#' @export
plotly_violinplot <- function(..., showlegend = F, title_y = NULL, title_group_by = NULL){
plotly::plot_ly(..., type = 'violin',
box = list(visible = T),
meanline = list(visible = T)) %>%
plotly::layout(xaxis = list(title = title_group_by, showline = T),
......
......@@ -10,20 +10,14 @@ is_shiny <- identical(knitr::opts_knit$get("rmarkdown.runtime"), "shiny")
```
```{r, eval=!is_shiny}
# set variables
y <- {{ env_id }}$y[1]
group_by <- {{ env_id }}$group_by[1]
if(is.null(group_by)){
df <- data.frame(y)
} else {
df <- data.frame(y, group_by)
}
# Function to create a dataframe for bar plot
output_list <- i2dash.scrnaseq::create_violinplot_df(y = {{ env_id }}$y[1], group_by = {{ env_id }}$group_by[1], title_y = {{ env_id }}$title_y, title_group_by = {{ env_id }}$title_group_by)
# Provide data download
i2dash::embed_var(df)
i2dash::embed_var(output_list$df)
# Render plot
i2dash.scrnaseq::plotly_violinplot(df = df, group_by = {{ env_id }}$group_by, title_y = {{ env_id }}$title_y, title_group_by = {{ env_id }}$title_group_by)
i2dash.scrnaseq::plotly_violinplot(x = output_list$x, y = output_list$y, split = output_list$split, showlegend = output_list$showlegend, title_y = output_list$title_y, title_group_by = output_list$title_group_by)
```
```{r, eval=is_shiny}
......@@ -37,7 +31,7 @@ if ({{ env_id }}$y_selection){
}
# selection field for group_by
if ({{ env_id }}$y_selection){
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)))
......@@ -47,7 +41,7 @@ if ({{ env_id }}$y_selection){
ui_list <- rlist::list.append(ui_list, tags$div(tags$br(), downloadButton('downloadData_{{ env_id }}', 'Download data')))
#
# Create reactive dataframe
# Create reactive method
#
df_{{ env_id }} <- shiny::reactive({
......@@ -62,15 +56,9 @@ df_{{ env_id }} <- shiny::reactive({
} else {
group_by <- {{ env_id }}$group_by[input$select_group_by_{{ env_id }}]
}
if(is.null(group_by)){
df <- data.frame(y)
} else {
df <- data.frame(y, group_by)
}
#print(head(df))
return(df)
})
return(i2dash.scrnaseq::create_violinplot_df(y = y, group_by = group_by, title_y = {{ env_id }}$title_y, title_group_by = {{ env_id }}$title_group_by))
})
#
# Download
......@@ -78,7 +66,7 @@ df_{{ env_id }} <- shiny::reactive({
output$downloadData_{{ env_id }} <- downloadHandler(
filename = paste('data-', Sys.Date(), '.csv', sep=''),
content = function(file) {
write.csv(df_{{ env_id }}(), file)
write.csv(df_{{ env_id }}()$df, file)
}
)
......@@ -86,9 +74,12 @@ output$downloadData_{{ env_id }} <- downloadHandler(
# Output
#
output$plot_{{ env_id }} <- plotly::renderPlotly(
i2dash.scrnaseq::plotly_violinplot(df = df_{{ env_id }}(), group_by = {{ env_id }}$group_by, title_y = {{ env_id }}$title_y, title_group_by = {{ env_id }}$title_group_by)
i2dash.scrnaseq::plotly_violinplot(x = df_{{ env_id }}()$x, y = df_{{ env_id }}()$y, split = df_{{ env_id }}()$split, showlegend = df_{{ env_id }}()$showlegend, title_y = df_{{ env_id }}()$title_y, title_group_by = df_{{ env_id }}()$title_group_by)
)
#
# Layout of component junk
#
shiny::fillCol(flex = c(NA, 1),
do.call(shiny::inputPanel, ui_list),
plotly::plotlyOutput("plot_{{ env_id }}", height = "100%")
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/visualization_functions.R
\name{create_violinplot_df}
\alias{create_violinplot_df}
\title{Function to create a dataframe for plotly_violinplot.}
\usage{
create_violinplot_df(y, group_by = NULL, title_y = NULL,
title_group_by = NULL)
}
\arguments{
\item{y}{A list with the observations for the violinplot.}
\item{group_by}{(Optional) A list with factorial values, by which observations can optionally be grouped.}
\item{title_y}{(Optional) The title of the y-axis that describes the observations.}
\item{title_group_by}{(Optional) The title of the x-axis that describes the grouping factor.}
}
\value{
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 'split', the boolean value 'showlegend', the character string "title_y", the character string "title_group_by".
}
\description{
Function to create a dataframe for plotly_violinplot.
}
......@@ -4,17 +4,17 @@
\alias{plotly_violinplot}
\title{Render a vertical violin plot with plotly.}
\usage{
plotly_violinplot(df, group_by = NULL, title_y = NULL,
plotly_violinplot(..., showlegend = F, title_y = NULL,
title_group_by = NULL)
}
\arguments{
\item{df}{A dataframe containing the data for the violinplot.}
\item{...}{these arguments are of either the form value or tag = value and should be valid for the 'plotly::plot_ly()' method.}
\item{group_by}{A factor, by which observations can be grouped. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{showlegend}{Boolean value that describes if the legend should be shown.}
\item{title_y}{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_y}{(Optional) The title of the y-axis that describes the observations.}
\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_group_by}{(Optional) The title of the x-axis that describes the grouping factor.}
}
\value{
An object of class \code{plotly}.
......
......@@ -10,15 +10,15 @@ violinplot(object, y, group_by = NULL, title = NULL, title_y = NULL,
\arguments{
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{y}{Numeric observations for the violinplot. In case of a named list, a dropdown menu will be provided in the interactive mode.}
\item{y}{A vector with numerical values or a named list, which represents the observations for the vertical violinplot (y-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 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_y}{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_y}{(Optional) The 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}{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_group_by}{(Optional) The 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.}
}
\value{
A string containing markdown code for the rendered component
......
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