How to use rpy2 - 10 common examples

To help you get started, we’ve selected a few rpy2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jonathan-taylor / formula / tests / test_design.py View on Github external
X = random_from_categorical_formula(d, size)

    X = ML.rec_append_fields(X, 'response', np.random.standard_normal(size))

    fname = tempfile.mktemp()
    ML.rec2csv(X, fname)

    Rstr = '''
    data = read.table("%s", sep=',', header=T)
    cur.lm = lm(response ~ %s, data)
    COEF = coef(cur.lm)
    ''' % (fname, d.Rstr)

    rpy2.robjects.r(Rstr)
    remove(fname)
    nR = list(np.array(rpy2.robjects.r("names(COEF)")))

    nt.assert_true('(Intercept)' in nR)
    nR.remove("(Intercept)")
    nF = [str(t).replace("_","").replace("*",":") for t in d.formula.terms]
             
    nR = sorted([sorted(n.split(":")) for n in nR])

    nt.assert_true('1' in nF)
    nF.remove('1')

    nF = sorted([sorted(n.split(":")) for n in nF])
    nt.assert_equal(nR, nF)

    return d, X, nR, nF
github CGATOxford / cgat / scripts / testPleiotropy.py View on Github external
E.info("Running {} regression for trait 2: {}".format(model2,
                                                          trait2))

    R('''source("%(scriptsdir)s/PET_functions.R")''' % locals())
    E.info("Pushing data objects into the R environment")
    # push everything into the R environment
    r_df = py2ri.py2ri_pandasdataframe(dataframe)
    R.assign("data.df", r_df)

    r_snps = ro.StrVector([sp for sp in snps])
    R.assign("snp.list", r_snps)

    E.info("Parsing covariates")
    covars = covars.split(",")
    r_covar = ro.StrVector([cv for cv in covars])
    R.assign("covar.list", r_covar)
    E.info("{} covariates found to adjust "
           "in regression  models".format(len(covars)))

    # clean up, replacing "missing values" with NAs for R
    R('''data.df[data.df == -9] <- NA''')
    R('''pet_results <- list()''')

    # loop over all SNP, calculate PCC and p-value
    # this takes a long time <- need to think of speed ups
    # possible Python-pure implementation, i.e. with LIMIX?
    E.info("Iteratively calculating PCC for all SNPs")
    R('''results <- loopPET(data.df=data.df, trait1="%(trait1)s", trait2="%(trait2)s", '''
      '''trait1.link=trait1.link, trait2.link=trait2.link, '''
      '''trait1.mod=trait1.mod, trait2.mod=trait2.mod, covars=covar.list,'''
      '''resamples=%(resamples)i, snp.list=snp.list)''' % locals())
github h2oai / h2o-2 / py / testdir_rpy2 / test_rpy2_1.py View on Github external
print result.r['p.value'][0][0]

# t test
a = [1,2,3,4]
b = [2,3,4,5]
result = R.r['t.test'](R.IntVector(a),R.IntVector(b),alternative='two.sided',paired=True)
print result.r['p.value'][0][0]
print result.r['estimate'][0][0]
# for paired test, if estimate > 0, then a is greater than b, else, b is greater than a (mean)


# FDR correction
# the pvalue_lst has to be FloatVector
# first we might have a list of test result objects, make pvalue list first
pvalue_lst = [v.r['p.value'][0][0] for v in testResult_lst]
p_adjust = R.r['p.adjust'](R.FloatVector(pvalue_lst),method='BY')
for v in p_adjust:
    print v
github rpy2 / rpy2 / tests / rinterface / test_na.py View on Github external
def test_bool_NAComplex():
    with pytest.raises(ValueError):
        bool(ri.NA_Complex)
github rpy2 / rpy2 / tests / rinterface / test_vector_str.py View on Github external
def test_getitem():
    vec = ri.StrSexpVector(['foo', 'bar', 'baz'])
    assert vec[1] == 'bar'
    with pytest.raises(TypeError):
        vec[(2, 3)]
github h2oai / h2o-2 / py / testdir_rpy2 / test_rpy2_1.py View on Github external
print "pi"
print type(r)
pi = r['pi']
print pi[0]
print type(pi)

print "piplus"
piplus = r('piplus = pi + 1')
print type (piplus)
print piplus
print piplus[0]


# some simple R things
print R.r.median(R.IntVector([1,2,3,4]))[0]

# create two R vectors and do correlation coefficient in R
a = R.IntVector([1,2,3,4])
b = R.IntVector([1,2,3,4])

# need the subscript to get authentic python type?
print R.r.cor(a,b,method="pearson")[0]

my_vec = R.IntVector([1,2,3,4])
my_chr_vec = R.StrVector(['aaa','bbb'])
my_float_vec = R.FloatVector([0.001,0.0002,0.003,0.4])

print "\nconvert to numpy array?"
vector = rpyn.ri2numpy(my_float_vec)
print vector
github rpy2 / rpy2 / tests / robjects / test_dataframe.py View on Github external
def test_init_stringsasfactors():
    od = {'a': robjects.IntVector((1,2)),
          'b': robjects.StrVector(('c', 'd'))}
    dataf = robjects.DataFrame(od, stringsasfactor=True)
    assert isinstance(dataf.rx2('b'), robjects.FactorVector)
    dataf = robjects.DataFrame(od, stringsasfactor=False)
    assert isinstance(dataf.rx2('b'), robjects.StrVector)
github rpy2 / rpy2 / tests / robjects / test_vector.py View on Github external
def test_sample():
    vec = robjects.IntVector(range(100))
    spl = vec.sample(10)
    assert len(spl) == 10
github rpy2 / rpy2 / tests / robjects / test_array.py View on Github external
def test_tcrossprod():
    m = robjects.r.matrix(robjects.IntVector(range(4)), nrow=2)
    mtcp = m.tcrossprod(m)
    for i,val in enumerate((4,6,6,10,)):
        assert mtcp[i] == val
github rpy2 / rpy2 / tests / rinterface / test_vector_pairlist.py View on Github external
def test_init_from_r():
    pairlist = ri.baseenv.find('pairlist')
    pl = pairlist(a=ri.StrSexpVector(['1', ]),
                  b=ri.StrSexpVector(['3', ]))
    assert pl.typeof == ri.RTYPES.LISTSXP