The language wars are over.
wrestlr uses python to do a two step dance:
⚠️ This library is highly experimental. I am actively using it to understand how far I can push translation. The interface and behaviors may change.
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')
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()
<ggplot: (-9223372036562628583)>
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 |