Skip to content
/ wrestlr Public

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

Notifications You must be signed in to change notification settings

machow/wrestlr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wrestlr

Binder

The language wars are over.

wrestlr uses python to do a two step dance:

  • parse R code that uses dplyr.
  • spit out python code that runs using the dplyr port siuba.

⚠️ This library is highly experimental. I am actively using it to understand how far I can push translation. The interface and behaviors may change.

Basic use

import wrestlr

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

wrestlr.rlang_convert(r_code)
mtcars >> filter(_.hp < 200)

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

import wrestlr
%load_ext wrestlr
%%wrestlr --print
1

'a'

TRUE

NULL

x$y

x[["y"]]
1

'a'

True

None

x.y

x['y']

Here is another example with ggplot.

%%wrestlr --print
mtcars %>%
  select(hp, mpg, cyl) %>%
  ggplot(aes(hp, mpg)) +
  geom_point() +
  facet_wrap(~cyl)
(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.

# 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.

%%wrestlr --print --execute --black

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

png

<ggplot: (-9223372036562628583)>

Learning more

See these example notebooks

name binder description
gallery Walk through rules wrestlr uses during conversion
cell_magic Get to know the %%wrestlr cell magic
debugging.ipynb Debugging the parser, AST, or siuba conversion
translate-tidytuesday Translating and executing the first half of a tidy tuesday R analysis

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published