Viliam Simko, FZI
19.11.2015
IPE-Akademie
In your RStudio:
File \( \rightarrow \)
New Project... \( \rightarrow \)
New Directory \( \rightarrow \)
R Package
| Dir Entry | Purpose |
|---|---|
R/ |
all your R-code lives here |
data/ |
all your data belongs to us |
tests/ |
guess what ? |
man/ |
generated docs, do not touch |
NAMESPACE |
do not touch either |
DESCRIPTION |
metadata |
LICENSE |
pick your favourite license |
README.md |
text for github |
mypkg.Rproj |
RStudio project configuration |
.travis.yml |
Travis-CI configuration file |
.Rbuildignore |
RStudio build-related files |
.gitignore |
list of files to ignore by git |
DESCRIPTION file contains metadata
Package: mypackage
Title: What The Package Does (one line)
Version: 0.1
Author: John Doe
Description: What the package does (paragraph)
License: MIT License + file LICENSE
LazyData: true
Depends: R (>= 3.1.0)
Imports: assertthat, zoo, ggplot2
Suggests: testthat, dplyr (>= 0.3.0.1), lintr
… describes dependencies
\( \rightarrow \) library(), require(), source() not used
R/@export, @import and @importFrom to all functions@examplesroxygen tool (CTRL+SHIFT+D in RStudio)roxygen generates:
man/*.Rd (manual pages)NAMESPACE file (from @export, @import and @importFrom directives)#' Add together two numbers.
#' @param x A number.
#' @param y A number.
#' @return The sum of \code{x} and \code{y}.
#' @examples
#' add(1, 1)
#' add(10, 1)
#' @export
add <- function(x, y) {
x + y
}
… this will produce man/add.Rd entry
testhat packagetests/testthat$ ls tests/
testthat/
testthat.R
$ ls tests/testthat/
test-behaviour1.R
test-behaviour2.R
...
context("some component")
pdf(NULL) # suppress generating any PDFs
test_that("Signal decomposition", {
sample32 %>% decompose_traffic %>%
.$coef %>% get_total_intervals %>%
expect_equal(6)
})
test_that("Plotting should work", {
expect_null( sample32 %>% plot_traffic )
})
test_that("Error for unsupported plot type", {
expect_error(
plot.biwavelet(some_wt, type = "dummy"),
regexp = "should be one of" )
})
After pressing CTRL+SHIFT+T in RStudio
==> devtools::test()
Loading biwavelet
Loading required package: testthat
biwavelet 0.17.11.6 loaded.
Testing biwavelet
lintr static code analysis : .
Performance optimizations : ...................
plot.biwavelet : ........
Compute wavelet (wt.bases) : ................
Significance of wavelet coherence (wtc.sig) : .....
Significance of wavelet transform (wt.sig) : .......
Cross-wavelet (xwt) : ....
DONE
install.packages("lintr")
lintr::lint_package() # or single file: lint("R/myfile.R")
… some checks already built into RStudio (version > 0.99.206)
# in file tests/testthat/lintr-style.R
if (requireNamespace("lintr", quietly=TRUE)) {
context("lints")
test_that("Package Style", {
lintr::expect_lint_free()
})
}
1. Failure: Package Style ----------------------------------
Not lint free
R/RcppExports.R:23:11: style: Only use double-quotes.
.Call('biwavelet_rcpp_wt_bases_dog', PACKAGE = 'bi..
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
R/RcppExports.R:45:1: style: lines should not be more than 80 characters.
.Call('biwavelet_rcpp_wt_bases_morlet', PACKAGE = 'bi..
...
README.md and pick a LICENSE
README.md
[](https://travis-ci.org/vsimko/biwavelet)
.travis.yml
language: r
warnings_are_errors: true
sudo: required
cache: apt
notifications:
email:
on_success: change
on_failure: change
r_github_packages:
- jimhester/covr
after_success:
- Rscript -e 'library(covr);codecov()'
Coverity scan for C/C++ code – https://scan.coverity.com/
Markdown is designed to be easy to write and read
From R code to R package
========================================
author: Viliam Simko, FZI
date: 19.11.2015
transition: fade
width: 1200
height: 800
autosize: false
Presentation in Markdown
========================================
Markdown is designed to be **easy** to
write and read
...
.onLoad() and .onAttach()
.onAttach <- function(libname, pkgname) {
packageStartupMessage("Welcome to my pkg")
}