【问题标题】:ggvis in jupyter/ipython notebookjupyter/ipython 笔记本中的 ggvis
【发布时间】:2015-04-01 08:47:02
【问题描述】:

可以使用rmagic 来使用 R 在 ipython 笔记本中绘图。

示例用法

%%R 
ggplot() + geom_point(data=ChickWeight, aes(Time, weight))

ggplot2 的下一个演变是ggvis,它支持交互式图形。所以我天真地尝试在笔记本中运行 ggvis 代码。

代码

%%R 
library(ggvis) 
library(dplyr) 
ChickWeight %>% ggvis(~Time, ~weight) %>% layer_points() 

错误

---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-34-d92c06ea4af8> in <module>()
----> 1 get_ipython().run_cell_magic(u'R', u'', u'# ChickWeight %>% head\nChickWeight %>% ggvis(~Time, ~weight) %>% layer_points()')

/opt/virtualenvs/test/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2259             magic_arg_s = self.var_expand(line, stack_depth)
   2260             with self.builtin_trap:
-> 2261                 result = fn(magic_arg_s, cell)
   2262             return result
   2263 

/opt/virtualenvs/test/lib/python2.7/site-packages/rpy2/ipython/rmagic.pyc in R(self, line, cell, local_ns)

/opt/virtualenvs/test/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):

/opt/virtualenvs/test/lib/python2.7/site-packages/rpy2/ipython/rmagic.pyc in R(self, line, cell, local_ns)
    640                     old_writeconsole = ri.get_writeconsole()
    641                     ri.set_writeconsole(self.write_console)
--> 642                     ro.r.show(result)
    643                     text_output += self.flush()
    644                     ri.set_writeconsole(old_writeconsole)

/opt/virtualenvs/test/lib/python2.7/site-packages/rpy2/robjects/functions.pyc in __call__(self, *args, **kwargs)
    168                 v = kwargs.pop(k)
    169                 kwargs[r_k] = v
--> 170         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
    171 
    172 pattern_link = re.compile(r'\\link\{(.+?)\}')

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

RRuntimeError: Error in view_static(x, ...) : unused argument (useS4 = FALSE)

此代码在 Rstudio 中运行良好。

是否可以在 ipython 笔记本中运行 ggvis (或任何闪亮的东西,我认为这就是问题所在)?

【问题讨论】:

  • ggvis 可能正在做 R 控制台特定的黑魔法。最好是在 bitbucket 上的 rpy2 问题跟踪器上打开一个问题。

标签: r ipython-notebook ggvis jupyter


【解决方案1】:

长话短说,您要么显式调用 R 的 print 方法,要么为 ipython 自定义 rpy2 的转换规则(此处未显示)。

%%R 
library(ggvis) 
library(dplyr) 
p <- ChickWeight %>% ggvis(~Time, ~weight) %>% layer_points() 
print(p)

请注意,获得的图与您的 ipython 笔记本在一个不同页面中,这有点打破了笔记本是独立的想法。

【讨论】:

  • 是否没有已知的解决方法,或者理论上甚至不可能解决这个问题?
  • 解决这个问题的一种方法是使用 rpy2 的转换系统返回一个 ipython 将理解为图形的对象(参见nbviewer.ipython.org/github/FRidh/ipython/blob/1.x/examples/…)。
  • 是否需要保存到p?你能把print 改成... %&gt;% print 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2015-05-12
  • 1970-01-01
  • 2017-03-22
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多