python3調用R語言乾貨

 

R語言知識:https://www.w3cschool.cn/r/r_lists.htmlhtml

 

1. 安裝庫rpy2

1. 下載與本地對應python版本模塊,pip install rpy2是安裝不上的python

下載地址是:http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2  這是python下包的專用地址
須要下載版本和平臺都相對應的whl包,我下的是rpy2-2.9.4-cp36-cp36m-win32.whl

pip install rpy2-2.9.4-cp36-cp36m-win32.whl安裝便可。

 若是還不行,參考:http://www.javashuo.com/article/p-gvdrqvzz-cw.htmlspa

2. 安裝broom --》R語言的一個庫--》與R腳本有關,能夠忽略

install.packages('broom')

 

3. 寫R腳本

library(broom)

test <- function() {
  # x <- c(1:1200000)
  # y <- c(1:1200000)
  x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
  y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
  
  relation <- lm(y ~ x)
  data <- summary(relation)
  
  data_dict <- c()
  
  newData <- c(data)
  data_dict["residuals"] <- newData["residuals"]
  data_dict["coefficients"] = newData["coefficients"]
  data_dict["aliased"] = newData["aliased"]
  data_dict["sigma"] = newData["sigma"]
  data_dict["df"] = newData["df"]
  data_dict["r.squared"] = newData["r.squared"]
  data_dict["adj.r.squared"] = newData["adj.r.squared"]
  data_dict["fstatistic"] = newData["fstatistic"]
  data_dict["cov.unscaled"] = newData["cov.unscaled"]
  data_dict["p.value"] = c(broom::glance(data))["p.value"]
  
  return(data_dict)
}


# result <- test()
# print(result)

 

4. 寫python腳本

報錯: RuntimeError: R_USER not defined..net

解決方案,各類搜索都是環境變量的問題,因而我各類加3d

 還tm不行..........................................又懶得重啓code

stackflow找到答案htm

os模塊的運用,直接看腳本blog

 

import os
os.environ['R_HOME'] = r'C:\Program Files\R\R-3.6.0'
os.environ['R_USER'] = r'C:\python3.6.3\Lib\site-packages\rpy2' #path depe

import rpy2.robjects as robjects   # ----------------------------------------------> 必定要注意這句,不能放到最上面,由於要先添加環境變量,才能找到這個rpy2。必定要注意
robjects.r.source(r'C:\code\r_test\test_one\test.R')
a = robjects.r('test()')
print(type(a))
# print(list(a))
from pandas import DataFrame
print(a[0])
print(a[0][0])

 

 

打印結果,以及轉換數據類型,參考:http://rpy.sourceforge.net/rpy2/doc-2.2/html/vector.html#creating-vectors                  http://www.javashuo.com/article/p-zoqiynfz-p.htmlip

 

5. python傳值給R腳本,如何實現, 形參方法1

R腳本: 這個腳本的關鍵在於如何將list轉換爲cci

library(broom)

test <- function(list_data) {
  # print(list_data)
  # print(class(list_data))
  # r語言list 轉換成 vector: v = as.vector(unlist(你的list))
  x = c(as.vector(unlist(list_data['x'])))
  y = c(as.vector(unlist(list_data['y'])))

  
  relation <- lm(y ~ x)
  data <- summary(relation)
  print(data)

  return(0)
}

 

python腳本

import os

os.environ['R_HOME'] = r'C:\Program Files\R\R-3.6.0'
os.environ['R_USER'] = r'C:\python3.6.3\Lib\site-packages\rpy2' #path depe

from pandas import DataFrame as df
import rpy2.robjects as robjects
import time
robjects.r.source(r'C:\code\r_test\test_one\test.R')

time1 = time.time()

y =  robjects.ListVector({
    "x":[1, 2, 3],
    "y":[1, 2, 3], # 這裏能夠給float

    })
a = robjects.r["test"](y)

 

6. python傳值給R腳本,如何實現, 形參方法2:相似python的args

R語言腳本

library(broom)

test <- function(...) {
  list_data <- list(...) # 相似python的args,能夠傳遞多個參數
  print(list_data)
  print(class(list_data))
  x = c(as.vector(unlist(list_data[1]))) # 注意R是從1開始的
  y = c(as.vector(unlist(list_data[2])))
  print(x)
  print(y)


  
  relation <- lm(y ~ x)
  data <- summary(relation)
  print(data)

  return(0)
}

python語言

import os

os.environ['R_HOME'] = r'C:\Program Files\R\R-3.6.0'
os.environ['R_USER'] = r'C:\python3.6.3\Lib\site-packages\rpy2' #path depe

from pandas import DataFrame as df
import rpy2.robjects as robjects
import time
robjects.r.source(r'C:\code\r_test\test_one\test.R')

x =  robjects.IntVector([151, 174, 138, 186, 128, 136, 179, 163, 152, 131])
y =  robjects.IntVector([63, 81, 56, 91, 47, 57, 76, 72, 62, 48])

a = robjects.r["test"](x, y)


 

 

相關文章
相關標籤/搜索