在python中调用r脚本?

时间:2018-05-08 12:40:58

标签: python r rpy2

下面提到的R代码使用库CALIBERdrugdose,并使用功能剂量转换器返回数据帧。我们需要在python中调用这个r脚本或rfunc(x)我们如何做到这一点?

library(CALIBERdrugdose)
rfunc <- function(x){
l=doseconvert(c(x))
return(l)
}

当我试图以这种方式运行时

robjects.r('''
    # create a function `f`
    library(CALIBERdrugdose)
   rfunc <- function(x){
   l=doseconvert(c(x))
  return(l)
    }
    # call the function `f` with argument value 3
    rfunc("sadas")
    ''')

它给了我这个错误..

  RRuntimeError                             Traceback (most recent call last)
<ipython-input-89-5aea02cf4722> in <module>()
     10         # call the function `f` with argument value 3
     11         rfunc("sadas")
---> 12         ''')

D:\workspace\lib\site-packages\rpy2\robjects\__init__.py in __call__(self, string)
    350     def __call__(self, string):
    351         p = _rparse(text=StrSexpVector((string,)))
--> 352         res = self.eval(p)
    353         return conversion.ri2py(res)
    354 

D:\workspace\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    176                 v = kwargs.pop(k)
    177                 kwargs[r_k] = v
--> 178         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
    179 
    180 pattern_link = re.compile(r'\\link\{(.+?)\}')

D:\workspace\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    104         for k, v in kwargs.items():
    105             new_kwargs[k] = conversion.py2ri(v)
--> 106         res = super(Function, self).__call__(*new_args, **new_kwargs)
    107         res = conversion.ri2ro(res)
    108         return res

RRuntimeError: Error in library(CALIBERdrugdose) : 
  there is no package called 'CALIBERdrugdose'

1 个答案:

答案 0 :(得分:1)

R返回的错误消息指出缺少您想要使用的包,或者更确切地说R无法找到它以加载它。