How to run R code in Python

R is freely available language and computational tool which is very popular among the statisticians and bioinformatics community. In this post I am going to list the steps to run R commands through python on Windows. On Linux it is much simple to setup but the steps are almost similar.


  1. R:
  2. Python 2.7, 3.4, 3.5 or 3.6:
  3. rpy2 pip package (Windows only):
My configuration is R version 3.4.1 ( and Python 3.5 Anaconda x64:

Installation Instructions:

  1. Install R
  2. Install Anaconda
  3. Edit Environment Variables (Control Panel -> System -> Advanced system settings -> Environment Variables)
    • Select Path -> Edit and add R installation path (default location is "C:\Program Files\R\R-3.4.1\bin\x64") to Path variable and press OK
    • Select New and add R_HOME in variable name and base R directory (default is "C:\Program Files\R\R-3.4.1") to variable value
    • Select New and add R_USER in variable name and your Windows user name (You can check this by typing echo %username% in command window) to variable value
  4. Open Windows command window as admin (or launch terminal on linux as root) and enter the following commands.
    D:\Downloads> pip install rpy2-2.8.6-cp35-cp35m-win_amd64.whl
    *On Linux just use the command pip install rpy2, environment variables should already be set otherwise you can set them by using export command.
  5. After successful install you can run the following commands to test if you can run R code in python
     C:\> python
     >>> import rpy2
     >>> print(rpy2.__version__)
     >>> from rpy2.rinterface import R_VERSION_BUILD
     >>> print(R_VERSION_BUILD)
     ('3', '3.3', '', 72310)
     >>> import rpy2.robjects as robjects
     >>> pi = robjects.r['pi']
     >>> pi[0]
    You should be able to run R commands from Python after this step. See for further help.


  1. hi shan
    i need your help if you have time
    spent several days now trying to build pyrit and cpyrit under windows
    but with no luck yet .
    please if you find time for me it will be great
    thanks . ziad


Post a Comment

Popular posts from this blog

Build caffe, pycaffe and matcaffe on Windows

Running Tensorboard on Windows

Build TensorFlow on Windows