我喜欢ipython笔记本中的R魔法。我不得不切换到新的服务器,并听说通过anaconda安装一切可以解决一个巨大的问题。但是,R magic似乎不起作用,这是我使用ipython笔记本的原因的一半。
安装anaconda后,我打开一个笔记本,只需键入以下内容即可。
%load_ext rmagic
%%R
print("hi")
这会产生一个运行时错误,我无法触及底部:
--------------------------------------------------------------------------
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)
我重新启动服务器以确保运行的唯一R实例就是这个。 Rpy2工作,因为我可以使用这个库打印出情节,但是R magic总是产生这个RunTimeError。
任何帮助?
(我不确定是否应该在SO或其他交易所发布此内容,但根据我得到的答案,我会适应)
答案 0 :(得分:4)
我不确定此advice from another thread是否有帮助,但似乎newer versions of rpy2不再使用%load_ext rmagic
。您可以尝试%load_ext rpy2.ipython
。