#11266: Wrapper for R graphics commands.
-------------------------------------------------+-------------------------
       Reporter:  JoalHeagney                    |        Owner:  was
           Type:  enhancement                    |       Status:  new
       Priority:  minor                          |    Milestone:  sage-6.4
      Component:  interfaces                     |   Resolution:
       Keywords:  R, graphics, cairo, wrapper,   |    Merged in:
  r-project                                      |    Reviewers:
        Authors:                                 |  Work issues:
Report Upstream:  N/A                            |       Commit:
         Branch:                                 |     Stopgaps:
   Dependencies:                                 |
-------------------------------------------------+-------------------------

Comment (by was):

 I'm happy to share my code for any use.  This is the code I currently use
 in SMC for this purpose.  The line "salvus.stdout('\n'); salvus.file(tmp,
 show=True); salvus.stdout('\n')" would have to change...

 {{{
 # Monkey patch the R interpreter interface to support graphics, when
 # used as a decorator.

 import sage.interfaces.r
 def r_eval0(*args, **kwds):
     return sage.interfaces.r.R.eval(sage.interfaces.r.r, *args,
 **kwds).strip('\n')

 r_dev_on = False
 def r_eval(code, *args, **kwds):
     """
     Run a block of R code.

     EXAMPLES::

          sage: print r.eval("summary(c(1,2,3,111,2,3,2,3,2,5,4))")   #
 outputs a string
          Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
          1.00    2.00    3.00   12.55    3.50  111.00

     In the notebook, you can put %r at the top of a cell, or type
 "%default_mode r" into
     a cell to set the whole worksheet to r mode.

     NOTE: Any plots drawn using the plot command should "just work",
 without having
     to mess with special devices, etc.
     """
     # Only use special graphics support when using r as a cell decorator,
 since it has
     # a 10ms penalty (factor of 10 slowdown) -- which doesn't matter for
 interactive work, but matters
     # a lot if one had a loop with r.eval in it.
     if sage.interfaces.r.r not in salvus.code_decorators:
         return r_eval0(code, *args, **kwds)

     global r_dev_on
     if r_dev_on:
         return r_eval0(code, *args, **kwds)
     try:
         r_dev_on = True
         tmp = '/tmp/' + uuid() + '.svg'
         r_eval0("svg(filename='%s')"%tmp)
         s = r_eval0(code, *args, **kwds)
         r_eval0('dev.off()')
         return s
     finally:
         r_dev_on = False
         if os.path.exists(tmp):
             salvus.stdout('\n'); salvus.file(tmp, show=True);
 salvus.stdout('\n')
             os.unlink(tmp)

 sage.interfaces.r.r.eval = r_eval
 }}}

--
Ticket URL: <http://trac.sagemath.org/ticket/11266#comment:24>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.

Reply via email to