May 30, 2020

Mimic Excel's Conditional Formatting in R


The DT package is an interface between R and the JavaScript DataTables library (RStudio DT documentation). In Example 3 (at this page) they show how to heatmap-format a table. This post modifies the example to
  1. format each column individually
  2. shade in green rather than red
  3. use base R syntax rather than piping
  4. omit the extra accoutrements of the displayed table (from the answer to this stackoverflow post), except
  5. include a title.
Here we generate data similar to that in Example 3, but with average values growing by column
set.seed(12345)
df = as.data.frame(
  cbind(round(rnorm(10, mean = 0), 3), 
  round(rnorm(10, mean = 4), 3), 
  round(rnorm(10, mean = 8), 3), 
  round(rnorm(10, mean = 16), 3), 
  round(rnorm(10, mean = 32), 3), 
  sample(0:1, 10, TRUE)))
Using the code in the example -- modified to green -- the darker values naturally appear in columns V4 and V5.


But that's not what we want.

For each column to have it's own scale, simply apply RStudio's algorithm to each column of df in a loop. The trick to notice is that formatStyle wants a datatable object as its first argument, and produces a datatable object as its result. Therefore, start off with a plain-Jane datatable and successively format each column, saving the result each time. Almost like building a ggplot. At the end, view the final result.
# Start with a (relatively) plain, unformatted datatable object
dt <- DT::datatable(df, 
                    options = list(dom = 't', ordering = FALSE),
                    caption = "Example 3 By Column")
# Loop through the columns formatting according to that column's distribution
for (j in seq_along(df)) {
  # Create breaks for shading column values high to low
  brks <- stats::quantile(x <- df[[j]], probs = seq(.05, .95, .05), na.rm = TRUE)
  # Create shades of green for backgrounds
  y <- round(seq(255, 40, length.out = length(brks) + 1), 0)
  clrs <- paste0("rgb(", y, ", 255,", y, ")")
  # Format cells in j-th column
  dt <- DT::formatStyle(dt, j, backgroundColor = DT::styleInterval(brks, clrs))
}
dt

Actuaries in the crowd might recognize the image at the top of the post as the table of link ratios from the GenIns dataset in the ChainLadder package. There do not appear to be any distinctive trends in the ratios by age.




May 15, 2020

How to Add a Vignette to a Package in RStudio



R package vignettes are user-friendly ways to demo your package's capabilities. They can also be helpful documentation for your own reference when modifying the package in the future  -- "what was I thinking there?"

The easiest way to create a vignette in RStudio is using File | New File | R Markdown | From Template | Package Vignette (HTML). Write your RMarkdown document. {Note: Be sure to copy the "title" of your vignette to where "Vignette Title" shows in the section below:
vignette: >
  %\VignetteIndexEntry{Vignette Title}
}

After that, build your package. But if you use the typical procedure for developing a package in RStudio, your vignette will not show up. [Pulling hair out] Let me explain.

My typical procedure for building package myPackage in RStudio is to use two menu items in the Build pane
  • Check, then
  • Install and Restart
Check makes sure everything checks out correctly. This includes processing vignettes in the myPackage/vignettes directory, which you can see in multiple areas of Check's output in that pane:
...
-  installing the package to build vignettes
v  creating vignettes (7.4s)
...
* checking files in 'vignettes' ... OK
...
* checking for unstated dependencies in vignettes ... OK
* checking package vignettes in 'inst/doc' ... OK
* checking re-building of vignette outputs ... OK
...
When R CMD check succeeded appears at the bottom of that output, click Install and Restart in the Build pane again and myPackage will be recompiled, reloaded, and ready to go ... all except for the vignettes, because when you look for them, they won't be there:
> browseVignettes("myPackage")
No vignettes found by browseVignettes("myPackage")
> 
It turns out, according to RStudio's slightly-outdated online documentation this behavior is to be expected because, presumably, developers don't want to worry about vignettes in the typical code/Install-and-Restart/re-code development cycle [I get that]; to wit: 
RStudio’s “Build & reload” does not build vignettes to save time.
{Note: "Build and reload" has been renamed "Install and Restart" in today's RStudio Version 1.2.5042.}

In any event, the correct workflow was pointed out in 2018 by user2554330 in a stackoverflow post
No. Go to the Build pane, then in the "More" dropdown, choose "Build Source Package". This will create a file with extension .tar.gz, which you can distribute to others. You install it using "Packages | Install | Install from... | Package Archive file". – user2554330 Mar 25 '18 at 19:10
That worked great. Thank you RStudio for software that tends to simply get it done one way or another, and thank you user2554330 for explaining how to get it done for vignettes.