I am trying to get R sessioninfo() by using rpy2
in python script, the reason because I am going to make api call from python to custom R library. The basic thing I want to try is get current R sessioninfo() using simple python script.
my attempt:
here is the attempt that I tried by following this SO
post:
import rpy2.robjects as robjects
import os
os.environ['R_HOME'] = "C:/PROGRA~1/R/R-36~1.3"
robjects.r('''
source('myfunc.r')
''')
myfunc = function(){
return(sessionInfo())
}
I got R_HOME by using Sys.getenv('R_HOME')
or R.home()
in R studio.
new updated attempt:
based on @Parfait suggestion, I aso tried this way:
pip install .\rpy2-2.9.5-cp37-cp37m-win_amd64.whl
import os
os.environ['PYTHONHOME'] = r"C:\Users\mia\AppData\Local\Programs\Python\Python37"
os.environ['PYTHONPATH'] = r'C:\Users\mia\AppData\Local\Programs\Python\Python37\Lib\site-packages'
os.environ['R_HOME'] = r'C:\Program Files\R\R-3.6.3'
os.environ['R_USER'] = r'C:\Users\mia\AppData\Local\Programs\Python\Python37\Lib\site-packages\rpy2'
# importing rpy2
import rpy2
import rpy2.robjects as robjects
# test : evaluating R code
robjects.r('''
# create a function `f`
f <- function(r, verbose=FALSE) {
if (verbose) {
cat("I am calling f().\n")
}
2 * pi * r
}
''')
r_f = robjects.r['f']
res = r_f(3)
print(res[0])
but now I get another error:
> ---------------------------------------------------------------
> OSError Traceback (most recent call last)
> <ipython-input-27-b5597ba1add5> in <module>
> 1 import rpy2
> ----> 2 import rpy2.robjects as robjects
> 3 import itertools
> 4 from datetime import datetime
> 5 import rpy2.rinterface as rinterface
>
> c:\users\mia\appdata\local\programs\python\python37\lib\site-packages\rpy2\robjects\__init__.py
> in <module>
> 14 import itertools
> 15 from datetime import datetime
> ---> 16 import rpy2.rinterface as rinterface
> 17 import rpy2.rlike.container as rlc
> 18
>
> c:\users\mia\appdata\local\programs\python\python37\lib\site-packages\rpy2\rinterface\__init__.py
> in <module>
> 44
> 45 if sys.platform == 'win32':
> ---> 46 _load_r_dll(R_HOME)
> 47
> 48 # cleanup the namespace
>
> c:\users\mia\appdata\local\programs\python\python37\lib\site-packages\rpy2\rinterface\__init__.py
> in _load_r_dll(r_home)
> 28 if r_bin not in os.environ.get('PATH'):
> 29 os.environ['PATH'] = ';'.join((os.environ.get('PATH'), r_bin, r_mod))
> ---> 30 ctypes.CDLL(r_dll)
> 31
> 32 R_HOME = get_r_home()
>
> c:\users\mia\appdata\local\programs\python\python37\lib\ctypes\__init__.py
> in __init__(self, name, mode, handle, use_errno, use_last_error)
> 362
> 363 if handle is None:
> --> 364 self._handle = _dlopen(self._name, mode)
> 365 else:
> 366 self._handle = handle
>
> OSError: [WinError 126] The specified module could not be found
now I can import rpy2
also can import rpy2.robjects
, but can't able to get R sessionInfo() correctly. any further thoughts? thanks
desired output:
I just want to print out R sessionInfo() in jupyternotebook or python script by using rpy2
? any solution to make this happen? any thoughts? thanks a lot