rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.68k stars 327 forks source link

Suggestion : support running Sagemath as a python engine. #327

Open EmmanuelCharpentier opened 6 years ago

EmmanuelCharpentier commented 6 years ago

Sagemath (a. k. a. Sage) is a mathematics software written in (and close to) Python. Its interactive environment is IPython, and it accepts grosso modo almost any legan python code (Python 2 at the moment, but support for Python3 is coming). Its REPL preprocesses inputs (for example, 2^3 is interpreted as 2**3). Of course, it extends python a lot in the math department.

Having reticulate support it would make R users able to use Sage symbolic and algebraic abilities. For example, a paper written with knitr or Rmarkdown could also use Sage for symbolic calculations supporting the R code.

Something like this can currently be (awkwardly) done by using both knitr and either Sagetex or Pythontex from a .Rnw file. Using this is a bit involved. it implies :

Doing this from .Rmd file is even more awkward : the relevant Sage chunks can be wrapped in \LaTeX-only chunks (you end the chunk with {=latex}), and you have to manually run Sage or Pythontex and \Latex again...

With a Sage-enabled reticulate, the normal mechanisms for knitting should suffice. Much simpler...

(The only alternative I am aware of is to use sympy from Python. This can be done already, but entails losing a lot of Sage-specific abilities (as well as those all the subsystems Sage can use, such as Maxima, Pari, Singular (and sympy itself), or even Maple, Mathematica or Fricas).

I checked that running reticulate by passing it the location of the shell script that starts Sage is not enough : one gets an interaction with the Python interpreter supporting Sage :

> library(reticulate)
> use_python("/usr/local/sage-8/sage")
> repl_python()
Python 2.7.15 (/usr/local/sage-8/sage)
Reticulate 1.9 REPL -- A Python interpreter in R.
>>> 2+3
5

The Python interpreter seems to work. But this is not enough to get Sage working :

>>> from sage.all import *
TypeError: sequence item 0: expected string, NoneType found
>>> 2^3
1

We are still in Python, no preprocessing

>>> sage.var("x")
NameError: name 'sage' is not defined

and the import really failed

>>> exit
> 

At least, this didn't seem to cause trouble on R's side...

Thoughts ?

EmmanuelCharpentier commented 6 years ago

Addition : a start in the right direction is discussed in this ask.sagemath.org question. TL;DR : it isn't sufficient (ATM, we are able to start a Sage IPython session, which can't be restored successfully after exit), but I have hints that it might work if we can manage to do what the IPython %load_ext sage statement does in a Python session...

Complementary question : could the Python (Sage, in my case) object interfacing Python to the calling R session be renamed (at least for Sage sessions) from r to, say, r_caller ? r is used to interface with an "inferior R" called from Sage, and has some mighty ties with the rest of the system (and R is semi-officially reserved for interaction with the R library via Rpy2). This renaming would also allow the re-use of old code without fear of name clashes...