Cannot save output from pairs.stanfit()

I’m trying to save the output from tmp <- pairs(my_stanfit), however, when I do the plot is drawn to my screen and tmp remains NULL. What am I missing?

R is free software and comes with ABSOLUTELY NO WARRANTY.
...
Type 'q()' to quit R.

> sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

loaded via a namespace (and not attached):
[1] compiler_4.2.2
> library(rstan, verbose = TRUE)
Loading required package: StanHeaders

rstan version 2.26.13 (Stan version 2.26.1)

For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions,
change `threads_per_chain` option:
rstan_options(threads_per_chain = 1)

Warning message:
In find.package(package, lib.loc, quiet = TRUE, verbose = verbose) :
  package ‘rstan’ found more than once, using the first from
  “/usr/local/lib/R/site-library/rstan”,
  “/usr/lib/R/site-library/rstan”
> sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] rstan_2.26.13       StanHeaders_2.26.13

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9         compiler_4.2.2     pillar_1.8.1       prettyunits_1.1.1
 [5] pkgbuild_1.3.1     jsonlite_1.8.3     lifecycle_1.0.3    tibble_3.1.8
 [9] gtable_0.3.1       pkgconfig_2.0.3    rlang_1.0.6        DBI_1.1.3
[13] cli_3.4.1          parallel_4.2.2     curl_4.3.3         loo_2.5.1
[17] gridExtra_2.3      dplyr_1.0.99.9000  generics_0.1.3     vctrs_0.5.0
[21] stats4_4.2.2       grid_4.2.2         tidyselect_1.2.0   glue_1.6.2
[25] inline_0.3.19      R6_2.5.1           processx_3.8.0     fansi_1.0.3
[29] ggplot2_3.4.0      callr_3.7.3        magrittr_2.0.3     codetools_0.2-18
[33] matrixStats_0.62.0 scales_1.2.1       ps_1.7.2           colorspace_2.0-3
[37] V8_4.2.1           utf8_1.2.2         RcppParallel_5.1.5 munsell_0.5.0
[41] crayon_1.5.2
> example(read_stan_csv)

rd_st_> csvfiles <- dir(system.file('misc', package = 'rstan'),
rd_st_+                 pattern = 'rstan_doc_ex_[0-9].csv', full.names = TRUE)

rd_st_> fit <- read_stan_csv(csvfiles)
> tmp <- pairs(fit, pars = c("mu", "sigma", "alpha", "lp__"), log = TRUE, las = 1)
Warning messages:
1: In par(usr) : argument 1 does not name a graphical parameter
2: In par(usr) : argument 1 does not name a graphical parameter
3: In par(usr) : argument 1 does not name a graphical parameter
4: In par(usr) : argument 1 does not name a graphical parameter
> tmp
NULL
> 

Extra question: why do I get the warnings about par(usr)?

Haven’t been able to find any documentation on this for pairs.stanfit, but unlike with pairs.stanfit, I can save the output from mcmc_pairs() to a variable. The plotting default settings are different, but it’s a very functional work around.

I suspect the difference in behavior between pairs.stanfit and mcmc_pairs has to do with what’s being returned by the functions.

Looking at the code for mcmc_pairs, the very end of it looks like this

.
.
.
  plots <- lapply(plots, function(x)
    x + xaxis_title(FALSE) + yaxis_title(FALSE))

  bayesplot_grid(plots = plots,
                 legends = FALSE,
                 grid_args = grid_args,
                 save_gg_objects = save_gg_objects)
}

In contrast, pairs.stanfit ends with

.
.
.
    mc <- match.call(expand.dots = TRUE)
    mc[1] <- call("pairs")
    mc$x <- x
    mc$labels <- labels
    mc$panel <- panel
    mc$lower.panel <- lower.panel
    mc$upper.panel <- upper.panel
    mc$diag.panel <- diag.panel
    mc$text.panel <- textPanel
    mc$log <- ""
    mc$condition <- NULL
    mc$pars <- NULL
    mc$include <- NULL
    eval(mc)
}

Perhaps someone who has a deeper understanding of R can explain how to returning something other than eval(mc) would return a bayesplot_grid() like object