1

I'm looking in integrate my code in R into Python with rpy2, but have been running into a lot of trouble getting started with the tutorials. Initially, I couldn't get anything to work, but I found this (rpy2 install on windows 7) and was able to get the basics working. It seemed kind of sketchy but I'm not going to complain if it works.

Starting with some basic tutorials I found online, I ended up with the following code:

import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector

package_names=('afex','emmeans')
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)
packnames_to_install = [x for x in package_names if not rpackages.isinstalled(x)]
if len(packnames_to_install) > 0:
    utils.install_packages(StrVector(packnames_to_install))

Everything works fine up until the if statemate, which returns the following error:

Traceback (most recent call last):

  File "<ipython-input-126-93d1ee5af75e>", line 2, in <module>
    utils.install_packages(StrVector(packnames_to_install))

  File "C:\Users\Matthew\Anaconda3\lib\site-packages\rpy2\robjects\functions.py", line 178, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)

  File "C:\Users\Matthew\Anaconda3\lib\site-packages\rpy2\robjects\functions.py", line 106, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)

RRuntimeError: Error in (function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,  : 
  unable to install packages

Does anybody know what I am doing wrong? Thank you!

Matthew
  • 21
  • 1

2 Answers2

0

RRuntimeError exception are originating from the embedded R. For some reason you are unable to install R packages. This could originate from permission issues.

lgautier
  • 11,363
  • 29
  • 42
0

Run python with admin privileges before calling utils.install_packages(..)

Nick
  • 11
  • 2