Skip to content

Commit 2af0efe

Browse files
authored
Merge pull request #207 from stan-dev/CmdStanMCMC_CSV
as.shinystan compatibility with cmdstanr::as_cmdstan_fit
2 parents bc87367 + 7249e7b commit 2af0efe

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ Additional_repositories:
6363
LazyData: true
6464
Encoding: UTF-8
6565
VignetteBuilder: knitr
66-
RoxygenNote: 7.1.2
66+
RoxygenNote: 7.3.3

R/shinystan-objects.R

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -870,49 +870,64 @@ setMethod(
870870

871871

872872
# as.shinystan (CmdStanMCMC) -----------------------------------------------
873+
as.shinystan_cmdstanr <- function(X,
874+
pars = NULL,
875+
model_name = NULL,
876+
note = NULL,
877+
...) {
878+
check_suggests("cmdstanr")
879+
check_suggests("posterior")
880+
if (is.null(model_name) && !is.null(X$runset)) {
881+
model_name <- X$runset$model_name()
882+
} else {
883+
model_name <- "unnamed model"
884+
}
885+
886+
if (X$metadata()$save_warmup == 0) {
887+
draws <- unclass(X$draws(pars))
888+
sampler_diagnostics <- X$sampler_diagnostics()
889+
n_warmup <- 0
890+
} else {
891+
draws <- unclass(X$draws(pars, inc_warmup = TRUE))
892+
sampler_diagnostics <- X$sampler_diagnostics(inc_warmup = TRUE)
893+
n_warmup <- X$metadata()$iter_warmup
894+
}
895+
896+
sampler_params <- list()
897+
for (j in seq_len(dim(sampler_diagnostics)[2])) {
898+
sampler_params[[j]] <- posterior::as_draws_matrix(sampler_diagnostics[, j ,])
899+
}
900+
901+
as.shinystan(
902+
draws,
903+
model_name = model_name,
904+
warmup = n_warmup,
905+
param_dims = X$metadata()$stan_variable_dims,
906+
model_code = NULL,
907+
note = note,
908+
sampler_params = sampler_params,
909+
algorithm = "NUTS",
910+
max_treedepth = X$metadata()$max_treedepth
911+
)
912+
}
913+
873914
setOldClass("CmdStanMCMC")
874915
#' @describeIn as.shinystan Create a \code{shinystan} object from a
875916
#' \code{CmdStanMCMC} object (\pkg{cmdstanr}).
876917
#'
877918
setMethod(
878919
"as.shinystan",
879920
signature = "CmdStanMCMC",
880-
definition = function(X,
881-
pars = NULL,
882-
model_name = NULL,
883-
note = NULL,
884-
...) {
885-
check_suggests("cmdstanr")
886-
check_suggests("posterior")
887-
if (is.null(model_name)) {
888-
model_name <- X$runset$model_name()
889-
}
890-
891-
if (X$metadata()$save_warmup == 0) {
892-
draws <- unclass(X$draws(pars))
893-
sampler_diagnostics <- X$sampler_diagnostics()
894-
n_warmup <- 0
895-
} else {
896-
draws <- unclass(X$draws(pars, inc_warmup = TRUE))
897-
sampler_diagnostics <- X$sampler_diagnostics(inc_warmup = TRUE)
898-
n_warmup <- X$metadata()$iter_warmup
899-
}
900-
901-
sampler_params <- list()
902-
for (j in seq_len(dim(sampler_diagnostics)[2])) {
903-
sampler_params[[j]] <- posterior::as_draws_matrix(sampler_diagnostics[, j ,])
904-
}
905-
906-
as.shinystan(
907-
draws,
908-
model_name = model_name,
909-
warmup = n_warmup,
910-
param_dims = X$metadata()$stan_variable_dims,
911-
model_code = NULL,
912-
note = note,
913-
sampler_params = sampler_params,
914-
algorithm = "NUTS",
915-
max_treedepth = X$metadata()$max_treedepth
916-
)
917-
}
921+
definition = as.shinystan_cmdstanr
922+
)
923+
924+
setOldClass("CmdStanMCMC_CSV")
925+
#' @describeIn as.shinystan Create a \code{shinystan} object from a
926+
#' \code{CmdStanMCMC_CSV} object created using
927+
#' \code{cmdstanr::as_cmdstan_fit()} (\pkg{cmdstanr}).
928+
#'
929+
setMethod(
930+
"as.shinystan",
931+
signature = "CmdStanMCMC_CSV",
932+
definition = as.shinystan_cmdstanr
918933
)

inst/ShinyStan/ui_utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ logo_and_name <- function() {
3636
),
3737
div(
3838
style = "margin-top: 25px",
39-
img(src = "stan_logo.svg", class = "stan-logo"),
39+
img(src = "stan_logo.png", class = "stan-logo"),
4040
div(id = "shinystan-title", "ShinyStan")
4141
)
4242
)

tests/testthat/test_creating_sso.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ test_that("as.shinystan works with CmdStanMCMC objects", {
204204
expect_equal(sso@param_names, c("log-posterior", "mu", "tau", paste0("theta[", 1:8, "]")))
205205
expect_equal(sso@n_chain, 2)
206206
expect_equal(sso@n_warmup, 500)
207+
208+
expect_no_error(sso2 <- as.shinystan(cmdstanr::as_cmdstan_fit(fit$output_files())))
207209
}
208210
})
209211

0 commit comments

Comments
 (0)