Stan output in RStan



Two questions about output when running stan() or sampling():

  • Are the exception messages stored anywhere in the fit object returned, or are they just written to stderr? If I’m running a series of models or happen to do anything that produces a lot of output after running stan(), the exception messages get scrolled off the top of my console buffer and lost.

  • Is there any way to redirect this output? I’ve tried using sink() and capture.output(), but warning messages and progress messages still are printed on the console.


Check out the startBigStan/watchBigStan/collectBigStan functions in my very much work-in-progress package ezStan. It creates a temp folder and runs the sampling in a separate process per core/chain and pipes the stdout & stderr messages to files you can look at. I’m about to add a bit to collectBigStan that will grab both and store them as an attribute to the returned stanfit object so you can check their contents from R.


We’re working toward truly shutting up Stan. Now that we’ve refactored the basic commands on the C++ side it should be easier going forward.


I’ve used the quietly() function in the purrr package on sampling() to suppress the output. quietly() returns a list with 4 elements: result for the data what would be returned by sampling, and output, warnings, and messages for different kinds of console output.

q_demo <- purrr::quietly(sampling)(my_model, data_list)
str(q_demo, max.level = 1)
#> List of 4
#>  $ result  :Formal class 'stanfit' [package "rstan"] with 10 slots
#>  $ output  : chr "\nSAMPLING FOR MODEL '10aab2914b4d5570b26da460b3b8758b' NOW (CHAIN 1).\n"| __truncated__
#>  $ warnings: chr(0) 
#>  $ messages: chr(0) 


On the other question (“Are exception messages stored anywhere in the fit objected returned?”) I take it then that the answer is no?

Fortunately, it looks like I can use purrr::quietly (thanks, tjmahr!) to capture those, even if other output currently leaks through.