Skip to content

Commit

Permalink
ensure purl'd vignette code can be executed, closes #328
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Sep 16, 2024
1 parent ee805e6 commit 89df29a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
25 changes: 14 additions & 11 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ jobs:
matrix:
config:
# running Windows and Mac with --no-examples to avoid spurious Stan crashes in examples
- {os: windows-latest, r: 'release', suffix: 'quick', not_cran: 'false', check_options: '"--no-build-vignettes", "--ignore-vignettes", "--no-examples"'}
- {os: macOS-latest, r: 'release', suffix: 'quick', not_cran: 'false', check_options: '"--no-build-vignettes", "--ignore-vignettes", "--no-examples"'}
- {os: macOS-latest, r: 'release', suffix: '', not_cran: 'true', check_options: 'NULL'}
- {os: ubuntu-latest, r: 'devel', suffix: '', not_cran: 'true', check_options: 'NULL', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release', suffix: '', not_cran: 'true', check_options: 'NULL', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'release', suffix: 'quick', not_cran: 'false', check_options: '"--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-1', suffix: 'quick', not_cran: 'false', check_options: '"--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-2', suffix: 'quick', not_cran: 'false', check_options: '"--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-3', suffix: 'quick', not_cran: 'false', check_options: '"--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-4', suffix: 'quick', not_cran: 'false', check_options: '"--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: windows-latest, r: 'release', suffix: 'quick', not_cran: 'false', check_options: '"--as-cran", "--no-build-vignettes", "--ignore-vignettes", "--no-examples"'}
- {os: macOS-latest, r: 'release', suffix: 'quick', not_cran: 'false', check_options: '"--as-cran", "--no-build-vignettes", "--ignore-vignettes", "--no-examples"'}
# running Mac without --as-cran and with --no-build-vignettes only to check that purl'd code from vignettes is runnable (#328)
- {os: macOS-latest, r: 'release', suffix: 'purl', not_cran: 'true', check_options: '"--no-build-vignettes"'}
# full checks on linux
- {os: ubuntu-latest, r: 'devel', suffix: '', not_cran: 'true', check_options: '"--as-cran"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release', suffix: '', not_cran: 'true', check_options: '"--as-cran"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
# quick checks on linux
- {os: ubuntu-latest, r: 'release', suffix: 'quick', not_cran: 'false', check_options: '"--as-cran", "--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-1', suffix: 'quick', not_cran: 'false', check_options: '"--as-cran", "--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-2', suffix: 'quick', not_cran: 'false', check_options: '"--as-cran", "--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-3', suffix: 'quick', not_cran: 'false', check_options: '"--as-cran", "--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-latest, r: 'oldrel-4', suffix: 'quick', not_cran: 'false', check_options: '"--as-cran", "--no-build-vignettes", "--ignore-vignettes"', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
Expand Down Expand Up @@ -80,7 +83,7 @@ jobs:
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
NOT_CRAN: ${{ matrix.config.not_cran }}
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran", ${{ matrix.config.check_options }}), error_on = "warning", check_dir = "check")
run: rcmdcheck::rcmdcheck(args = c("--no-manual", ${{ matrix.config.check_options }}), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Upload check results
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# tidybayes (development version)

* Ensure vignette code run through `knitr::purl()` can be executed. (#328)


# tidybayes 3.0.7

New features:

* Add support for `draw_indices` parameter in `spread_draws()` and
`gather_draws()`. (#323)


Bug fixes:

* Support for matrix columns in `nest_rvars()` and `unnest_rvars()`. (#316)
Expand Down
3 changes: 2 additions & 1 deletion vignettes/children/chunk_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ eval_chunks = identical(Sys.getenv("NOT_CRAN"), "true")
knitr::opts_chunk$set(
fig.width = small_width,
fig.height = small_height,
eval = eval_chunks
eval = eval_chunks,
purl = eval_chunks
)
if (capabilities("cairo") && Sys.info()[['sysname']] != "Darwin") {
knitr::opts_chunk$set(
Expand Down
45 changes: 43 additions & 2 deletions vignettes/tidybayes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,48 @@ Shunting data from a data frame into a format usable in samplers like JAGS or St

A hierarchical model of our example data might fit an overall mean across the conditions (`overall_mean`), the standard deviation of the condition means (`condition_mean_sd`), the mean within each condition (`condition_mean[condition]`) and the standard deviation of the responses given a condition mean (`response_sd`):

```{stan ABC_stan_model, output.var = "ABC_stan", results = "hide"}
```{r ABC_stan_model_R, include = FALSE}
# a stupid workaround for the fact that knitr::purl() will not generate an
# R script that can be executed from top to bottom if a vignette contains a stan
# code block (i.e. the very next code block), and some R CMD CHECK configurations
# unreasonably expect that it can. (see https://stat.ethz.ch/pipermail/r-devel/2014-May/069113.html)
# so for now we'll just do this in R code behind the scenes, even though the best
# solution would be that people don't run R CMD CHECK with those configurations
# (most configurations on CRAN have this problem except macos on R 4.3
# for some reason, which as of 9/16/2024 appears to be configured differently
# from other platforms). See https://github.com/mjskay/tidybayes/issues/328
ABC_stan = stan_model(model_code = '
data {
int<lower=1> n;
int<lower=1> n_condition;
int<lower=1, upper=n_condition> condition[n];
real response[n];
}
parameters {
real overall_mean;
vector[n_condition] condition_zoffset;
real<lower=0> response_sd;
real<lower=0> condition_mean_sd;
}
transformed parameters {
vector[n_condition] condition_mean;
condition_mean = overall_mean + condition_zoffset * condition_mean_sd;
}
model {
response_sd ~ cauchy(0, 1); // => half-cauchy(0, 1)
condition_mean_sd ~ cauchy(0, 1); // => half-cauchy(0, 1)
overall_mean ~ normal(0, 5);
condition_zoffset ~ normal(0, 1); // => condition_mean ~ normal(overall_mean, condition_mean_sd)
for (i in 1:n) {
response[i] ~ normal(condition_mean[condition[i]], response_sd);
}
}
')
```


```{stan ABC_stan_model, output.var = "ABC_stan", results = "hide", eval = FALSE}
data {
int<lower=1> n;
int<lower=1> n_condition;
Expand Down Expand Up @@ -629,7 +670,7 @@ bayes_results

Here, `to_broom_names()` will convert `.lower` and `.upper` into `conf.low` and `conf.high` so the names of the columns we need to make the comparison (`condition`, `estimate`, `conf.low`, and `conf.high`) all line up easily. This makes it simple to combine the two tidy data frames together using `bind_rows`, and plot them:

```{r fig.width = tiny_width, fig.height = tiny_height}
```{r fig.width = tiny_width, fig.height = tiny_height, eval = eval_chunks && requireNamespace("emmeans", quietly = TRUE)}
bind_rows(linear_results, bayes_results) %>%
mutate(condition = fct_rev(condition)) %>%
ggplot(aes(y = condition, x = estimate, xmin = conf.low, xmax = conf.high, color = model)) +
Expand Down

0 comments on commit 89df29a

Please sign in to comment.