Skip to content

Commit 1f4efad

Browse files
authored
Merge pull request #967 from sbfnk/print-line-numbers
print line numbers
2 parents c3d0168 + 8e8e0f4 commit 1f4efad

8 files changed

+36
-2
lines changed

R/model.R

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
#' file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan")
5555
#' mod <- cmdstan_model(file)
5656
#' mod$print()
57+
#' # Print with line numbers. This can be set globally using the
58+
#' # `cmdstanr_print_line_numbers` option.
59+
#' mod$print(line_numbers = TRUE)
5760
#'
5861
#' # Data as a named list (like RStan)
5962
#' stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
@@ -298,11 +301,21 @@ CmdStanModel <- R6::R6Class(
298301
}
299302
private$stan_code_
300303
},
301-
print = function() {
304+
print = function(line_numbers = getOption("cmdstanr_print_line_numbers", FALSE)) {
302305
if (length(private$stan_code_) == 0) {
303306
stop("'$print()' cannot be used because the 'CmdStanModel' was not created with a Stan file.", call. = FALSE)
304307
}
305-
cat(self$code(), sep = "\n")
308+
lines <- self$code()
309+
if (line_numbers) {
310+
line_num_indent <- nchar(as.character(length(lines)))
311+
line_nums <- vapply(seq_along(lines), function(y) {
312+
paste0(
313+
rep(" ", line_num_indent - nchar(as.character(y))), y, collapse = ""
314+
)
315+
}, character(1))
316+
lines <- paste(paste(line_nums, lines, sep = ": "), collapse = "\n")
317+
}
318+
cat(lines, sep = "\n")
306319
invisible(self)
307320
},
308321
stan_file = function() {

man/CmdStanModel.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cmdstan_model.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cmdstanr-package.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-optimize.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-pathfinder.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-sample.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model-method-variational.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)