The next set of tutorials focuses on more advanced functionality in distr6. This is not to say that the code itself may be any more complex, but that the use-cases for this functionality is more advanced. Examples of these include composite distributions, decorators for numeric results and piping methods. We start with S3 and piping as these (especially S3) will be familiar concepts to most users, are available in distr6 but are not part of the core functionality.

Using the R62S3 package (Sonabend 2019) most R6 methods in distr6 can instead be called as an S3 method. Even if you’re not aware of it, you’ve probably used S3 every time you’ve opened R. For example the `plot`

function produces a different plot depending on what object it’s called on, a numeric, a linear regression model, or something more complicated, this is because of the S3 dispatch system. Every distribution, kernel, parameter set, wrapper and decorator method is available through S3. Note that constructors always use R6 syntax, i.e. `$new()`

. Below are some examples, see the documentation help pages for the different S3 and R6 syntax but remember these are almost identical except that the first argument in S3 will be the object whereas this is omitted in the R6 usage.

```
N <- Normal$new()
N$pdf(1:2) # R6
#> [1] 0.24197072 0.05399097
pdf(N, 1:2) # S3
#> [1] 0.24197072 0.05399097
N$summary()
#> Normal Probability Distribution. Parameterised with:
#> c("mean", "var") = c(0, 1)
#>
#> Quick Statistics
#> Mean: 0
#> Variance: 1
#> Skewness: 0
#> Ex. Kurtosis: 0
#>
#> Support: ℝ Scientific Type: ℝ
#>
#> Traits: continuous; univariate
#> Properties: symmetric; mesokurtic; no skew
summary(N)
#> Normal Probability Distribution. Parameterised with:
#> c("mean", "var") = c(0, 1)
#>
#> Quick Statistics
#> Mean: 0
#> Variance: 1
#> Skewness: 0
#> Ex. Kurtosis: 0
#>
#> Support: ℝ Scientific Type: ℝ
#>
#> Traits: continuous; univariate
#> Properties: symmetric; mesokurtic; no skew
N$kurtosis()
#> [1] 0
kurtosis(N)
#> [1] 0
setParameterValue(N,var = 2)
getParameterValue(N, "var")
#> [1] 2
```

We won’t go into too much detail about piping because it will be familiar to users who need to make use of it and may not be necessary for others. Piping is the process of calling functions in a set order using the pipe, `%>%`

notation, from the magrittr package (Milton Bache and Wickham 2014). We specifically mention piping because it goes hand-in-hand with method chaining, which we discussed in the introduction to R6 tutorial. Below are some examples of how piping can be used in distr6.

```
library(magrittr)
Normal$new() %>% setParameterValue(var = 2) %>% getParameterValue("var")
#> [1] 2
# Don't worry about unfamiliar functions used here, we return to this in the next tutorial
Normal$new() %>% decorate(ExoticStatistics) %>% hazard(2)
#> Norm is now decorated with ExoticStatistics
#> [1] 2.373216
```

Notice that usually the S3 methods require the first argument to be the object, in this case `N`

or `Normal$new()`

but by using the pipe notation, this is filled automatically. For a more in-depth tutorial on magrittr see here.

In this tutorial we covered using S3 and piping instead of/as well as R6 syntax in distr6. These allow you to use whichever syntax you’re most comfortable with whilst maintaining the integrity of a well-designed and (hopefully) user-friendly object-oriented interface. In the next tutorial we look at another big feature in distr6, decorators.

Milton Bache, Stefan, and Hadley Wickham. 2014. “Magrittr: A Forward-Pipe Operator for R.” CRAN. https://CRAN.R-project.org/package=magrittr.

Sonabend, Raphael. 2019. “R62S3: Automatic Method Generation from R6.” CRAN. https://CRAN.R-project.org/package=R62S3.