Skip to content

Commit

Permalink
Add travis, tests, coverage, more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan committed Jan 17, 2018
1 parent 4ea63b9 commit 45285c6
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 29 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
^README-.*\.png$
^data-raw$
^docs$
^codecov\.yml$
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: r
cache: packages

r_github_packages:
- r-lib/covr

after_success:
- Rscript -e 'covr::codecov()'
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ Imports:
colorspace,
magrittr,
htmlwidgets
Suggests:
Suggests:
knitr,
testthat,
rmarkdown,
TCGAbiolinks,
gdata,
SummarizedExperiment,
edgeR,
pkgdown
pkgdown,
covr
VignetteBuilder: knitr
RoxygenNote: 6.0.1.9000
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ document:

install:
R --no-save --slave -e 'devtools::install()'

test:
R --no-save --slave -e 'devtools::test()'
7 changes: 7 additions & 0 deletions R/basic.R → R/boxplot.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#' A boxplot with jittered points
#' @param x A data.frame
#' @param box_hoverinfo Show hoverinfo for the boxplot trace? (logical)
#' @param colors The colors to be used for the x-axis groups
#' @return A plotly object
#' @examples
#' plotly_boxplot(iris[, -5])
#' @export
plotly_boxplot <- function(x,
box_hoverinfo = FALSE,
Expand Down
25 changes: 18 additions & 7 deletions R/lineplot.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
## https://stackoverflow.com/questions/48096600/can-i-make-a-line-plot-with-a-dropdown-menu-in-r-without-using-shiny-or-plotly/
#' A line plot with selectable y variables
#' @param df A data.frame
#' @param x The column name (as a string) of the x variable
#' @param yvars The column names (as strings) to be used as y variables
#' @return A plotly object
#' @examples
#' set.seed(42)
#' df <- lapply(1:26, function(x) rnorm(50)) %>%
#' as.data.frame()
#' colnames(df) <- letters
#' df[["seq"]] <- 1:50
#' df %>% dropdown_lineplot(x="seq", yvars = letters)
#' @export
dropdown_lineplot <- function(df, x) {
dropdown_lineplot <- function(df, x, yvars) {
## Add trace directly here, since plotly adds a blank trace otherwise
cols <- setdiff(colnames(df), x)
p <- plot_ly(
type = "scatter",
mode = "lines",
x = ~ df[[x]],
y = ~ df[[cols[[1]]]],
name = cols[[1]])
y = ~ df[[yvars[[1]]]],
name = yvars[[1]])
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[-1]) {
for (col in yvars[-1]) {
p <- p %>% add_lines(
x = ~ df[[x]],
y = df[[col]],
Expand All @@ -28,9 +39,9 @@ dropdown_lineplot <- function(df, x) {
list(
y = 0.7,
## Add all buttons at once
buttons = lapply(cols, function(col) {
buttons = lapply(yvars, function(col) {
list(method="restyle",
args = list("visible", cols == col),
args = list("visible", yvars == col),
label = col)
})
)
Expand Down
35 changes: 21 additions & 14 deletions R/linked_scatterplot.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#' linked_scatterplot
#'
#'
#' A scatterplot with links
#'
#' @param x x values
#' @param y y values
#' @param groups Groups to color the points by.
#' @param text Hovertext for the points
#' @param title Plot title
#' @param xlab,ylab X/Y axis title
#' @param xlim,ylim Range of X/Y axis
#' @param colors Colors to use for the groups
#' @examples
#' linked_scatterplot(
#' 1:10,
#' 1:10,
#' rep("https://www.google.com", 10),
#' 1:10,
#' 1:10,
#' rep("https://www.google.com", 10),
#' groups=sample(letters[1:2], 10, replace=TRUE))
#' @importFrom assertthat assert_that
#' @export
linked_scatterplot <- function(x,
y,
links,
groups=rep("1", length(x)),
linked_scatterplot <- function(x,
y,
links,
groups=rep("1", length(x)),
text = rep("", length(x)),
title="",
xlab="",
Expand All @@ -27,15 +34,15 @@ linked_scatterplot <- function(x,
length(text) == length(y),
length(colors) == length(unique(groups)))

createWidget("linked_scatterplot",
x = list(x=x,
createWidget("linked_scatterplot",
x = list(x=x,
xlab=xlab,
xlim=xlim,
y=y,
y=y,
ylab=ylab,
ylim=ylim,
links=links,
groups=groups,
links=links,
groups=groups,
colors=colors,
title=title,
text=text),
Expand Down
84 changes: 82 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,87 @@ output:
plotlytalk
============

This package contains a talk and related functions for an Edinbr meeting.
This package contains a talk and related functions for an talk about [plotly](https://plot.ly/).

Installation
============
```{r, eval=FALSE}
devtools::install_github("alanocallaghan/plotlytalk")
```
```{r, echo=FALSE}
suppressPackageStartupMessages(library(plotlytalk))
suppressPackageStartupMessages(library(SummarizedExperiment))
```

Outline
-------
=======

The talk shows many examples of [ggplotly](https://www.rdocumentation.org/packages/plotly/versions/4.7.1/topics/ggplotly), some examples of how to plot using plotly, several functions which create custom plotly plots, and
a custom htmlwidget written using JavaScript.

The custom plots are shown below:

## A boxplot with jittered points
```{r}
plotly_boxplot(iris[, -5])
```

## A line plot with selectable y variable
```{r}
set.seed(42)
df <- lapply(1:26, function(x) rnorm(50)) %>%
as.data.frame()
colnames(df) <- letters
df[["seq"]] <- 1:50
df %>% dropdown_lineplot(x="seq", yvars = letters)
```


## A density plot of a matrix or data.frame
```{r}
plotly_density(
log2(assay(GBMdata[, 1:10]) + 0.5),
palette=colorspace::rainbow_hcl,
xlab = "log<sub>2</sub>(reads + 0.5)") %>%
layout(showlegend = FALSE)
```

## A scatter plot wherein clicking on a point opens a link in a new tab
```{r}
set.seed(42)
tt <- GBMtopTable[sample(seq_len(nrow(GBMtopTable)), 1000), ]
linked_scatterplot(
x = tt[["logFC"]],
xlab = "log<sub>2</sub>(fold-change)",
y = -log10(tt[["adj.P.Val"]]),
ylab = "-log<sub>10</sub>(FDR-adjusted p-value)",
xlim = c(-max(abs(tt[["logFC"]])), max(abs(tt[["logFC"]]))) * 1.1,
text = tt[["Text"]],
links = tt[["Links"]],
groups = tt[["Group"]],
title = "Glioblastoma - IDH1 mutant vs wt",
colors = c("#0000ff", "#000000", "#ff0000"))
```



Useful links
============

- [The site for this talk](https://alanocallaghan.github.io/plotlytalk/)
- [github repository](https://github.com/Alanocallaghan/plotlytalk/)
- [plotly (CRAN)](https://cran.r-project.org/web/packages/plotly/index.html)
- [plotly for R (book)](https://plotly-book.cpsievert.me)
- [plotly (R reference)](https://plot.ly/r/reference/)
- [plotly (JavaScript reference)](https://plot.ly/javascript/reference/)
- [plotly (github)](https://github.com/ropensci/plotly/)
- [Carson Sievert's github (plotly R package maintainer)](https://github.com/cpsievert/)
- [heatmaply (CRAN)](https://github.com/talgalili/heatmaply)
- [heatmaply (github)](https://cran.r-project.org/web/packages/plotly/index.html)
- [heatmaply (publication)](https://academic.oup.com/bioinformatics/advance-article/doi/10.1093/bioinformatics/btx657/4562328)
- [iheatmapr (CRAN)](https://cran.r-project.org/web/packages/iheatmapr/index.html)
- [iheatmapr (github)](https://github.com/ropensci/iheatmapr)

1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comment: false
29 changes: 29 additions & 0 deletions man/dropdown_lineplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions man/linked_scatterplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/plotly_boxplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(plotlytalk)

test_check("plotlytalk")
43 changes: 43 additions & 0 deletions tests/testthat/test_plotlytalk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
context("plotlytalk")


test_that("boxplot works", {
expect_is(plotly_boxplot(iris[, -5]), "plotly")
})
test_that("density plot works", {
expect_is(
plotly_density(
log2(GBMdata@assays[[1]][, 1:10]) + 0.5),
palette=colorspace::rainbow_hcl,
xlab = "log<sub>2</sub>(reads + 0.5)") %>%
layout(showlegend = FALSE),
"plotly")
})

test_that("dropdown lineplot works", {
set.seed(42)
df <- lapply(1:26, function(x) rnorm(50)) %>%
as.data.frame()
colnames(df) <- letters
df[["seq"]] <- 1:50

p <- df %>% dropdown_lineplot(x="seq")
expect_is(p, "plotly")
})

test_that("linked scatterplot works", {
set.seed(42)
tt <- GBMtopTable[sample(seq_len(nrow(GBMtopTable)), 1000), ]
p <- linked_scatterplot(
x = tt[["logFC"]],
xlab = "log<sub>2</sub>(fold-change)",
y = -log10(tt[["adj.P.Val"]]),
ylab = "-log<sub>10</sub>(FDR-adjusted p-value)",
xlim = c(-max(abs(tt[["logFC"]])), max(abs(tt[["logFC"]]))) * 1.1,
text = tt[["Text"]],
links = tt[["Links"]],
groups = tt[["Group"]],
title = "Glioblastoma - IDH1 mutant vs wt",
colors = c("#0000ff", "#000000", "#ff0000"))
expect_is(p, "htmlwidget")
})
2 changes: 1 addition & 1 deletion vignettes/5_line_plots.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ df %>% plot_ly(x = ~seq, y = ~b, type = "scatter", mode = "lines")


```{r}
df %>% dropdown_lineplot(x="seq")
df %>% dropdown_lineplot(x="seq", yvars = letters)
```

0 comments on commit 45285c6

Please sign in to comment.