Skip to content

R interpreter bridge

This notebook is generated from r_bridge_example.py and demonstrates sending a small table to R via RInterpreter and reading it back as an emzed.Table.

import emzed


def main():
    with emzed.r.RInterpreter(do_log=False) as rip:
        print()

        rip.mz0 = 100
        rip.execute(
            """
            tab <- data.frame(
                mz = c(mz0, 2*mz0, 3*mz0),
                intensity = c(1000, 1500, 1300)
            )
            print(tab)

            other_var <- 17
            mz0 <- mz0 + 1
            """
        )
        mz0 = rip.mz0
        other_var = rip.other_var
        print(f"mz0 from R: {mz0}")
        print(f"other_var from R: {other_var}")
        print()
        table = rip.get_df_as_table("tab")
        print(table)


if __name__ == "__main__":
    main()
[R stdout]    mz intensity
[R stdout] 1 100      1000
[R stdout] 2 200      1500
[R stdout] 3 300      1300
mz0 from R: 101.0
other_var from R: 17.0

mz           intensity
float        float
-----------  -----------
 100.000000  1000.000000
 200.000000  1500.000000
 300.000000  1300.000000