# Is using saveRDS and readRDS with a stanfit object a supported usage?

**URL:** <https://discourse.mc-stan.org/t/is-using-saverds-and-readrds-with-a-stanfit-object-a-supported-usage/3391>\
**Category:** RStan\
**Created:** [February 23, 2018, 2:10pm UTC](https://discourse.mc-stan.org/t/is-using-saverds-and-readrds-with-a-stanfit-object-a-supported-usage/3391 "2018-02-23T14:10:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjramsey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jjramsey/32/3319_2.png) [@jjramsey](https://discourse.mc-stan.org/u/jjramsey)\
**Post date:** [February 23, 2018, 2:10pm UTC](https://discourse.mc-stan.org/t/is-using-saverds-and-readrds-with-a-stanfit-object-a-supported-usage/3391/1 "2018-02-23T14:10:23Z")

</div>

I’ve used saveRDS and readRDS on stanfit objects and found that for most of my use cases, it _happens_ to work for the most part. I haven’t even had to use the trick [mentioned in an earlier post](http://discourse.mc-stan.org/t/saving-and-sharing-an-rstan-model-fit/1059/2), where the attribute `stanmodel@dso` of a stanfit object is set to `new("cxxdso")` before saving it with saveRDS. However, there are some methods for which a saved stanfit object _doesn’t_ work, such as [`unconstrain_pars`](https://github.com/stan-dev/rstan/issues/445), and this is apparently considered normal operation and not a bug. This makes me wonder if what I’ve been doing has only worked because of luck.

Should I be using saveRDS with stanfit objects at all, or should I assume that a stanfit object is only guaranteed to work in the same session that it was generated in?

Would a “best practice” be to just extract the information from the stanfit object that I _think_ I’ll need and then save that to files that I know will be portable?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [February 23, 2018, 5:39pm UTC](https://discourse.mc-stan.org/t/is-using-saverds-and-readrds-with-a-stanfit-object-a-supported-usage/3391/2 "2018-02-23T17:39:19Z")

</div>

> [@jjramsey](#):
>
> Should I be using saveRDS with stanfit objects at all, or should I assume that a stanfit object is only guaranteed to work in the same session that it was generated in?

The latter. The stuff that does not work requires that the compiled Stan program be loaded into R’s memory as a dynamic shared object and those do not persist across R sessions. It is the same as if you loaded the draws from CSV files.
