2

rmagicR を ipython ノートブックでのプロットに使用することができます。

使用例

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

の次の進化版ggplot2ggvis、インタラクティブ グラフィックスをサポートする です。そこで、単純にノートブックで 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を実行することは可能ですか(または、そのことについて光沢のあるものは何でも、それが問題だと思います)?

4

1 に答える 1

1

簡単に言うと、R の print メソッドを明示的に呼び出すか、rpy2 の変換規則を ipython 用にカスタマイズする必要があります (ここには示されていません)。

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

得られた図はipython ノートブックとは別のページにあることに注意してください。これは、ノートブックが自己完結型であるという考えを少し壊します。

于 2015-05-25T02:28:00.647 に答える