rpy2

Passing a Python variable to R using rpy2

喜夏-厌秋 提交于 2019-12-20 06:15:47
问题 I have basic R script that performs a GLM on a MySQL dataset. This runs fine using Rscript in bash. However I would like to call it within a python script so I can add it to a loop, I can create the sql statement but I can't seem to pass it to R using rpy2; for word in words: sql_scores = "select a.article_id, response, score from scores as a join profile as b on a.article_id = b.article_id where response in (1,0) and keyword = '%s';" % (word[0]) robjects.r("library(RMySQL)") robjects.r("mydb

Compiling with an SDK that doesn't seem to exist: /Developer/SDKs/MacOSX10.6.sdk

自作多情 提交于 2019-12-20 03:51:19
问题 I'm trying to install rpy2 for Python3 with the following command: easy_install-3.2 rpy2 This gives a few warnings and an error: warning: no previously-included files matching '*patch*' found anywhere in distribution warning: no previously-included files matching '*diff*' found anywhere in distribution warning: no previously-included files matching '.hg' found anywhere in distribution no previously-included directories found matching 'dist' gcc-4.2 not found, using clang instead Compiling

How to set the R_HOME environment variable to the R home directory?

最后都变了- 提交于 2019-12-19 02:39:17
问题 I am trying to import rpy2 into python and I am getting this error: RuntimeError: The R home directory could not be determined. Try to install R <https://www.r-project.org/>, set the R_HOME environment variable to the R home directory, or add the directory of the R interpreter to the PATH environment variable. I am completely lost here as I have no idea what this is telling me. I tried to search up how to set the environment variable and couldn't find anything detailed enough that was helpful

aligning distinct non-facet plots in ggplot2 using Rpy2 in Python

人走茶凉 提交于 2019-12-18 15:00:36
问题 I am combining two distinct plots into a grid layout with grid as suggested by @lgautier in rpy2 using python. The top plot is a density and and the bottom a bar graph: iris = r('iris') import pandas # define layout lt = grid.layout(2, 1) vp = grid.viewport(layout = lt) vp.push() # first plot vp_p = grid.viewport(**{'layout.pos.row': 1, 'layout.pos.col':1}) p1 = ggplot2.ggplot(iris) + \ ggplot2.geom_density(aes_string(x="Sepal.Width", colour="Species")) + \ ggplot2.facet_wrap(Formula("~

Converting an RPy2 ListVector to a Python dictionary

雨燕双飞 提交于 2019-12-18 14:16:35
问题 The natural Python equivalent to a named list in R is a dict, but RPy2 gives you a ListVector object. import rpy2.robjects as robjects a = robjects.r('list(foo="barbat", fizz=123)') At this point, a is a ListVector object. <ListVector - Python:0x108f92a28 / R:0x7febcba86ff0> [StrVector, FloatVector] foo: <class 'rpy2.robjects.vectors.StrVector'> <StrVector - Python:0x108f92638 / R:0x7febce0ae0d8> [str] fizz: <class 'rpy2.robjects.vectors.FloatVector'> <FloatVector - Python:0x10ac38fc8 / R

IPython notebook and rmagic/rpy2: cannot find module ri2py (OSX 10.8.5, python 2.7, R 3.1)

邮差的信 提交于 2019-12-18 13:37:10
问题 I'm trying to use the rmagic extension for the IPython notebook, using Python 2.7.6 via Enthought Canopy. When I try the following example: import numpy as np import pylab X = np.array([0,1,2,3,4]) Y = np.array([3,5,4,6,7]) pylab.scatter(X, Y) %Rpush X Y %R lm(Y~X)$coef I get an error: AttributeError Traceback (most recent call last) <ipython-input-7-96dff2c70ba0> in <module>() 1 get_ipython().magic(u'Rpush X Y') ----> 2 get_ipython().magic(u'R lm(Y~X)$coef') … /Users/hrob/Library/Enthought

Error when loading rpy2 with anaconda

流过昼夜 提交于 2019-12-18 13:28:32
问题 I am trying to load rpy2 inside a project where I am working with anaconda and I am getting a surprising error for which I cannot find a solution. My python version is 3.4 , my anaconda version is 3.17.0 - I am using a Mac (OSX Yosemite version 10.10.4) R version 3.2.2 (2015-08-14) -- "Fire Safety" Platform: x86_64-apple-darwin11.4.2 (64-bit) try: from rpy2 import robjects except ImportError as e: print(e) I am getting this error message dlopen(/Users/user1/anaconda/lib/python3.4/site

How to create an empty R vector to add new items

耗尽温柔 提交于 2019-12-18 10:00:58
问题 I want to use R in Python, as provided by the module Rpy2. I notice that R has very convenient [] operations by which you can extract the specific columns or lines. How could I achieve such a function by Python scripts? My idea is to create an R vector and add those wanted elements into this vector so that the final vector is the same as that in R. I created a seq() , but it seems that it has an initial digit 1, so the final result would always start with the digit 1, which is not what I want

Calling Custom functions from Python using rpy2

℡╲_俬逩灬. 提交于 2019-12-18 02:43:12
问题 Is there a way to call functions defined in a file say myfunc.r ---------------myfunc.r -------------- myfunc = function(){ return(c(1,2,3,4,5,6,7,8,9,10)) } getname = function(){ return("chart title") } ---- Python How to call getname() here ? Any help would be greatly appreciated ? 回答1: The are features in rpy2 that should help making this cleaner than dumping objects into the global workspace. from rpy2.robjects.packages import STAP # if rpy2 < 2.6.1 do: # from rpy2.robjects.packages

How to implement R's p.adjust in Python

梦想与她 提交于 2019-12-17 21:53:41
问题 I have a list of p-values and I would like to calculate the adjust p-values for multiple comparisons for the FDR. In R, I can use: pval <- read.csv("my_file.txt",header=F,sep="\t") pval <- pval[,1] FDR <- p.adjust(pval, method= "BH") print(length(pval[FDR<0.1])) write.table(cbind(pval, FDR),"pval_FDR.txt",row.names=F,sep="\t",quote=F ) How can I implement this code in Python? Here was my feable attempt in Python with the help of Google: pvalue_list [2.26717873145e-10, 1.36209234286e-11 , 0