-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
166 lines (117 loc) · 3.85 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# DFplyr
<!-- badges: start -->
<!-- badges: end -->
The goal of DFplyr is to enable `dplyr` and `ggplot2` support for
`S4Vectors::DataFrame` by providing the appropriate extension methods. As row
names are an important feature of many Bioconductor structures, these are
preserved where possible.
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("jonocarroll/DFplyr")
```
You can install from [Bioconductor](https://bioconductor.org) with:
``` r
if (!require("BiocManager", quietly =TRUE))
install.packages("BiocManager")
# The following initializes usage of Bioc devel
BiocManager::install(version='devel')
BiocManager::install("DFplyr")
```
## Examples
First create an S4Vectors `DataFrame`, including S4 columns if desired
```{r}
library(S4Vectors)
m <- mtcars[, c("cyl", "hp", "am", "gear", "disp")]
d <- as(m, "DataFrame")
d$grX <- GenomicRanges::GRanges("chrX", IRanges::IRanges(1:32, width = 10))
d$grY <- GenomicRanges::GRanges("chrY", IRanges::IRanges(1:32, width = 10))
d$nl <- IRanges::NumericList(lapply(d$gear, function(n) round(rnorm(n), 2)))
d
```
This will appear in RStudio's environment pane as a
```
Formal class DataFrame (dplyr-compatible)
```
when using `DFplyr`. No interference with the actual object is required, but
this helps identify that `dplyr`-compatibility is available.
`DataFrame`s can then be used in `dplyr` calls the same as `data.frame` or
`tibble` objects. Support for working with S4 columns is enabled provided they
have appropriate functions. Adding multiple columns will result in the new
columns being created in alphabetical order
```{r}
library(DFplyr)
mutate(d, newvar = cyl + hp)
mutate(d, nl2 = nl * 2)
mutate(d, length_nl = lengths(nl))
mutate(d,
chr = GenomeInfoDb::seqnames(grX),
strand_X = BiocGenerics::strand(grX),
end_X = BiocGenerics::end(grX)
)
```
the object returned remains a standard `DataFrame`, and further calls can be
piped with `%>%`
```{r}
mutate(d, newvar = cyl + hp) %>%
pull(newvar)
```
Some of the variants of the `dplyr` verbs also work
```{r}
mutate_if(d, is.numeric, ~ .^2)
mutate_if(d, ~ inherits(., "GRanges"), BiocGenerics::start)
```
Use of `tidyselect` helpers is limited to within `dplyr::vars()` calls and using
the `_at` variants
```{r}
mutate_at(d, vars(starts_with("c")), ~ .^2)
select_at(d, vars(starts_with("gr")))
```
Importantly, grouped operations are supported. `DataFrame` does not
natively support groups (the same way that `data.frame` does not) so these
are implemented specifically for `DFplyr`
```{r}
group_by(d, cyl, am)
```
Other verbs are similarly implemented, and preserve row names where possible
```{r}
select(d, am, cyl)
arrange(d, desc(hp))
filter(d, am == 0)
slice(d, 3:6)
group_by(d, gear) %>%
slice(1:2)
```
`rename` is itself renamed to `rename2` due to conflicts between {dplyr} and
{S4Vectors}, but works in the {dplyr} sense of taking `new = old` replacements
with NSE syntax
```{r}
select(d, am, cyl) %>%
rename2(foo = am)
```
Row names are not preserved when there may be duplicates or they don't make
sense, otherwise the first label (according to the current de-duplication
method, in the case of `distinct`, this is via `BiocGenerics::duplicated`). This
may have complications for S4 columns.
```{r}
distinct(d)
group_by(d, cyl, am) %>%
tally(gear)
count(d, gear, am, cyl)
```
## Coverage
Most `dplyr` functions are implemented with the exception of `join`s.
If you find any which are not, please [file an issue](https://github.com/jonocarroll/DFplyr/issues/new).