在这里,我尝试从python代码调用R函数。
import numpy as np
import pandas as pd
import rpy2.robjects as ro
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
time_series_data = pd.read_csv(daily_file_path, sep=",", header=None).values
nr,nc = time_series_data.shape
r_time_series_data = ro.r.matrix(time_series_data, nrow=nr, ncol=nc)
ro.r.assign("r_time_series_data", r_time_series_data)
当我导入R库时,它会抛出以下错误:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Edu 4.0\helpers\pydev\pydevd.py", line 1596, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Edu 4.0\helpers\pydev\pydevd.py", line 1023, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Edu 4.0\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/nca150130/PycharmProjects/anomaly/src/test/test4.py", line 26, in <module>
import rpy2.robjects as ro
File "C:\Users\cc\AppData\Local\Continuum\Anaconda3\lib\site-packages\rpy2-2.9.1-py3.5-win-amd64.egg\rpy2\robjects\__init__.py", line 16, in <module>
import rpy2.rinterface as rinterface
File "C:\Users\cc\AppData\Local\Continuum\Anaconda3\lib\site-packages\rpy2-2.9.1-py3.5-win-amd64.egg\rpy2\rinterface\__init__.py", line 45, in <module>
_load_r_dll(R_HOME)
File "C:\Users\cc\AppData\Local\Continuum\Anaconda3\lib\site-packages\rpy2-2.9.1-py3.5-win-amd64.egg\rpy2\rinterface\__init__.py", line 29, in _load_r_dll
ctypes.CDLL(r_dll)
File "C:\Users\cc\AppData\Local\Continuum\Anaconda3\lib\ctypes\__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
我尝试将R使用的dll文件添加到PATH
环境中。不过,我收到了这个错误。你能给我任何建议吗?