Skip to content

Commit

Permalink
make line number printing optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk committed May 13, 2024
1 parent a74e51a commit 3eaa74e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,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)
}
lines <- self$code()
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))
cat(
paste(paste(line_nums, lines, sep = ": "), collapse = "\n"), sep = "\n"
)
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

0 comments on commit 3eaa74e

Please sign in to comment.