---
title: "Introduction to the *fastverse*"
subtitle: "An Extensible Suite of High-Performance and Low Dependency Packages for Statistical Computing and Data Manipulation in R"
author: "Sebastian Krantz"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette

vignette: >
  %\VignetteIndexEntry{Introduction to the *fastverse*}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#" # comment = "#>" # tidy = FALSE #, out.width = '100%' #,  cache = FALSE
)
# https://github.com/r-lib/crayon/issues/24
options(repos = c(CRAN = "https://cran.rstudio.com/")) # crayon.enabled = TRUE

# Hooks needs to be set to deal with outputs
# thanks to fansi logic
# old_hooks <- fansi::set_knit_hooks(knitr::knit_hooks, 
#                                    which = c("output", "message", "error"))

.fv_styling <- FALSE # identical(Sys.getenv("PKGDOWN"), "TRUE")

options(fastverse.styling = .fv_styling)

NCRAN <- identical(Sys.getenv("NCRAN"), "TRUE")
```

The *fastverse* is an extensible suite of R packages, developed independently by various people, that jointly contribute to the objectives of:

- Speeding up R through heavy use of compiled code (C, C++, Fortran)
- Enabling more complex statistical and data manipulation operations in R
- Reducing the number of dependencies required for advanced computing in R

The *fastverse* installs 6 core packages (*data.table*, *collapse*, *matrixStats*, *kit*, *magrittr* and *fst*) that are (by default) harmonized and attached with `library(fastverse)`. These packages were selected because they provide high quality compiled code for most common statistical and data manipulation tasks, have carefully managed APIs, jointly depend only on base R and *Rcpp*, and work remarkably well together. 

```{r}
library(fastverse)
```

The *fastverse* package then provides functionality familiar from the *tidyverse* package, such as checking and reporting namespace clashes, and utilities for updating packages, listing dependencies etc...

```{r}
# Checking for any updates
fastverse_update()
```

A key feature of the *fastverse* is that it can liberally be extended with other packages that can be loaded and managed using the tools this package provides. 
<!-- 
While placing a clear emphasis on high-quality, high-performance and low-dependency code, as well as statistical rigor and broad based compatibility with the classes and objects most used in R - which warranted a selection of core packages and a number of [suggested extensions](<https://sebkrantz.github.io/fastverse/#suggested-extensions>) - the *fastverse* is not particularly opinionated about coding style, function nomenclature, or the format of inputs and outputs. -->
Users are encouraged to make of use the features described in the remainder of this vignette to extend the *fastverse* or even create 'verses' of packages that suit their personal analysis needs. A selection of [suggested packages](<https://sebkrantz.github.io/fastverse/#suggested-extensions>) is provided on the website^[Let me know about other packages you think should be featured there.]. 

<!-- These extensions can then be loaded and managed by the *fastverse* package, just like the core packages. -->

## Extending the *fastverse* for the Session
After the core packages have been attached with `library(fastverse)`, it is possible to extend the *fastverse* for the current session by adding any number of additional packages with `fastverse_extend()`. This will attach the packages and, by default, check for namespace clashes with attached packages, as well as among the added packages. 

```{r}
# Extend the fastverse for the session
fastverse_extend(xts, roll, fasttime)

# See that these are now part of the fastverse
fastverse_packages()

# They are also saved in a like-named option 
options("fastverse.extend")
```

All *fastverse* packages (or particular packages) can be detached using `fastverse_detach`.

```{r}
# Detaches all packages (including the fastverse) but does not (default) unload them
fastverse_detach()
```

For programming purposes it is also possible to pass vectors of packages to both `fastverse_extend` and `fastverse_detach`^[In particular, the `...` expression is first captured using `substitute(c(...))`, and then evaluated inside `tryCatch`. If this evaluation fails or did not result in a character vector, the expression is coerced to character.]. The defaults of `fastverse_detach` are set such that detaching is very 'light'. Packages are not unloaded and all *fastverse* options set for the session are kept.

```{r}
# Extensions are still here ...
options("fastverse.extend")

# Thus attaching the fastverse again will include them
library(fastverse)
```

'Harder' modes of detaching can be achieved using arguments `unload = TRUE` (and `force = TRUE`) to (forcefully) detach and unload *fastverse* packages, and/or `session = TRUE` which will clear all *fastverse* options set^[`options("fastverse.quiet")` and `options("fastverse.styling")` will only be cleared if all packages are detached. If selected packages are detached, they are removed from `options("fastverse.extend")`.]. 
```{r}
# Detaching and unloading all packages and clearing options
fastverse_detach(session = TRUE, unload = TRUE)
```

`fastverse_detach` can also be used to detach any other attached packages not part of the *fastverse*. 

Since `options("fastverse.extend")` keeps track of which packages were added to the *fastverse* for the current session, it is also possible to set it before loading the *fastverse* e.g. 
```{r, include=FALSE}
options(fastverse.styling = .fv_styling)
```
```{r}
options(fastverse.extend = c("dygraphs", "tidyfast"))
library(fastverse)

fastverse_detach(session = TRUE)
```
```{r, include=FALSE}
options(fastverse.styling = .fv_styling)
```

## Permanent Extensions

`fasvtverse_extend` and `fastverse_detach` both have an argument `permanent = TRUE` which can be used to make these changes persist across R sessions. This is implemented using a global configuration file saved to the package directory^[Thus it will be removed when the *fastverse* is reinstalled.]. 

For example, suppose most of my work involves time series analysis, and I would like to add *xts*, *zoo*, *roll*, and *dygraphs* to my *fastverse*. Let's say I also don't really use the *fst* file format, and I don't really need *matrixStats* either as I can do most of the time series statistics I need with base R and *collapse*. Let's finally say that I don't want `xts::first` and `xts::last` to mask `data.table::first` and `data.table::last`.

Then I could permanently modify my *fastverse* as follows^[I note that namespace conflicts can also be detected and handled with the [conflicted](<https://cran.r-project.org/package=conflicted>) package on CRAN.]: 

```{r, eval=NCRAN}
library(fastverse)

# Adding extensions
fastverse_extend(xts, zoo, roll, dygraphs, permanent = TRUE)

# Removing some core packages
fastverse_detach(data.table, fst, matrixStats, permanent = TRUE)

# Adding data.table again, so it is attached last
fastverse_extend(data.table, permanent = TRUE)
```

To verify our modification, we can see the order in which the packages are attached, and do a conflict check:

```{r, eval=NCRAN}
# This will be the order in which packages are attached
fastverse_packages(include.self = FALSE)

# Check conflicts to make sure data.table functions take precedence
fastverse_conflicts()
```

Note that `options("fastverse.extend")` is still empty, because we have written those changes to a config file^[When fetching the names of *fastverse* packages, `fastverse_packages` first checks any config file and then checks `options("fastverse.extend")`.]. Now lets see if our permanent modification worked: 

```{r, eval=NCRAN}
# detach all packages and clear all options
fastverse_detach(session = TRUE)
```
```{r, include=FALSE}
options(fastverse.styling = .fv_styling)
```
```{r}
library(fastverse) 
```

After this permanent modification, the *fastverse* can still be extend for the session:

```{r}
# Extension for the session
fastverse_extend(Rfast2, coop)

# These packages go here
options("fastverse.extend")

# This fetches packages from both the file and the option
fastverse_packages()
```

As long as the current installation of the *fastverse* is kept, these modifications will persist across R sessions. Needless to say this is not ideal as reinstallation of the *fastverse* will remove the config file. Therefore, the *fastverse* also offers a persistent and more flexible mechanism to configure it inside projects.

## Custom *fastverse* Configurations for Projects

You can put together a custom collection of packages for a project, and load / manage them with `library(fastverse)`. 

For this you need to include a configuration file named `.fastverse` (no file extension) inside a project directory, and place inside that file the names of packages to be loaded when calling `library(fastverse)`^[You can place package names in that file any manner you deem suitable: separated using spaces or commas, on one or multiple lines. Note that the file will be read from left to right and from top to bottom. Packages are attached in the order found in the file.]. Note that **all** packages to be loaded as core *fastverse* for your project need to be included in that file, in the order they should be attached.    

In addition, you can set global options and environment variables, either before or after the list of packages. Options must be prefixed with `_opt_` and environment variables with `_env_`, and either must be placed on separate lines. For example, including a `.fastverse` script like this:

```
_opt_collapse_mask = c("manip", "helper)
_opt_fastverse.install = TRUE

data.table, kit, magrittr, collapse, qs, fixest, 
ranger, robustbase, decompr

_opt_max.print = 100
_opt_kit.nThread = 4
_env_NCRAN = TRUE
```
in a project directory, and placing `library(fastverse)` at the top of an R script in the project will first set `options(collapse_mask = c("manip", "helper), fastverse.install = TRUE)`^[Setting `options(fastverse.install = TRUE)` before loading the packages will make sure any packages missing on your system will be installed beforehand. `options(collapse_mask = ...)` can be used to make base R and dplyr functions with faster versions provided in the *collapse* package. See `help("collapse-options")`.], then attach all the packages in the order provided, and then set `options(max.print = 100, kit.nThread = 4)` and `Sys.setenv(NCRAN = TRUE)`. Note that packages can be spread across multiple lines, but need to be together i.e. they cannot be separated by options. 

Using the *fastverse* to jointly load important packages and set important options can facilitate package management inside projects and serve as a bridge between loading packages individually and using more rigorous package and namespace management solutions such as [renv](<https://github.com/rstudio/renv>), [conflicted](<https://github.com/r-lib/conflicted>), [box](<https://github.com/klmr/box>) or [import](<https://github.com/rticulate/import>).

At the most basic level, loading packages with the *fastverse* displays the package versions and checks namespace conflicts, helping you spot issues that might arise as packages are updated. It also allows you to easily check the dependencies and update status of packages used in your project with `fastverse_sitrep()`, and update if necessary with `fastverse_update()`. 

<!-- 
You additionally have the option to call `fastverse::fastverse_install()` at the beginning of a project file before calling `library(fastverse)`. This will execute a swift check if all packages listed in `.fastverse` are available, and, by default, install any missing packages needed for the project. -->

Using a config file in a project will ignore any global configuration as discussed in the previous section. You can still extend the *fastverse* inside a project session using `fastverse_extend`, or `options("fastvers.extend")` before `library(fastverse)`^[If you populate `options("fastvers.extend")` before calling `library(fastverse)`, with `_opt_fastverse.install = TRUE` set in `.fastverse`, the availability of these packages will also be guaranteed.]. 

## Creating Separate Package-Verses

An added feature in version 0.2.0 if the *fastverse* is the ability to create wholly separate and fully customizable verses - with the `fastverse_child()` function. Let's say I would like to create a verse for time series analysis that I want to keep separate from the *fastverse*. This is easily done using e.g.

```{r, eval=FALSE}
fastverse_child(
  name = "tsverse", 
  title = "Time Series Package Verse", 
  pkg = c("xts", "roll", "zoo", "tsbox", "urca", "tseries", "tsutils", "forecast"), 
  maintainer = 'person("GivenName", "FamilyName", role = "cre", email = "your@email.com")',
  dir = "C:/Users/.../Documents", 
  theme = "tidyverse")
```

By default (`install = TRUE`, `keep.dir = TRUE`) the package is installed and a source directory is created under dir/name, allowing further edits to the package. Such *fastverse* children inherit 90% of the functionality of the *fastverse* package: they are not permanently globally extensible and can not bear children themselves, but can be configured for projects^[Using, in this case, a `.tsverse` config file.] and extended in the session. The function uses a prepared 'child' branch of the [GitHub repository](<https://github.com/SebKrantz/fastverse>), and thus does not require any further packages such as `devtools`.  

## Dependencies, Situational Reports and Updating

Just like it's *tidyverse* equivalent, `fastverse_deps()` (recursively) determines the joint dependencies of *fastverse* packages and also checks local versions against CRAN versions.

```{r, message=FALSE, warning=FALSE}
# Recursively determine the joint dependencies of the current fastverse configuration
fastverse_deps(recursive = TRUE) # Returns a data frame
```

Additional flexibility is offered by the `pkg` argument allowing dependency and update status checks for any other packages. `fastverse_sitrep()` displays the same information in a more elegant printout, also showing the version of R, and whether any global or project-level configuration files - as discussed in previous sections - are used. 

```{r}
# Check versions and update status of packages and dependencies
fastverse_sitrep() # default is recursive = FALSE
```

`fastverse_update()` can be used to (default) print an `install.packages()` statement to update *fastverse* packages and dependencies, or to install updates straight away (`install = TRUE`). In all three functions, `check.deps = FALSE` can be specified to exclude dependencies of *fastverse* packages, `recursive = TRUE` can be used to check all dependencies, and `include.self = TRUE` can be used to also check for updates of the `fastverse` package itself.

## Other *fastverse* Options

Apart from `"fastverse.extend"`, the *fastverse* also has options `"fastverse.install"`, `"fastverse.styling"` and `"fastverse.quiet"`^[I note that at this point in time it is not possible to permanently set `options("fastverse.quiet")` or `options("fastverse.styling")`.]. Setting `options(fastverse.install = TRUE)` before `library(fastverse)` will make sure any packages missing on your system will be installed beforehand. This can also be done ex-post using the `fastverse_install()` function. Setting `options(fastverse.styling = FALSE)` will disable coloured text printed to the R console (as done for this vignette). `options(fastverse.quiet = TRUE)` will omit any messages printed from `library(fastverse)` and `fastverse_extend()`:

```{r}
fastverse_detach()
options(fastverse.quiet = TRUE)
library(fastverse) # Nothing to see here

# This gives lots of function clashes with data.table, but they are not displayed in quiet mode
fastverse_extend(lubridate)
```

If you only want to omit a function clash check when calling `fastverse_extend`, you can also use `fastverse_extend(..., check.conflicts = FALSE)`. 

## Conclusion

The *fastverse* was developed principally for 2 reasons: to promote quality high-performance software development for R, and to provide a flexible approach to package loading and management in R, particularly for users wishing to combine various high-performance packages in statistical workflows. To the extent that high-performance software development in R continues to prioritize low-dependency and stable APIs, complex statistical and project workflows can be developed without sophisticated package management solutions.  

<!-- This vignette introduced the key features of the package. Suggested *fastverse* extension packages are introduced in the [README](<https://sebkrantz.github.io/fastverse/#suggested-extensions>) file. -->

```{r, eval=NCRAN}
# Resetting the fastverse to defaults (clearing all permanent extensions and options)
fastverse_reset()
# Detaching 
fastverse_detach()
```

```{r, eval=!NCRAN, include=FALSE}
# Detaching 
fastverse_detach()
```




