def test_mutate(self):
     dataf_a = dplyr.DataFrame(mtcars)
     dataf_b = dataf_a.mutate(foo=1, bar=rl('gear+1'))
     assert type(dataf_b) is dplyr.DataFrame
     assert all(a == b
                for a, b in zip(dataf_a.rx2('gear'), dataf_b.rx2('gear')))
     assert all(a + 1 == b
                for a, b in zip(dataf_a.rx2('gear'), dataf_b.rx2('bar')))
Exemple #2
0
TARGET_VERSION = '3.3.'
if not ggplot2.__version__.startswith(TARGET_VERSION):
    warnings.warn(
        'This was designed againt ggplot2 versions starting with %s but you '
        'have %s' % (TARGET_VERSION, ggplot2.__version__))
ggplot2_env = robjects.baseenv['as.environment']('package:ggplot2')

StrVector = robjects.StrVector


def as_symbol(x):
    return rlang.sym(x)


_AES_RLANG = rl('ggplot2::aes()')


class GGPlot(robjects.vectors.ListVector):
    """ A Grammar of Graphics Plot.

    GGPlot instances can be added to one an other in order to construct
    the final plot (the method `__add__()` is implemented).
    """

    _constructor = ggplot2._env['ggplot']
    _rprint = ggplot2._env['print.ggplot']
    _add = ggplot2._env['%+%']

    @classmethod
    def new(cls, data, mapping=_AES_RLANG, **kwargs):
Exemple #3
0
 def test_as_labeller(self, labeller):
     if isinstance(labeller, dict):
         labeller = ggplot2.dict2rvec(labeller)
     gp = (ggplot2.ggplot(mtcars) + ggplot2.facet_wrap(
         rl('~am'), labeller=ggplot2.as_labeller(labeller)))
     assert isinstance(gp, ggplot2.GGPlot)
Exemple #4
0
class TestGGplot(object):
    def test_gglot(self):
        gp = ggplot2.ggplot(mtcars)
        assert isinstance(gp, ggplot2.GGPlot)

    def test_gglot_mapping(self):
        gp = ggplot2.ggplot(mtcars, ggplot2.aes_string(x='gear'))
        assert isinstance(gp, ggplot2.GGPlot)

    def test_element_text(self):
        et = ggplot2.element_text()
        assert isinstance(et, ggplot2.ElementText)

    def test_element_text_repr(self):
        et = ggplot2.element_text()
        assert repr(et).startswith('<instance of')

    def test_element_rect(self):
        er = ggplot2.element_rect()
        assert isinstance(er, ggplot2.ElementRect)

    def test_element_blank(self):
        eb = ggplot2.element_blank()
        assert isinstance(eb, ggplot2.ElementBlank)

    def test_element_line(self):
        eb = ggplot2.element_line()
        assert isinstance(eb, ggplot2.ElementLine)

    def test_labs(self):
        la = ggplot2.labs()
        assert isinstance(la, ggplot2.Labs)

    def test_add(self):
        gp = ggplot2.ggplot(mtcars)
        gp += ggplot2.aes_string(x='wt', y='mpg')
        gp += ggplot2.geom_point()
        assert isinstance(gp, ggplot2.GGPlot)

    def test_aes(self):
        gp = ggplot2.ggplot(mtcars)
        gp += ggplot2.aes(x='wt', y='mpg')
        gp += ggplot2.geom_point()
        assert isinstance(gp, ggplot2.GGPlot)

        gp = ggplot2.ggplot(mtcars)
        gp += ggplot2.aes('wt', 'mpg')
        gp += ggplot2.geom_point()
        assert isinstance(gp, ggplot2.GGPlot)

    def test_vars(self):
        gp = (ggplot2.ggplot(mtcars) + ggplot2.aes(x='wt', y='mpg') +
              ggplot2.geom_point() + ggplot2.facet_wrap(ggplot2.vars('gears')))
        assert isinstance(gp, ggplot2.GGPlot)

    @pytest.mark.parametrize('theme_name', [
        'theme_grey', 'theme_classic', 'theme_dark', 'theme_grey',
        'theme_light', 'theme_bw', 'theme_linedraw', 'theme_void',
        'theme_minimal'
    ])
    def test_theme(self, theme_name):
        theme = getattr(ggplot2, theme_name)
        gp = (ggplot2.ggplot(mtcars) + theme())
        assert isinstance(gp, ggplot2.GGPlot)

    @pytest.mark.parametrize(
        'labeller', (rl('as_labeller(c(`0` = "Zero", `1` = "One"))'), {
            '0': 'Zero',
            '1': 'One'
        }))
    def test_as_labeller(self, labeller):
        if isinstance(labeller, dict):
            labeller = ggplot2.dict2rvec(labeller)
        gp = (ggplot2.ggplot(mtcars) + ggplot2.facet_wrap(
            rl('~am'), labeller=ggplot2.as_labeller(labeller)))
        assert isinstance(gp, ggplot2.GGPlot)
Exemple #5
0
 def group_by(self, *args, _add=False,
              _drop=robjects.rl('group_by_drop_default(.data)')):
     """Call the R function `dplyr::group_by()`."""
     res = dplyr.group_by(self, *args,
                          **{'.add': _add, '.drop': _drop})
     return GroupedDataFrame(res)
 def test_mutate_all(self):
     dataf_a = dplyr.DataFrame(mtcars)
     dataf_b = dataf_a.mutate_all(rl('sqrt'))
     assert type(dataf_b) is dplyr.DataFrame
 def test_mutate_at(self):
     dataf_a = dplyr.DataFrame(mtcars)
     dataf_b = dataf_a.mutate_at(StrVector(["gear"]), rl('sqrt'))
     assert type(dataf_b) is dplyr.DataFrame
 def test_splitmerge_function(self):
     dataf = dplyr.DataFrame(mtcars)
     dataf_by_gear = dataf.group_by(rl('gear'))
     dataf_avg_mpg = dataf_by_gear.summarize(foo=rl('mean(mpg)'))
     assert isinstance(dataf_avg_mpg, dplyr.DataFrame)
 def test_group_by(self):
     dataf_a = dplyr.DataFrame(mtcars)
     dataf_g = dataf_a.group_by(rl('gear'))
     assert dataf_g.is_grouped_df
     assert not dataf_g.ungroup().is_grouped_df
     assert dataf_g.is_grouped_df
 def test_filter_onefilter_function(self):
     dataf = dplyr.DataFrame(mtcars)
     ngear_gt_3 = len(tuple(x for x in dataf.rx2('gear') if x > 3))
     dataf_filter = dplyr.filter(dataf, rl('gear > 3'))
     assert ngear_gt_3 == dataf_filter.nrow
 def test_arrange(self):
     dataf = dplyr.DataFrame(mtcars)
     dataf_arrange = dataf.arrange(rl('mpg'))
     assert tuple(sorted(dataf.collect().rx2('mpg'))) == \
         tuple(dataf_arrange.collect().rx2('mpg'))
Exemple #12
0
 def ungroup(self,
             *args,
             _add=False,
             _drop=robjects.rl('group_by_drop_default(.data)')):
     res = dplyr.ungroup(*args, _add=_add, _drop=_drop)
     return guess_wrap_type(res)(res)