2 Kickstarting an R package
- Kickstart an R package
- Write functions
- Document functions
At any time, if you are lost or do not understand how functions in the proposed solution work, type ?<function>
in the R console and a help menu will appear.
You can also check the help tab in the corresponding quadrant.
2.1 Proposed package functionalities
In this section, we are going to create a package which aims at:
- Importing
.bigwig
coverage track files as well as.bed
files of genomic coordinates - Filtering imported
GRanges
to make sure they all are contained within the coverage tracks - Calculating the average (+/- quantiles) of coverage of the
.bigwig
file over theGRanges
of interest - Plot the aggregated
.bigwig
coverage over theGRanges
of interest
Eventually, the functions shipped in this package should be able to turn a pair of .bigwig
/.bed
files into this type of plot:
…
2.2 Starting a new package
To start developing a new package, one may want to make sure all useful developer resources are already set-up.
Now the time has come to create your very first package! To keep a coherent cohorte, we’ll create a package named <YOURNAME>TestPackage
. But first, you need to make sure that this package name is available.
If this name is available, let’s create your package.
When creating your package, devtools
+RStudio
combo automatically opens a new RStudio
window, with the package’s project automatically activated. Navigate to your package RStudio project window and re-open this instruction sheet.
The DESCRIPTION file automatically created does not exactly match Bioconductor requirements. You should replace it by a better-fitted DESCRIPTION file.
A nice touch is to systematically provide a README
file. This provides a good overview of your package when browsed e.g. in GitHub, and for non-R specialists, who don’t necessarily know about DESCRIPTION
and vignettes. And since you are developing an R package, let’s stick with the Rmd format for your README
.
That being said, package submission requires that a REAMDE.md
file only is present in the repository. A way to provide this is to 1) devtools::build_readme()
to parse README.Rmd
into README.md
(with R chunks processed!), and 2) add README.Rmd
to .gitignore.
A bunch of extra useful commands from bioc/usethis
can be performed at this stage, e.g. to add a NEWS file, a Code of Conduct, a citation file, etc… Beware, each command creates/modify one/several files. Pay attention to the comments printed in the R console!
2.3 Write functions to populate your package
To write your first package function, you can either create an R/<name>.R
file manually, or run usethis::use_r(name)
.
Now is time to write! Refer to the functionalities (descriped in Section 0) that we are aiming to provide in this package, and start writing 4 functions which, together, can fulfill these functionalities. Briefly, we suggest to design functions to:
import
a coverage track and a set of genomic features (scaled to a fixed width) together in a list;filter
the imported features to only retain those fully overlapping with covered genome segments (from the coverage track);compute
the mean coverage and its confidence interval, for each position within the features’ width;plot
the results withggplot2
.
The content of the proposed function files is shown in Share/Day2/functions/
. These files provides an example of implementation to fulfill our package’s requirements.
2.4 Add function documentation
Once you have documented each function, you can regenerate the documentation with document()
.