ropensci / jagstargets

Reproducible Bayesian data analysis pipelines with targets and JAGS
https://docs.ropensci.org/jagstargets
Other
10 stars 6 forks source link

Missing definition of `n` in MCMC pipeline vignette #15

Closed dill closed 3 years ago

dill commented 3 years ago

Prework

Description

This is part of ropensci/software-review#425.

Running tar_make() at this line fails:

> tar_make()
• start target example_data
✖ error target example_data
• end pipeline
Error : object 'n' not found
Error: callr subprocess failed: object 'n' not found
Visit https://books.ropensci.org/targets/debugging.html for debugging advice.

I suspect because n is not defined in generate_data() (but is fine in tar_jags_example_data() which is run when generating the vignette?)

It's not clear to me (at this point of neophyte experience) if one should parameterize generate_data() as generate_data(n) or if n is better fixed (otherwise I would have just submitted a PR :)).

Reproducible example

lines <- "model {
  for (i in 1:n) {
    y[i] ~ dnorm(x[i] * beta, 1)
  }
  beta ~ dnorm(0, 1)
}"
writeLines(lines, "x.jags")

lines <- "# _targets.R
library(targets)
library(jagstargets)

generate_data <- function() {
  true_beta <- stats::rnorm(n = 1, mean = 0, sd = 1)
  x <- seq(from = -1, to = 1, length.out = n)
  y <- stats::rnorm(n, x * true_beta, 1)
  out <- list(n = n, x = x, y = y)
}

list(
  tar_jags(
    example,
    jags_files = \"x.jags\",
    parameters.to.save = \"beta\",
    data = generate_data(),
    stdout = R.utils::nullfile(),
    stderr = R.utils::nullfile(),
  )
)"
writeLines(lines, "_targets.R")

library(targets)
library(jagstargets)

tar_make()

Expected result

The pipeline to run :)

Diagnostic information

> sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] targets_0.4.2.9000     jagstargets_0.0.0.9001

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0       purrr_0.3.4.9000       lattice_0.20-41
 [4] colorspace_2.0-0       vctrs_0.3.7            generics_0.1.0
 [7] htmltools_0.5.1.1      yaml_2.2.1             utf8_1.2.1
[10] R2jags_0.6-1           rlang_0.4.10           pillar_1.6.0
[13] glue_1.4.2             withr_2.4.2            DBI_1.1.1
[16] tarchetypes_0.1.1.9000 distributional_0.2.2   lifecycle_1.0.0
[19] posterior_0.1.4        munsell_0.5.0          gtable_0.3.0
[22] htmlwidgets_1.5.3      visNetwork_2.0.9       stringfish_0.15.1
[25] codetools_0.2-18       coda_0.19-4            fst_0.9.4
[28] RApiSerialize_0.1.0    callr_3.7.0            ps_1.6.0
[31] parallel_4.0.4         fansi_0.4.2            Rcpp_1.0.6
[34] scales_1.1.1           backports_1.2.1        checkmate_2.0.0
[37] jsonlite_1.7.2         RcppParallel_5.1.2     abind_1.4-5
[40] farver_2.1.0           fs_1.5.0               tensorA_0.36.2
[43] ggplot2_3.3.3          rjags_4-10             digest_0.6.27
[46] processx_3.5.1         dplyr_1.0.5            qs_0.24.1
[49] grid_4.0.4             cli_2.5.0              tools_4.0.4
[52] magrittr_2.0.1         tibble_3.1.1           crayon_1.4.1
[55] pkgconfig_2.0.3        ellipsis_0.3.1         data.table_1.14.0
[58] rstudioapi_0.13        assertthat_0.2.1       R6_2.5.0
[61] boot_1.3-27            R2WinBUGS_2.1-21       igraph_1.2.6
[64] compiler_4.0.4

Using fc3154d

wlandau commented 3 years ago

Thanks @dill, should be fixed now.