knitr::opts_chunk$set(
  collapse=TRUE,
  warning=FALSE,
  message=FALSE,
  comment="#>",
  fig.path="man/figures/README-"
);
options(knitr.table.format='markdown')

jamba

The goal of jamba is to provide useful custom functions for R data analysis and visualization.

Package Reference

A full online function reference is available via the pkgdown documentation:

Full jamba command reference

Functions are categorized, some examples are listed below:

Background

The R functions in jamba have been built up, used, tested, revised over several years. They are immediately useful for day-to-day work, and efficient and robust enough for production pipelines.

Many were inspired by discussion from Stackoverflow, R-help, or Bioconductor, with citations thanking principal author(s). Many thanks to the original authors! The R community is built upon the collective greatness of its contributors!

Most of the functions are designed around workflows for Bioinformatics analyses, where functions need to be efficient when operating over 10,000 to 100,000 elements. (They work quite well with millions as well.) Usually the speed gains are obvious with about 100 elements, then scale linearly (or worse) as the number increases. I and others use these functions all the time.

One example function writeOpenxlsx() is a simple wrapper around very useful openxlsx::write.xlsx(), which also applies column formatting for column types: P-values, fold changes, log2 fold changes, numeric, and integer values. Columns use conditional Excel formatting to apply color-shading to cells for each type.

Similarly, readOpenxlsx() is a wrapper function to openxlsx::read.xlsx() which reads each worksheet and returns a list of data.frame objects. It can detect multi-row column headers, for which it returns combined column names. It also applies equivalent of check.names=FALSE so column names are returned without change.

Small and large efficiencies are used wherever possible. The mixedSort() functions are based upon gtools::mixedsort(), with additional optimizations for speed and custom needs. It sorts chromosome names, gene names, micro-RNA names, etc.

Example R functions

Efficient alphanumeric sort

Example:

x <- sort(c(
  "miR-12","miR-1","miR-122","miR-1b",
  "miR-1a","miR-2", "miR-22",
  "ABCA2", "ABCA12"));
df1 <- data.frame(
  miRNA=x,
  sort_rank=seq_along(x),
  mixedSort_rank=order(jamba::mixedOrder(x)),
  check.names=FALSE,
  stringsAsFactors=FALSE);
df2 <- jamba::mixedSortDF(df1);
df2;

Base R plotting

These functions help with base R plots, in all those little cases when the amazing ggplot2 package is not a smooth fit.

require(jamba);
set.seed(123);
x <- matrix(ncol=2, data=rnorm(40000*2));
x[,2] <- x[,1] + rnorm(40000)*0.15;
x[1:3000,] <- t(t(x[1:3000,,drop=FALSE])+c(0.6,-0.7));
x[1:2000,2] <- x[1:2000,1] + rnorm(1000)*0.5;
opar <- par("mfrow"=c(1,2), "mar"=c(2.5, 2, 3, 0.5));
smoothScatter(x, main="smoothScatter()");
plotSmoothScatter(x, main="plotSmoothScatter()");
par(opar);
plotPolygonDensity(x[,1] - x[,2]);
set.seed(23);
opar <- par("mar"=c(1,1,1,1));
m <- matrix(sample(colors(), 9), ncol=3)
imageByColors(m, cellnote=m);
par(opar);
opar <- par("mar"=c(1,1,1,1));
nullPlot(fill="navy", plotAreaTitle="", doMargins=FALSE);
text <- shadowText;
drawLabels(txt="shadowText() label",
  boxColor=alpha2col("palegoldenrod", 0.6),
  labelCol=setTextContrastColor(alpha2col("palegoldenrod", 0.6),
    bg="navy"),
  x=1.6, y=1.3, labelCex=2);
rm(text);
drawLabels(txt="text() label",
  boxColor=alpha2col("palegoldenrod", 0.6),
  labelCol=setTextContrastColor(alpha2col("palegoldenrod", 0.6),
    bg="navy"),
  x=1.3, y=1.7, labelCex=2);
par(opar);

Excel export

Every Bioinformatician/statistician needs to write data to Excel, the writeOpenxlsx() function is consistent and makes it look pretty. You can save numerous worksheets in a single Excel file, without having to go back and custom-format everything.

Color

Almost everything uses color somewhere, especially on R console, and in every R plot.

opar=par("mar"=c(1,1,1,1));
rainbowv <- c("red","yellow","green","cyan","blue","magenta");
colorlist <- list(
  viridis_lens5=rep(each=2, getColorRamp("viridis", n=12, lens=5)),
  viridis=rep(each=2, getColorRamp("viridis", n=12)),
  `viridis_lens-5`=rep(each=2, getColorRamp("viridis", n=12, lens=-5)),
  RdBu_r_lens5=rep(each=2, getColorRamp("RdBu_r", n=11, lens=5)),
  RdBu_r=rep(each=2, getColorRamp("RdBu_r", n=11)),
  `RdBu_r_lens-5`=rep(each=2, getColorRamp("RdBu_r", n=11, lens=-5)),
  rainbow=rep(rainbowv, each=4),
  rainbow_gradient2=rep(each=2, color2gradient(rainbowv, n=2, gradientWtFactor=1/3)),
  rainbow_gradient4=color2gradient(rainbowv, n=4, gradientWtFactor=1/3)
)
showColors(colorlist, xaxt="n", labelCells=FALSE);
par(opar);

List

Cool methods to operate on super-long lists in one call, to avoid looping through the list either with for() loops, lapply() or map() functions.

Names

We use R names as an additional method to make sure everything is kept in the proper order. Many R functions return results using input names, so it helps to have a really solid naming strategy. For the R functions that remove names -- I highly recommend adding them back yourself!

data.frame/matrix/tibble

String / grep

Numeric

Practical / helpful

jargs(plotSmoothScatter)

R console

Other related Jam packages



jmw86069/jamba documentation built on March 26, 2024, 5:26 a.m.