1

I want to use the rpy2 package. When I import R's PortfolioAnalytics library which does not belong to the standard library, I do not get any output anymore. (I run my python code directly from Idle on Windows and not via the Windows console (because of the problem I outlined below).)

Examples:

First code works fine:

    import rpy2.robjects as robjects
    from rpy2.robjects.packages import importr

    print 'check output 1'
    utils = importr("utils")        # 'utils' belongs to the standard lib

    print 'check output 2'

Output:

    check output 1
    check output 2

So far so good. But in the following example it will not work anymore.

Second code does not work:

    import rpy2.robjects as robjects
    from rpy2.robjects.packages import importr

    print 'check output 1'
    utils = importr("PortfolioAnalytics", lib_loc = "C:\username\Documents\R\win-library\3.3")

    print 'check output 2'

Output:

    check output 1

Additional note: I am not sure whether the following is connected to the problem: When I want to execute my code using the Windows Console , I get the error

    RuntimeError: R_USER not defined.

(I have added R's bin directory to the PATH variable.) Anyways, that is not my urgent problem. I only wanted to add this information in case it might be connected to my problem.

cel
  • 24,830
  • 16
  • 80
  • 107
Charlotte01
  • 45
  • 1
  • 4

1 Answers1

2

This should be a comment, but I can't write comments yet.

I tried to reproduce your error, which I couldn't. Here are my thoughts:

  1. During installation of R on Windows I was forced to set R_USER. I don't know how you got around that, but I think it is a good idea to do so.
    There are different versions of R available. The newest one (R 3.3.0) does store the R.dll in the directory specified in this post, which is were R_HOME supposedly should point to. I have seen at other places people using higher directories (.../R/R-x.y.z/) or different directories (.../R/R-x.y.z/bin/i386). If it causes problems, you might want to try one of those.

  2. It also seems to be a good idea to add the directory containing the R.dll to your PATH variable.

  3. The last thing I can think of concerns your usage of lib_loc. While the documentation of rpy2 states, that this option to importrshould point to the library, the CRAN page of Portfolio Analytics states, that the package has to be compiled. Unlikely as it may be, could it be that your package is not installed via (for instance) install.packages("PortfolioAnalytics"), or at least not correctly? In that case your lib_loc value might point to the correct directory, but the library is not actually compiled.

Approximately what I did:

  1. Install R
  2. Install Python
  3. Download and install the visual c++ compiler for python
  4. Set the R_HOME and R_USER environment variables and add the directory containing R.dll to my PATH.
  5. Use the R command line to install PortfolioAnalytics (and all its dependencies) with install.packages("package_name") taking into account the accepted answer from here.

After that, running your code worked for me without errors. (Tested from command line, IDLE and Eclipse (PyDev) on Windows 10 with Python 2.7 and R 3.3.0.)

If this doesn't help you, maybe you can share some more information on what you have tried so far.

Edit: I was able to partly reproduce the described behavior and it seems to be related to the usage of lib_loc. There seems to be no detailed documentation for importr and also not for the usage of lib_loc. After making sure that the package was installed correctly, I tried to specify its location with the lib_loc argument. The following directories all turned out to be invalid values for this purpose:

  • /R_DIR/library
  • /R_DIR/library/PortfolioAnalytics
  • /R_DIR/library/PortfolioAnalytics/libs
  • /R_DIR/library/PortfolioAnalytics/libs/x64
  • /R_DIR/library/PortfolioAnalytics/libs/i386

That's all I checked. However, I am fairly sure, that installing the package into /R_DIR/library (which works best, if R is not installed in any system directory like Program Files, due to Windows restrictions) should make it unnecessary to specify lib_loc. If I am correct, that is a viable workaround, especially if you are just starting to set up your R environment.

Actually a different issue and already explained here has occurred during the importr command (I think) at some point. As explained in the link this is related to the missing ls function in Windows and solved for me by Preet Kukreti's answer.

Community
  • 1
  • 1
niak
  • 310
  • 3
  • 10