twibiral / obsidian-execute-code

Obsidian Plugin to execute code in a note.
MIT License
994 stars 61 forks source link

environment pane #325

Open MichelNivard opened 6 months ago

MichelNivard commented 6 months ago

Hi,

as an Rstudio developer I really love my "environment pane": image

It basically tracks al objects you generate when executing code. I love it so much that I want to build one for Obsidian/Execute-code, where it would track the variables in the environment as chunks are evaluated (if in notebook mode, obviously).

I think i can do this by modifying your "execution runtimes" pane and populating it with the outcome af a 2/3 line R script that aggregates the list of objects in memory, their class, and dimensions. Could you give me pointers as to where the code for the current panel that displays the runtimes lives? If this kind of modificaiton violates the license of your plugin then sorry forget I ever asked.

Best, Michel

twibiral commented 6 months ago

Hi, I think that's a great idea!

The runtime environments are for python and js inherit ReplExecutor (in the PythonExecutor.ts and NodeJSExecutor.ts files respectively under src/executors/). The side panel is implemented in ExecutorManagerView.ts.

You can add you overview directly under the R instance in the side panel (You have to implement a new ReplExecutor for R). If you know how to execute R code in Read-Execute-Print-Loop (REPL) fashion, that you can pretty much take the python executor and adapt it for R.

Feel free to open up a pull request early and ping me if you need any help.

MichelNivard commented 6 months ago

Thanks, ill try fiddling around with it, would it be desirable if I try to make the REPL/Rscript behaviour more closely mirror the behaviour of the R console as implemented in the base-R GUI, and/or Rstudio? your current R RE(P)L returns objects (lists, data.frames etc) "as-is" where the R console generally present the user with print(object), defaulting to the print class for the specific object, for me the code chunk also results the last object created, even if the user assigns it. Again these might be conscious choices on your part and I don't want blindly implement what other R console's/markdown processors do without checking in

twibiral commented 6 months ago

To be honest, I don't really use R and I also didn't implement the current implementation for the plugin.

You seem much more knowledgeable about R than me, so if you think there is a better or more common way to present objects, then feel free to implement it that way. Generally, I tried to make the plugin as simple and easy to use as possible so that it will be helpful to people learning to program or just taking quick notes.

MichelNivard commented 5 months ago

So here is where I am at, I did some basic cleanup, which makes the plugin's R REPL behave far more like the original/official R GUI and RStudio. See the side by side screen capture I link to below

However to achieve this, I did have to introduce dependence on an R package, is that a problem? If so ill have to workaround it. the package will have many advantages, it will also let me "catch" any plots (whether made with plot() or in another way) and I can save these all to tmp files, and eventually display them to the user.

Without too much modification I can also let users use R functions that output tables in markdown format directly. However, this would mean I need to circumvent the current "magic" code that finds R plots, and implement a handler for output that neither text nor plot (i.e. tables etc).

let me know if anything I did so far would be objectionable.

https://github.com/twibiral/obsidian-execute-code/assets/11858442/5928d5e7-c27e-4ea6-84c9-e9b1729e1275

MichelNivard commented 5 months ago

Left is current behaviour, right is my dev fork... The last chunk not printing anything is expected R behaviour BTW.

twibiral commented 1 month ago

Wow, your version looks much better! Sounds great to me, I think the dependency is totally fine, we should put a note about this in the readme.

Feel free to open a pull request and mark it as draft until it is ready, then I can take a look at it

MichelNivard commented 1 month ago

Hi, I had let this languish a bit for the lack of responses, I hadn't quite got some of the pieces to work (especially the notebook like functionality where all chunks share a ingle workspace). Ill get back into it and let you know when its ready.

twibiral commented 1 month ago

Hi, great to hear! The notebook functionality is already implemented for JavaScript and for Python, maybe this implementations can help?

yihui commented 2 weeks ago

@MichelNivard Just FYI, you may also consider using xfun::record(), which will save plots to files directly instead of recording them as display lists.

MichelNivard commented 2 weeks ago

Hi @yihui thanks, thats a great idea. I am comfortable with the R/RMarkdown/evaluate end of things (the packages are very well written thanks!) i am just severely out of my depth with js/ts. So while I can write a very basic slightly better R REPL in this obsidian plugin, I was stuck on getting it to work in notebook mode (where all chucks in one doc share a workspace) or getting the outputted plots to show.. If my day job allows me the time I might give xfun::record() a spin, but it fundamentally comes down to me not being a ts developer at all. IT seems to me almost all changes needed could be contained in: https://github.com/twibiral/obsidian-execute-code/blob/master/src/executors/RExecutor.ts

Anyway, smoother plot/output handling for R in this plugin all sounds like something you could probably get working in an afternoon, so if you're interested please! I feel Obsidian with just better RMarkdown like features would be ultimate literate programming endgame, dont even think you'd need an environment pane (it be neat though...). My last fork for reference: https://github.com/MichelNivard/obsidian-execute-code