R, Python: install packages on rpy2

前端 未结 3 1455
南旧
南旧 2020-12-08 12:34

I\'m using R in my Python script through the rpy2 library and I need a package that is not in the default installation of R. How can I install it?

相关标签:
3条回答
  • 2020-12-08 12:55

    Ricardo's answer no longer works.

    To install from Python:

    from rpy2.robjects.packages import importr
    utils = importr('utils')
    utils.install_packages('DirichletReg')
    

    That utils package is the R.utils package whose documentation can be found here: https://CRAN.R-project.org/package=R.utils

    As of my last edit, the documentation still says to do this.

    0 讨论(0)
  • 2020-12-08 12:55

    When running pytest, Aaron's answer makes my Python hang and R keep giving error messages, probably because of this:

    Calling install_packages() without first choosing a mirror will require the user to interactively choose a mirror.

    According to rpy2 documentation, I used this which worked:

    from rpy2 import robjects
    import rpy2.robjects.packages as rpackages
    
    utils = rpackages.importr('utils')
    utils.chooseCRANmirror(ind=1)
    utils.install_packages("DirichletReg")
    DirichletReg = rpackages.importr("DirichletReg")
    
    0 讨论(0)
  • 2020-12-08 12:56

    How about this

    >>> import rpy2.interactive as r
    >>> r.importr("utils")
    >>> package_name = "DirichletReg"
    >>> r.packages.utils.install_packages(package_name)
    
    0 讨论(0)
提交回复
热议问题