Skip to content

Commit

Permalink
Merge pull request #967 from sbfnk/print-line-numbers
Browse files Browse the repository at this point in the history
print line numbers
  • Loading branch information
andrjohns authored May 13, 2024
2 parents c3d0168 + 8e8e0f4 commit 1f4efad
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 2 deletions.
17 changes: 15 additions & 2 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
#' file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan")
#' mod <- cmdstan_model(file)
#' mod$print()
#' # Print with line numbers. This can be set globally using the
#' # `cmdstanr_print_line_numbers` option.
#' mod$print(line_numbers = TRUE)
#'
#' # Data as a named list (like RStan)
#' stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
Expand Down Expand Up @@ -298,11 +301,21 @@ CmdStanModel <- R6::R6Class(
}
private$stan_code_
},
print = function() {
print = function(line_numbers = getOption("cmdstanr_print_line_numbers", FALSE)) {
if (length(private$stan_code_) == 0) {
stop("'$print()' cannot be used because the 'CmdStanModel' was not created with a Stan file.", call. = FALSE)
}
cat(self$code(), sep = "\n")
lines <- self$code()
if (line_numbers) {
line_num_indent <- nchar(as.character(length(lines)))
line_nums <- vapply(seq_along(lines), function(y) {
paste0(
rep(" ", line_num_indent - nchar(as.character(y))), y, collapse = ""
)
}, character(1))
lines <- paste(paste(line_nums, lines, sep = ": "), collapse = "\n")
}
cat(lines, sep = "\n")
invisible(self)
},
stan_file = function() {
Expand Down
3 changes: 3 additions & 0 deletions man/CmdStanModel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/cmdstan_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/cmdstanr-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/model-method-optimize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/model-method-pathfinder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/model-method-sample.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/model-method-variational.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f4efad

Please sign in to comment.