Skip to content

Commit a1a38b4

Browse files
committed
adjust doc formatting to match other functions better
1 parent 759640d commit a1a38b4

21 files changed

+110
-105
lines changed

R/as_draws_rvars.R

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @template draws_format-skeleton
99
#' @template args-format-nchains
1010
#'
11-
#' @details Objects of class `"draws_rvars"` are lists of [rvar] objects. See **Examples**.
11+
#' @details Objects of class `"draws_rvars"` are lists of [`rvar`] objects. See **Examples**.
1212
#'
1313
NULL
1414

@@ -164,21 +164,27 @@ as_draws_rvars.mcmc.list <- function(x, ...) {
164164
#' @rdname draws_rvars
165165
#' @export
166166
draws_rvars <- function(..., .nchains = 1) {
167-
# TODO: should this be as_rvar or rvar? depends on desired constructor...
168-
out <- lapply(list(...), as_rvar)
167+
out <- lapply(list(...), function(x) {
168+
if (is_rvar(x)) x
169+
else rvar(x)
170+
})
171+
169172
if (!rlang::is_named(out)) {
170173
stop2("All variables must be named.")
171174
}
175+
172176
.nchains <- as_one_integer(.nchains)
173177
if (.nchains < 1) {
174178
stop2("Number of chains must be positive.")
175179
}
180+
176181
.ndraws <- ndraws(out[[1]])
177182
if (.ndraws %% .nchains != 0) {
178183
stop2("Number of chains does not divide the number of draws.")
179184
}
185+
180186
# TODO: store nchains somewhere, maybe as an attribute
181-
as_draws_rvars(out)
187+
.as_draws_rvars(out)
182188
}
183189

184190
class_draws_rvars <- function() {

R/rdo.R

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22
#'
33
#' Execute an expression to create a random variable.
44
#'
5-
#' @param expr A bare expression that can (optionally) contain [rvar]s. The expression
5+
#' @param expr A bare expression that can (optionally) contain [`rvar`]s. The expression
66
#' supports [quasiquotation].
7-
#' @param ndraws When no [rvar]s are supplied in `expr`, this is the number
7+
#' @param ndraws When no [`rvar`]s are supplied in `expr`, this is the number
88
#' of draws that will be used to construct new random variables. If `NULL`,
99
#' getOption("rvar_ndraws") is used (default 4000).
1010
#' @template args-rvar-dim
1111
#'
1212
#' @details This function evaluates `expr` possibly multiple times, once for each draw of
13-
#' the [rvar]s it contains, then returns a new [rvar] representing the output of those
14-
#' expressions. To identify [rvar]s, `rdo()` searches the calling environment for any variables
15-
#' named in `expr` for which [is_rvar()] evaluates to `TRUE`. If `expr` contains no [rvar]s,
16-
#' then it will be executed `ndraws` times and an [rvar] with that many draws returned.
13+
#' the [`rvar`]s it contains, then returns a new [`rvar`] representing the output of those
14+
#' expressions. To identify [`rvar`]s, `rdo()` searches the calling environment for any variables
15+
#' named in `expr` for which [is_rvar()] evaluates to `TRUE`. If `expr` contains no [`rvar`]s,
16+
#' then it will be executed `ndraws` times and an [`rvar`] with that many draws returned.
1717
#'
1818
#' `rdo()` is not necessarily *fast* (in fact in some cases it may be very slow), but
19-
#' it has the advantage of allowing nearly arbitrary R code to be executed against [rvar]s
19+
#' it has the advantage of allowing nearly arbitrary R code to be executed against [`rvar`]s
2020
#' simply by wrapping it with `rdo( ... )`. This makes it especially useful as a prototyping
2121
#' tool. If you create code with `rdo()` and it is unacceptably slow for your application,
22-
#' consider rewriting it in terms of math operations directly on [rvar]s (which should be fast),
23-
#' or in terms of operations directly on the arrays that back the [rvar]s, using [draws_of()].
22+
#' consider rewriting it in terms of math operations directly on [`rvar`]s (which should be fast),
23+
#' or in terms of operations directly on the arrays that back the [`rvar`]s, using [draws_of()].
2424
#'
25-
#' @return An [rvar].
25+
#' @return An [`rvar`].
2626
#'
2727
#' @examples
2828
#'

R/rfun.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#'
55
#' @param .f A function (or a one-sided formula representing a function that can be parsed by
66
#' [rlang::as_function()]) to turn into a function that accepts and/or produces random variables.
7-
#' @param rvar_args The arguments of `.f` that should be allowed to accept [rvar]s as arguments.
7+
#' @param rvar_args The arguments of `.f` that should be allowed to accept [`rvar`]s as arguments.
88
#' If `NULL` (the default), all arguments to `.f` are turned into arguments that accept
9-
#' [rvar]s.
10-
#' @param ndraws When no [rvar]s are supplied as arguments to the new function, this is the number
9+
#' [`rvar`]s.
10+
#' @param ndraws When no [`rvar`]s are supplied as arguments to the new function, this is the number
1111
#' of draws that will be used to construct new random variables. If `NULL`,
1212
#' `getOption("rvar_ndraws")` is used (default 4000).
1313
#'
14-
#' @details This function wraps an existing funtion (`.f`) such that it returns [rvar]s containing
14+
#' @details This function wraps an existing funtion (`.f`) such that it returns [`rvar`]s containing
1515
#' whatever type of data `.f` would normally return.
1616
#'
1717
#' @return A function with the same argument specification as `.f`, but which can accept and return
18-
#' [rvar]s.
18+
#' [`rvar`]s.
1919
#'
2020
#' @export
2121
rfun <- function (.f, rvar_args = NULL, ndraws = NULL) {

R/rvar-.R

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @name rvar
66
#'
77
#' @param x A vector or array where the first dimension represents draws from
8-
#' a distribution. The resulting [rvar] will have dimension `dim(x)[-1]`; that is,
8+
#' a distribution. The resulting [`rvar`] will have dimension `dim(x)[-1]`; that is,
99
#' everything except the first dimension is used for the shape of the variable, and the
1010
#' first dimension is used to index draws from the distribution.
1111
#' @template args-rvar-dim
@@ -80,13 +80,13 @@ new_rvar <- function(x = double()) {
8080

8181
#' Is `x` a random variables?
8282
#'
83-
#' Test if `x` is an [rvar()].
83+
#' Test if `x` is an [`rvar`].
8484
#'
8585
#' @param x An object
8686
#'
8787
#' @seealso [as_rvar()] to convert objects to `rvar`s.
8888
#'
89-
#' @return `TRUE` if `x` is an [rvar()], `FALSE` otherwise.
89+
#' @return `TRUE` if `x` is an [`rvar`], `FALSE` otherwise.
9090
#'
9191
#' @export
9292
is_rvar <- function(x) {
@@ -393,20 +393,20 @@ as.list.rvar <- function(x, ...) {
393393

394394
#' Get/set array of draws underlying a random variable
395395
#'
396-
#' Gets/sets the array-representation that backs an [rvar()]. Should be used rarely.
396+
#' Gets/sets the array-representation that backs an [`rvar`]. Should be used rarely.
397397
#'
398-
#' @param x An [rvar()]
398+
#' @param x An [`rvar`]
399399
#' @param value An array
400400
#'
401401
#' @details
402402
#'
403-
#' While [rvar]s implement fast versions of basic math operations (including
403+
#' While [`rvar`]s implement fast versions of basic math operations (including
404404
#' [matrix multiplication][rvar-matmult]), sometimes you may need to bypass
405-
#' the [rvar] abstraction to do what you need to do more efficiently.
405+
#' the [`rvar`] abstraction to do what you need to do more efficiently.
406406
#' `draws_of()` allows you to get / set the underlying array of draws in
407407
#' order to do that.
408408
#'
409-
#' [rvar]s represent draws internally using arrays of arbitrary dimension, which
409+
#' [`rvar`]s represent draws internally using arrays of arbitrary dimension, which
410410
#' is returned by `draws_of(x)` and can be set using `draws_of(x) <- value`.
411411
#' The **first** dimension of these arrays is the index of the draws.
412412
#'
@@ -469,9 +469,9 @@ vec_restore.rvar <- function(x, ...) {
469469
#'
470470
#' The probability density function (`density()`), cumulative distribution
471471
#' function (`cdf()`), and quantile function / inverse CDF (`quantile()`) of
472-
#' an [rvar()].
472+
#' an [`rvar`].
473473
#'
474-
#' @param x an [rvar()]
474+
#' @param x an [`rvar`]
475475
#' @param q,at vector of quantiles.
476476
#' @param probs vector of probabilities
477477
#' @param ... Additional arguments passed onto underlying methods:
@@ -482,7 +482,7 @@ vec_restore.rvar <- function(x, ...) {
482482
#' @return
483483
#'
484484
#' A vector of the same length as the input (`q`, `at`, or `probs`) containing
485-
#' values from the corresponding function of the given [rvar()].
485+
#' values from the corresponding function of the given [`rvar`].
486486
#'
487487
#' @name rvar-functions
488488
#' @export
@@ -543,8 +543,7 @@ bind_rvar <- function(.f, args, .axis = 2) {
543543
}
544544

545545
if (is.data.frame(args[[2]])) {
546-
# TODO
547-
stop2("not implemented")
546+
stop2("TODO: IMPLEMENT")
548547
}
549548

550549
args[[2]] <- as_rvar(args[[2]])

R/rvar-cast.R

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#' Coerce to a random variable
22
#'
3-
#' Convert `x` to an [rvar()] object.
3+
#' Convert `x` to an [`rvar`] object.
44
#'
5-
#' @param x An object that can be converted to an [rvar], such as a vector or array
6-
#' (or an [rvar] itself).
5+
#' @param x An object that can be converted to an [`rvar`], such as a vector or array
6+
#' (or an [`rvar`] itself).
77
#' @template args-rvar-dim
88
#'
9-
#' @details For objects that are already [rvar]s, returns them (with modified dimensions
9+
#' @details For objects that are already [`rvar`]s, returns them (with modified dimensions
1010
#' if `dim` is not `NULL`).
1111
#'
12-
#' For numeric or logical vectors or arrays, returns an [rvar] with a single draw and
12+
#' For numeric or logical vectors or arrays, returns an [`rvar`] with a single draw and
1313
#' the same dimensions as `x`. This is in contrast to the [rvar()] constructor, which
1414
#' treats the first dimension of `x` as the draws dimension. As a result, `as_rvar()`
1515
#' is useful for creating constants.
1616
#'
17-
#' @seealso [rvar()] to construct [rvar]s directly.
17+
#' @seealso [rvar()] to construct [`rvar`]s directly.
1818
#'
1919
#' @return An object of class `"rvar"` representing a random variable.
2020
#'
@@ -37,7 +37,7 @@ as_rvar <- function(x, dim = NULL) {
3737

3838
#' rvar vctrs compatibility
3939
#'
40-
#' These functions implement compatibility with [vctrs-package] for [rvar]s.
40+
#' These functions implement compatibility with [vctrs-package] for [`rvar`]s.
4141
#'
4242
#' @name vctrs-compat
4343
#'

R/rvar-math.R

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#' Compute expectations (`E()` or `mean()`), probabilities (`Pr()`), and
66
#' medians (`median()`) from a random variable.
77
#'
8-
#' @param x an [rvar()]
8+
#' @param x an [`rvar`]
99
#' @param na.rm Should `NA` values in the random variable be removed before
1010
#' computing summaries?
1111
#'
1212
#' Both `E()`, `mean()`, and `Pr()` take means over the draws dimension of the provided
13-
#' random variable. `Pr()` additionally checks that the provided [rvar]
13+
#' random variable. `Pr()` additionally checks that the provided [`rvar`]
1414
#' is a logical variable (hence, taking its expectation results in a probability).
1515
#' `median()` takes medians.
1616
#'
@@ -68,7 +68,7 @@ Pr <- function(x, na.rm = FALSE) {
6868
#'
6969
#' Compute summaries of random variables within draws, producing a new random variable.
7070
#'
71-
#' @param x an [rvar()]
71+
#' @param x an [`rvar`]
7272
#' @param na.rm Should `NA` values in the random variable be removed before
7373
#' computing summaries?
7474
#'
@@ -85,7 +85,7 @@ Pr <- function(x, na.rm = FALSE) {
8585
#' - `range()`
8686
#'
8787
#' @return
88-
#' An [rvar()] of length 1 (or in the case of `range()`, length 2) with the same number
88+
#' An [`rvar`] of length 1 (or in the case of `range()`, length 2) with the same number
8989
#' of draws as the input rvar(s) containing the summary statistic computed within
9090
#' each draw of the input rvar(s).
9191
#'
@@ -189,22 +189,22 @@ Math.rvar <- function(x, ...) {
189189
#'
190190
#' @name rvar-matmult
191191
#' @aliases %**%
192-
#' @param x An [rvar], [numeric], or [logical]. Must be 1 or 2-dimensional. If it is a vector,
192+
#' @param x An [`rvar`], [`numeric`], or [`logical`]. Must be 1 or 2-dimensional. If it is a vector,
193193
#' it is treated as a row vector.
194-
#' @param y An [rvar], [numeric], or [logical]. Must be 1 or 2-dimensional. If it is a vector,
194+
#' @param y An [`rvar`], [`numeric`], or [`logical`]. Must be 1 or 2-dimensional. If it is a vector,
195195
#' it is treated as a column vector.
196196
#'
197197
#' @details
198198
#' If `x` or `y` are vectors, they are converted into matrices prior to multiplication, with `x`
199199
#' converted to a row vector and `y` to a column vector. Numerics and logicals can be multiplied
200-
#' by [rvar]s and are broadcasted across all draws of the [rvar] argument. Tensor multiplication
201-
#' is used to efficiently multiply matrices across draws, so if either `x` or `y` is an [rvar],
200+
#' by [`rvar`]s and are broadcasted across all draws of the [`rvar`] argument. Tensor multiplication
201+
#' is used to efficiently multiply matrices across draws, so if either `x` or `y` is an [`rvar`],
202202
#' `x %**% y` will be much faster than `rdo(x %*% y)`.
203203
#'
204-
#' Because [rvar] is an S3 class and S3 classes cannot properly override `%*%`, [rvar]s use
204+
#' Because [`rvar`] is an S3 class and S3 classes cannot properly override `%*%`, [`rvar`]s use
205205
#' `%**%` for matrix multiplication.
206206
#'
207-
#' @return An [rvar] representing the matrix product of `x` and `y`.
207+
#' @return An [`rvar`] representing the matrix product of `x` and `y`.
208208
#'
209209
#' @importFrom tensorA mul.tensor as.tensor
210210
#' @export

R/rvar-print.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#' Print or format a random variable
22
#'
3-
#' Printing and formatting methods for [rvar]s.
3+
#' Printing and formatting methods for [`rvar`]s.
44
#'
55
#' @encoding UTF-8
6-
#' @param x,object An [rvar].
6+
#' @param x,object An [`rvar`].
77
#' @param color Whether or not to use color when formatting the output. If `TRUE`,
88
#' the [pillar::style_num()] functions may be used to produce strings containing
99
#' control sequences to produce colored output on the terminal.
@@ -16,10 +16,10 @@
1616
#' @param ... Further arguments passed to other functions.
1717
#'
1818
#' @details
19-
#' `print()` and `str()` print out [rvar()] objects by summarizing each element
19+
#' `print()` and `str()` print out [`rvar`] objects by summarizing each element
2020
#' in the random variable with either its mean±sd or median±mad, depending on
2121
#' the value of `summary`. Both functions use the `format()` implementation for
22-
#' [rvar()] objects under the hood, which returns a character vector in the
22+
#' [`rvar`] objects under the hood, which returns a character vector in the
2323
#' mean±sd or median±mad form.
2424
#'
2525
#' @return

man-roxygen/args-rvar-dim.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#' @param dim Override for the dimensions of the [rvar] to be created (see [dim()]): an integer vector of length one
1+
#' @param dim Override for the dimensions of the [`rvar`] to be created (see [dim()]): an integer vector of length one
22
#' or more giving the maximal indices in each dimension. If `NULL` (the default), this is determined by the input.

man/E.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/Summary.rvar.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/as_rvar.Rd

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)