nbstata icon indicating copy to clipboard operation
nbstata copied to clipboard

`preserve` does not persist across cells when `echo = None`

Open ideabucket opened this issue 2 years ago • 4 comments

In exploratory analysis I will sometimes do this:

preserve
generate somevar = somecommand
* and various other commands

and run the cell, the intention being to inspect the results with a %browse or similar.

But if I run a cell like this, and then from another cell try to summarize somevar, I get a variable not found error. Similarly, if I restore, I get "nothing to restore, r(622);".

~The only way I can make sense of this is if nbstata is using preserve/restore itself, and unwinding my preserve behind the scenes. If so it might be a good idea to detect preserve in user input and throw an error.~

ideabucket avatar Mar 17 '23 00:03 ideabucket

No, but that makes sense that preserve doesn't work, now that you mention it.

Well, does it work if you first run %set echo=False at the top of the notebook?

If not, (A) the explanation is that, behind the scenes, pystata runs each multi-line code cell as if it were a temporary do-file. So, just as running preserve in a do-file doesn't carry over into interactive mode (or another do-file), preserve doesn't carry over across cells.

If so, (B) they've made preserve work across pystata.stata.run calls (as they've apparently done for locals and version) and the reason it's not working is that you were using the (nbstata default) echo=None mode, which is another step more complicated behind the scenes.

Either way, I'd like to fix the issue, if possible. (I routinely use preserve myself, though I apparently hadn't tried it in a notebook yet.) But a solution is not coming immediately to mind.

hugetim avatar Mar 17 '23 02:03 hugetim

Ah, that's it all right! Works fine without echo = None. How unfortunate. But at least that means there's a workaround.

ideabucket avatar Mar 17 '23 04:03 ideabucket

Yes, that's good news. That means I should be able to make it work with echo=None, too, then. I've got an idea now: it can be implemented in a different way that I perhaps prematurely judged too complicated.

hugetim avatar Mar 17 '23 12:03 hugetim

For now, I pushed a temporary fix that just switches any cell with preserve or restore to "echo" mode automatically.

hugetim avatar Mar 18 '23 03:03 hugetim

Not planning to do anything further on this. Once Stata 18.5 is supported (#45), with its native support for no echo, it should no longer be an issue anyhow.

hugetim avatar Oct 28 '24 14:10 hugetim

i would also suggest using frames for the same purpose. that way the original data is not changed

On Mon, Oct 28, 2024 at 10:55 AM Tim Huegerich @.***> wrote:

Not planning to do anything further on this. Once Stata 18.5 is supported, with its native support for no echo, it should no longer be an issue anyhow.

— Reply to this email directly, view it on GitHub https://github.com/hugetim/nbstata/issues/13#issuecomment-2441822270, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASZKKFSW24WM3K7OOJMSRUTZ5ZF47AVCNFSM6AAAAABQXTXRX2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINBRHAZDEMRXGA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

friosavila avatar Oct 28 '24 15:10 friosavila