R magic for ipython notebook runtime error turning

2019-08-02 12:33发布

问题:

I loved the R magic in ipython notebook. I had to switch to a new server and heard that a huge headache can be taken out by installing everything through anaconda. However, R magic doesn't seem to work, which is half the reason I use ipython notebook.

After installing anaconda, I open a notebook and simply type the following.

%load_ext rmagic
%%R
print("hi")

This produces a Runtime error I can't get to the bottom of:

--------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-6-89259627ca18> in <module>()
----> 1 get_ipython().run_cell_magic(u'R', u'', u'print("hi")')

/nas1/apps/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2160             magic_arg_s = self.var_expand(line, stack_depth)
   2161             with self.builtin_trap:
-> 2162                 result = fn(magic_arg_s, cell)
   2163             return result
   2164 

/nas1/apps/anaconda/lib/python2.7/site-packages/IPython/extensions/rmagic.pyc in R(self, line, cell, local_ns)

/nas1/apps/anaconda/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/nas1/apps/anaconda/lib/python2.7/site-packages/IPython/extensions/rmagic.pyc in R(self, line, cell, local_ns)
    626             return
    627         finally:
--> 628             self.r('dev.off()')
    629 
    630         # read out all the saved .png files

/nas1/apps/anaconda/lib/python2.7/site-packages/rpy2/robjects/__init__.pyc in __call__(self, string)
    244     def __call__(self, string):
    245         p = rinterface.parse(string)
--> 246         res = self.eval(p)
    247         return res
    248 

/nas1/apps/anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs)
    164                 v = kwargs.pop(k)
    165                 kwargs[r_k] = v
--> 166         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
    167 
    168 pattern_link = re.compile(r'\\link\{(.+?)\}')

/nas1/apps/anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs)
     97         for k, v in kwargs.items():
     98             new_kwargs[k] = conversion.py2ri(v)
---> 99         res = super(Function, self).__call__(*new_args, **new_kwargs)
    100         res = conversion.ri2ro(res)
    101         return res

RRuntimeError: Error in dev.off() : cannot shut down device 1 (the null device)

I restart my server to assure that the only R instance running is this one. Rpy2 works, as I can print out plots using this library, but the R magic always produces this RunTimeError.

Any help?

(I'm not sure if I should post this on SO or another exchange, but based on what answers I get, I will adapt)

回答1:

I'm not sure if this advice from another thread helps, but seems like newer versions of rpy2 do not use %load_ext rmagic anymore. You might want to try %load_ext rpy2.ipython.