R Packages, Just as the Doctor Ordered

Gábor Csárdi

R Packages, Just as the Doctor Ordered

Gábor Csárdi

Outline

  1. Open source @ Mango
  2. Good practices for R packages
  3. Sneak peek 1: R docs in Markdown
  4. Sneak peek 2: surprise

github.com/ MangoTheCat

Why R packages?

What could (should?) be an R package?

We need to improve the tools

Existing
Tools

R CMD check

jimhester/covr

jimhester/lintr

goodpractice

goodpractice features

   Package building advice

   Test coverage (covr)

   Code style linting (lintr)

   Code complexity

   Anti-patterns, anti-functions

   Documentation quality (soon)

   Custom rule sets

   Use on a CI

xmlparsedata

Linting: labeled subgraph isomorphisms

Example: search for 1:nrow(<expression>)

<expr>
  +-- <expr>
    +-- NUM_CONST: 1
  +-- ':'
  +-- <expr>
    +-- <expr>
      +-- SYMBOL_FUNCTION_CALL nrow
    +-- '('
    +-- <expr>
    +-- ')'

Easy with XPath

Example: search for 1:nrow(<expr>)

//expr
  [expr[NUM_CONST[text()='1']]]
  [OP-COLON]
  [expr[expr[SYMBOL_FUNCTION_CALL[text()='nrow']]]]

Just convert the R parse tree to XML with xmlparsedata.

R docs in Markdown

w/o roxygen2

\name{make_style}
\alias{make_style}
\title{Create an ANSI color style}
\usage{
make_style(..., bg = FALSE, grey = FALSE, colors = num_colors())
}
\usage{
\arguments{
  \item{\dots}{\itemize{
    \item An R color name, see \code{\link{colors}}.
    \item A 6- or 8-digit hexa color string, e.g. \code{#ff0000} means
      red. Transparency (alpha channel) values are ignored.
    \item A one-column matrix with three rows for the red, green
      and blue channels, as returned by
      \code{\link[grDevices]{col2rgb}}.
  }}
  ...
}

w/ roxygen2

#' Create an ANSI color style
#'
#' @param ... Anything of the following:\itemize{
#' \item An R color name, see \code{\link{colors}}.
#' \item A 6- or 8-digit hexa color string, e.g. \code{#ff0000} means
#'   red. Transparency (alpha channel) values are ignored.
#' \item A one-column matrix with three rows for the red, green
#'   and blue channels, as returned by \code{\link[grDevices]{col2rgb}}.
#' }

make_style <- function(..., bg = FALSE, grey = FALSE,
  colors = num_colors()) {
...

w/ roxygen2 6.0.0 (soon)

#' Create an ANSI color style
#'
#' @param Anything of the following:
#'   * An R color name, see [colors].
#'   * A 6- or 8-digit hexa color string, e.g. `#ff0000` means
#'     red. Transparency (alpha channel) values are ignored.
#'   * A one-column matrix with three rows for the red, green
#'     and blue channels, as returned by [grDevices::col2rgb].

make_style <- function(..., bg = FALSE, grey = FALSE,
  colors = num_colors()) {
...

Like Shiny apps?

How do you test this app?

The shinytest package (WIP)

library(shinytest)

test_that("the kmeans app updates the plot", {
  app <- shinyapp$new("050-kmeans-example")

  expect_equal(app$get_value("xcol"), "Sepal.Length")
  expect_equal(app$get_value("ycol"), "Sepal.Width")
  expect_equal(app$get_value("clusters"), 3)

  expect_update(app, xcol = "Sepal.Width", output = "plot1")
  expect_update(app, ycol = "Petal.Width", output = "plot1")
  expect_update(app, clusters = 4, output = "plot1")
})

The shinytest package

library(shinytest)

test_that("table is rendered and updated", {
  app <- shinyapp$new("myapp")
  expect_update(app, dataset = "iris", output = "table")
  expect_equal(
    nrow(app$get_value("table")),
    5
  )
})

github.com/ MangoTheCat