11
11
# ' When using `pars` for tidy parameter selection, the `regex_pars` argument
12
12
# ' is ignored because **bayesplot** supports using
13
13
# ' [tidyselect helper functions][tidyselect::select_helpers]
14
- # ' (`starts_with()`, `contains()`, etc.) for the same purpose.
15
- # '
16
- # ' **bayesplot** also exports additional helper functions
17
- # ' `param_range()` and `param_glue()`
18
- # ' to help with parameter selection. See the **Examples** section.
14
+ # ' (`starts_with()`, `contains()`, `num_range()`, etc.) for the same purpose.
15
+ # ' **bayesplot** also exports some additional helper functions
16
+ # ' to help with parameter selection:
17
+ # ' * `param_range()`: like [tidyselect::num_range()] but used when parameter
18
+ # ' indexes are in brackets (e.g. `beta[2]`).
19
+ # ' * `param_glue()`: for more complicated parameter names with multiple
20
+ # ' indexes (including variable names) inside the brackets
21
+ # ' (e.g., `beta[(Intercept) age_group:3]`).
22
+ # ' These functions can be used inside of `vars()`, `dplyr::select()`,
23
+ # ' and similar functions just like the
24
+ # ' [tidyselect helper functions][tidyselect::select_helpers].
25
+ # ' See the **Examples** section.
19
26
# '
20
27
# ' @examples
21
28
# ' x <- example_mcmc_draws(params = 6)
28
35
# ' mcmc_hist(x, pars = vars(param_range("beta", c(1, 3, 4))))
29
36
# '
30
37
# ' \donttest{
31
- # ' ############################
32
- # ' ## Example using rstanarm ##
33
- # ' ############################
38
+ # ' #############################
39
+ # ' ## Examples using rstanarm ##
40
+ # ' #############################
34
41
# ' if (requireNamespace("rstanarm", quietly = TRUE)) {
35
42
# ' # see ?rstanarm::example_model
36
43
# ' fit <- example("example_model", package = "rstanarm", local=TRUE)$value
42
49
# ' mcmc_hist(posterior, pars = vars(size, contains("period")))
43
50
# '
44
51
# ' # same as previous but using dplyr::select() and piping
45
- # ' library(dplyr)
52
+ # ' library(" dplyr" )
46
53
# ' posterior %>%
47
54
# ' select(size, contains("period")) %>%
48
55
# ' mcmc_hist()
@@ -81,19 +88,27 @@ dplyr::vars
81
88
82
89
# ' @rdname tidy-params
83
90
# ' @export
91
+ # ' @param vars `NULL` or a character vector of parameter names to choose from.
92
+ # ' This is only needed for the atypical use case of calling the function as a
93
+ # ' standalone function outside of `vars()`, `select()`, etc. Typically this is
94
+ # ' left as `NULL` and will be set automatically for the user.
84
95
# ' @param prefix,range For `param_range()` only, `prefix` is a string naming a
85
- # ' parameter and `range` is an integer vector providing the indices of the
96
+ # ' parameter and `range` is an integer vector providing the indices of a
86
97
# ' subset of elements to select. For example, using
87
98
# '
88
99
# ' param_range("beta", c(1,2,8))
89
100
# '
90
101
# ' would select parameters named `beta[1]`, `beta[2]`, and `beta[8]`.
91
102
# ' `param_range()` is only designed for the case that the indices are integers
92
- # ' surrounded by brackets.
103
+ # ' surrounded by brackets. If there are no brackets use
104
+ # ' [num_range()][tidyselect::select_helpers].
93
105
# '
94
- param_range <- function (prefix , range ) {
106
+ param_range <- function (prefix , range , vars = NULL ) {
107
+ if (! is.null(vars ) && ! is.character(vars )) {
108
+ abort(" 'vars' must be NULL or a character vector." )
109
+ }
95
110
nms <- paste0(prefix , " [" , range , " ]" )
96
- param_matches <- match(nms , tidyselect :: peek_vars())
111
+ param_matches <- match(nms , vars % || % tidyselect :: peek_vars())
97
112
param_matches [! is.na(param_matches )]
98
113
}
99
114
@@ -113,7 +128,11 @@ param_range <- function(prefix, range) {
113
128
# ' See the **Examples** section below for demonstrations.
114
129
# '
115
130
# ' @examples
116
- # ' # more examples of param_glue()
131
+ # ' \dontrun{
132
+ # ' ###################################
133
+ # ' ## More examples of param_glue() ##
134
+ # ' ###################################
135
+ # ' library(dplyr)
117
136
# ' posterior <-
118
137
# ' structure(list(
119
138
# ' b_Intercept = rnorm(1000),
@@ -130,6 +149,7 @@ param_range <- function(prefix, range) {
130
149
# ' )
131
150
# ' str(posterior)
132
151
# '
152
+ # ' # using one expression in braces
133
153
# ' posterior %>%
134
154
# ' select(
135
155
# ' param_glue(
@@ -138,6 +158,7 @@ param_range <- function(prefix, range) {
138
158
# ' ) %>%
139
159
# ' mcmc_hist()
140
160
# '
161
+ # ' # using multiple expressions in braces
141
162
# ' posterior %>%
142
163
# ' select(
143
164
# ' param_glue(
@@ -146,13 +167,15 @@ param_range <- function(prefix, range) {
146
167
# ' type = c("Intercept", "Slope"))
147
168
# ' ) %>%
148
169
# ' mcmc_hist()
170
+ # '}
149
171
# '
150
- # '
151
- # '
152
- param_glue <- function (pattern , ... ) {
172
+ param_glue <- function (pattern , ... , vars = NULL ) {
173
+ if (! is.null(vars ) && ! is.character(vars )) {
174
+ abort(" 'vars' must be NULL or a character vector." )
175
+ }
153
176
dots <- as.list(expand.grid(... ))
154
177
nms <- as.character(glue :: glue_data(dots , pattern ))
155
- param_matches <- match(nms , tidyselect :: peek_vars())
178
+ param_matches <- match(nms , vars % || % tidyselect :: peek_vars())
156
179
param_matches [! is.na(param_matches )]
157
180
}
158
181
0 commit comments