Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/machow/wrestlr

Why debate R vs python, when you can convert R to python?
https://github.com/machow/wrestlr

Last synced: 3 months ago
JSON representation

Why debate R vs python, when you can convert R to python?

Awesome Lists containing this project

README

        

# wrestlr

[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/machow/wrestlr/master)

The language wars are over.

wrestlr uses python to do a two step dance:

* parse R code that uses [dplyr](https://github.com/tidyverse/dplyr).
* spit out python code that runs using the dplyr port [siuba](http://github.com/machow/siuba).

## Basic use

```{python}
import wrestlr

r_code = """
mtcars %>%
filter(hp < 200)
"""

wrestlr.rlang_convert(r_code)
```

It's much nicer to use IPython cell magic, though!

```{python}
import wrestlr
# %load_ext wrestlr
```

```{python}
# %%wrestlr --print
1

'a'

TRUE

NULL

x$y

x[["y"]]
```

Here is another example with ggplot.

```{python}
# %%wrestlr --print
mtcars %>%
select(hp, mpg, cyl) %>%
ggplot(aes(hp, mpg)) +
geom_point() +
facet_wrap(~cyl)
```

## Executing

Instead of printing code, you can execute it using the `--execute` option. First, we'll import some python functions.

```{python}
# import wrestlr
# # %load_ext wrestlr

import pandas as pd

from siuba import _, mutate, group_by, ungroup
from siuba.data import mtcars

from plotnine import *

def factor(x):
return pd.Categorical(x)
```

Next we convert and execute the code.

```{python}
# %%wrestlr --print --execute --black

mtcars %>%
group_by(cyl) %>%
mutate(demeaned_mpg = mpg - mean(mpg)) %>%
ungroup() %>%
ggplot(aes(factor(cyl), demeaned_mpg)) +
geom_boxplot()

```

## Learning more

See these example notebooks

| name | binder | description |
| ---- | ------ | ----------- |
| [gallery](docs/gallery.ipynb) | [![](https://mybinder.org/badge_logo.svg)][b-gallery] | Walk through rules wrestlr uses during conversion |
| [cell_magic](docs/cell_magic.ipynb) | [![](https://mybinder.org/badge_logo.svg)][b-cell-magic] | Get to know the %%wrestlr cell magic |
| [debugging.ipynb](docs/debugging.ipynb) | [![](https://mybinder.org/badge_logo.svg)][b-debugging] | Debugging the parser, AST, or siuba conversion |
| [translate-tidytuesday](docs/translate-tidytuesday.ipynb) | [![](https://mybinder.org/badge_logo.svg)][b-translate-tidytuesday] | Translating and executing the first half of a tidy tuesday R analysis |

[b-gallery]: https://mybinder.org/v2/gh/machow/wrestlr/master?filepath=docs/gallery.ipynb
[b-cell-magic]: https://mybinder.org/v2/gh/machow/wrestlr/master?filepath=docs/cell_magic.ipynb
[b-debugging]: https://mybinder.org/v2/gh/machow/wrestlr/master?filepath=docs/debugging.ipynb
[b-translate-tidytuesday]: https://mybinder.org/v2/gh/machow/wrestlr/master?filepath=docs/translate-tidytuesday.ipynb