1

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

jyson
  • 245
  • 1
  • 8
  • 27
  • @Parfait thanks for heads up. how am I gonna use `R_USER` ? Can you show me how it is done for using python script to get `sessionInfo()`? do you mind elaborating on your workable solution? thanks – jyson Apr 20 '20 at 15:38
  • @Parfait are you saying use this: `R_USER = os.getenv("R_USER")`? it is not working, could you take a look my updated attempt above? – jyson Apr 20 '20 at 15:47
  • @Parfait I can import `rpy2`, but can't import `rpy2.robjects` which caused OS error? any possible help from you? – jyson Apr 20 '20 at 16:09
  • @Parfait yes, I tried `pip install rpy2-2.9.5-cp37-cp37m-win_amd64.whl`, set `R_USER = r"C:\Users\mia\AppData\Local\Programs\Python\Python37\Lib\site-packages\rpy2"`, but still can't get R `sessionInfo()`? any solution from you? thanks – jyson Apr 20 '20 at 17:00
  • Was that your original installation or installation just now per my suggestion? You should remove any previous version before new install. Do you receive same exact error as posted? – Parfait Apr 20 '20 at 17:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212105/discussion-between-user88911-and-parfait). – jyson Apr 20 '20 at 17:05
  • Please clean up your entire post and show latest code attempt (without all the edits or error) calling `sessionInfo`. Also let's delete these past comments if `rpy2.robjects` no longer is an issue. – Parfait Apr 20 '20 at 17:16
  • @Parfait for the correction, I still can't import `rpy2.robjects`. – jyson Apr 20 '20 at 17:29
  • @Parfait I tried of using subprocess.call() to get R session information, why I wouldn't get R session information? any idea? – jyson Apr 20 '20 at 20:01

1 Answers1

1

I ran into same problem on my windows machine, you are having this problem because you didn't add R installed path to system variables. You should do the following:

open Control Panel\System and Security\System, open Environment Variables, find System Variables, then select Path to add C:\Program Files\R\R-3.3.2\bin\x64 to your System Variables. Make sure you install rpy2 from uci website, download rpy2.whl then pip install rpy2.xx.xx.whl

Then do this setting in your python script:

import os


os.environ['PYTHONHOME'] = r"C:\Users\me\AppData\Local\Programs\Python\Python37"
os.environ['PYTHONPATH'] = r"C:\Users\me\AppData\Local\Programs\Python\Python37\Lib\site-packages"
os.environ['R_HOME'] = r"C:\Program Files\R\R-3.3.2"
os.environ['R_USER'] = r"C:\Users\me\AppData\Local\Programs\Python\Python37\Lib\site-packages\rpy2"

import rpy2
import rpy2.robjects as robjects

pi = robjects.r['pi']
print(pi)

this problem can solve your issue right away.

Jerry07
  • 929
  • 1
  • 10
  • 28