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:
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.
It also seems to be a good idea to add the directory containing the R.dll to your PATH variable.
The last thing I can think of concerns your usage of lib_loc. While the documentation of rpy2
states, that this option to importr
should 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:
- Install R
- Install Python
- Download and install the visual c++ compiler for python
- Set the
R_HOME
and R_USER
environment variables and add the directory containing R.dll to my PATH
.
- 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.