knitr::opts_chunk$set( message = FALSE, warning = FALSE, fig.height=5, fig.width=5, # results='hide', # fig.keep='none', fig.path='images/datasets-', echo=TRUE, collapse = TRUE, comment = "#>" )
set.seed(1071) options(width=80, digits=5, continue=" ") library(heplots) library(candisc) library(ggplot2) library(dplyr) library(tidyr)
Datasets used in package examples are such an important part of making a package understandable and usable, but is often overlooked.
In developing the heplots package I collected a large collection of data sets illustrating a
variety of multivariate linear models with some an analyses, and graphical displays. Each of these have much more than the
usual stub examples, that often look like:
data(dataset) # str(dataset); plot(dataset)
But .Rd, and now roxygen, don't make it easy to work with numerous datasets in a package, or, more importantly, to document what they illustrate. I'm showing the work to create this vignette, in case these ideas are useful to others.
In this release, I started with a file generated by:
vcdExtra::datasets("heplots") |> head(4)
Then, in the roxygen documentation, I added @concept tags to classify these datasets according to methods used.
(@concept entries are indexed with the package, so they work via help.search())
For example,
the documentation for the AddHealth data contains these lines:
#' @name AddHealth #' @docType data ... #' @keywords datasets #' @concept MANOVA #' @concept ordered
With standard
processing, these concepts along with the keywords, appear in the Index section of the manual constructed by devtools::build_manual. In the pkgdown
site for this package, they are also searchable in the search box.
With a bit of extra processing, I created a dataset datasets.csv used below.
How this is done is described in this StackExchange question. The first step is to extract the concept tags from the man/*.Rd files:
#| eval: false concepts <- readLines(pipe("grep concept man/*.Rd")) %>% grep("concept{", ., fixed = TRUE, value = TRUE) %>% read.table(text = ., sep = "{", comment.char = "}", col.names = c("dataset", "tags")) %>% separate(dataset, c(NA, "dataset"), extra = "drop") %>% summarize(tags = paste(tags, collapse = " "), .by = dataset) %>% arrange(dataset) head(concepts)
Then, I use vcdExtra::datasets to extract title and other information for the datasets in the package, munge the result and left_join with concepts:
#| eval: FALSE dsets <- vcdExtra::datasets("heplots")[, c("Item", "dim", "Title")] rowcols <- as.data.frame(stringr::str_split_fixed(dsets$dim,"x", 2)) colnames(rowcols) <- c("rows", "cols") dsets <- cbind(dsets, rowcols) |> rename(dataset = Item) |> select(-dim) |> relocate(c(rows, cols), .after=dataset) |> left_join(concepts, by = "dataset")
For use in pkgdown documentation, I also turn the name of the dataset into a hyperlink to the documentation.
#| eval: FALSE refurl <- "http://friendly.github.io/candisc/reference/" dsets <- dsets |> mutate(dataset = glue::glue("[{dataset}]({refurl}{dataset}.html)"))
The main methods used in the example datasets are shown in the table below:
In addition, a few examples illustrate special handling for linear hypotheses concerning factors:
The dataset names are linked to the documentation with graphical output on the
pkgdown website, [http://friendly.github.io/heplots/].
library(here) library(dplyr) library(tinytable) #dsets <- read.csv(here::here("extra", "datasets.csv")) # doesn't work in a vignette dsets <- read.csv("https://raw.githubusercontent.com/friendly/heplots/master/extra/datasets.csv") dsets <- dsets |> dplyr::select(-X) |> arrange(tolower(dataset)) # link dataset to pkgdown doc refurl <- "http://friendly.github.io/heplots/reference/" dsets <- dsets |> mutate(dataset = glue::glue("[{dataset}]({refurl}{dataset}.html)")) #knitr::kable(dsets) tinytable::tt(dsets) |> format_tt(markdown = TRUE)
This table can be inverted to list the datasets that illustrate each concept:
concepts <- dsets |> select(dataset, tags) |> tidyr::separate_longer_delim(tags, delim = " ") |> arrange(tags, dataset) |> summarize(datasets = toString(dataset), .by = tags) |> rename(concept = tags) #knitr::kable(concepts) tinytable::tt(concepts) |> format_tt(markdown = TRUE)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.