# Traceplot does not work when called from within a function

**URL:** <https://discourse.mc-stan.org/t/traceplot-does-not-work-when-called-from-within-a-function/22287>\
**Category:** RStan\
**Created:** [May 4, 2021, 1:02am UTC](https://discourse.mc-stan.org/t/traceplot-does-not-work-when-called-from-within-a-function/22287 "2021-05-04T01:02:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wqmeeker](https://avatars.discourse-cdn.com/v4/letter/w/f07891/32.png) [@wqmeeker](https://discourse.mc-stan.org/u/wqmeeker)\
**Post date:** [May 4, 2021, 1:02am UTC](https://discourse.mc-stan.org/t/traceplot-does-not-work-when-called-from-within-a-function/22287/1 "2021-05-04T01:02:13Z")

</div>

I am doing some simulation work where in each simulation iteration I call pre-compiled Stan models (using the rstan sampling function). When I go to a new factor-level combination of my simulation inputs I like to look carefully at the MCMC diagnortics for 30 or so trials. In my simulation R function I inserted calls to pairs and traceplot just after the call to sampling. The pairs plot come out fine, but not the traceplot results. Traceplot works fine when I save a stanfit object and call traceplot from the R command line. What am I doing wrong?

I am running on Windows 10 using using R 3.6.3 and rstan 2.21.2.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [May 11, 2021, 8:37am UTC](https://discourse.mc-stan.org/t/traceplot-does-not-work-when-called-from-within-a-function/22287/2 "2021-05-11T08:37:53Z")

</div>

Hi, this is possibly more of a general R thing. R does by default print return values of functions evaluated in console. So often what you need to do to see plots from within a function is either a) return the plot or b) call `print(your_plot)`

Good luck with your work!

---

<div class="post-metadata">

**Author:** ![wqmeeker](https://avatars.discourse-cdn.com/v4/letter/w/f07891/32.png) [@wqmeeker](https://discourse.mc-stan.org/u/wqmeeker)\
**Post date:** [May 11, 2021, 10:14pm UTC](https://discourse.mc-stan.org/t/traceplot-does-not-work-when-called-from-within-a-function/22287/3 "2021-05-11T22:14:31Z")

</div>

> [@martinmodrak](#):
>
> print(your\_plot)

Thanks very much. print(your\_plot) did the trick.

Bill
