rcst / rim

rim provides an interface to Maxima for R. Maxima is a powerful and fairly complete computer algebra system.
10 stars 1 forks source link

Unable to run 'Maxima' code chunks #41

Open asadrh8 opened 1 month ago

asadrh8 commented 1 month ago

While attempting to use the rim package in R markdown to run chunks of maxima code, I have been running into the following error, which is difficult to follow up on:

image

This chunk has been used directly from the provided test code, so I imagine this is likely a bug or perhaps a local problem.

Any guidance would be appreciated!

rcst commented 1 month ago

Thanks for reporting.

Please paste here the full RMarkdown file content as well as your output from sessionInfo() and maxima.version(), so I can try to replicate the error.

asadrh8 commented 1 month ago

I've attached a PDF containing the knitted Rmd file with output from sessionInfo() and maxima.version(). The code chunk for maxima doesn't produce the same error when knitted, but does so in RStudio itself. rim_maxima_test.pdf

rcst commented 1 month ago

Thank you. Unfortunately I don't use MacOS, but it seems that this is not OS specific. Does the error occur when you try to knit either file docs/index.Rmd or inst/extdata/test.Rmd?

Does the error occur when you are knitting the entire document or are you possibly running this code-chunk individually from RStudio IDE?

Does the error also occur when you name the chunk that produces the error? E.g.:

```{maxima name-of-first-chunk}
L: sqrt(1 - 1/R^2);
assume(R>0);


PS: Just to be sure, your above error comes from using rim's knitr-engine. Running Maxima code via `maxima.get()` inside a R chunk does **not** use the knitr-engine.
rcst commented 1 month ago

I pushed a fix. Could you try installing from this repo and test it?

remotes::install_github("rcst/rim")
asadrh8 commented 1 month ago

> I pushed a fix. Could you try installing from this repo and test it?

I've tried reinstalling the package but am still met with the same error, unfortunately.

> Thank you. Unfortunately I don't use MacOS, but it seems that this is not OS specific. Does the error occur when you try to knit either file docs/index.Rmd or inst/extdata/test.Rmd?

Both of those files when knitted don't produce any errors. I can attach the knitted files if that may be helpful in any way.

> Does the error occur when you are knitting the entire document or are you possibly running this code-chunk individually from RStudio IDE?

Yes, the error occurs when running the code-chunks individually from the RStudio.

> Does the error also occur when you name the chunk that produces the error? E.g.:

L: sqrt(1 - 1/R^2);
assume(R>0);

Yep, unfortunately that makes no difference. I've tried playing around with assigning inline/engine labels as TRUE or FALSE under maxima.options(), and that hasn't made a difference either.

> PS: Just to be sure, your above error comes from using rim's knitr-engine. Running Maxima code via maxima.get() inside a R chunk does not use the knitr-engine.

Yes, that makes sense.

rcst commented 1 month ago

I see now what the problem is:

When you run an individual code chunk from RStudio by clicking on the chunk's play-button, the code is sent to the rim knitr engine. However, every time a code chunk is processed the engine checks (a) whether a dedicated Maxima process is running and if not will start it and (b) whether the current code chunk is the last Maxima code chunk in the document. If the current Maxima code chunk is indeed the last one, the dedicated Maxima process will be terminated after the code was processed. This approach ensures reproducible output.

However, if you press the "play button", the Maxima knitr engine cannot determine if the code chunk is the last one, since it's "not aware" of the other code chunks.

I see now two possibilities to fix this: Making the knitr engine aware that the play button was pressed and instead of creating and stopping a new dedicated Maxima process, send the code to the "interactive" Maxima process (the one used by functions maxima.start(), maxima.stop(), maxima.get() and maxima.eval().

What do you think?

To me this seems intuitive, it seems that the play button's purpose is to experiment with code chunks before finally knitting the whole document.

asadrh8 commented 1 month ago

Ah! That makes a lot of sense. I think that sending the code to the "interactive" Maxima process seem like the most reasonable solution. The only alternative I can think of is having an optional command for the user to somehow provide the argument of whether a chunk is the last chunk or not, but that seems unnecessary.

I think that having the ability to play with code chunks will be useful as I imagine much of my usage (and perhaps many others) will be of the 'rim' package in code chunks within RMarkdown.

rcst commented 1 week ago

@asadrh8 I pushed a fix for this. Could you try install from this repo from branch "issue41" and test?

Thanks for your patience.