How to import r-packages in Python

2024/10/15 11:23:31

I'm a bit troubled with a simple thing. I was trying to install a package called hunspell, but I discovered it is originally an R package. I installed this version: https://anaconda.org/conda-forge/r-hunspell, but I'm not being able to import it. Is this package supposed to work with Python? Should I use rpy2 to import it? First time using cross-platform packages so I'm a bit confused.

Just to be clear, import hunspell brings ModuleNotFoundError: No module named 'hunspell' and import r-hunspell brings SyntaxError: invalid syntax.

I also noticed that this package, also installed an r-base package, but I'm also not sure how to import that.

Answer

After running in the command line:

pip install rpy2

or with the "!" if you're in a Jupyter Notebook. The following procedure will answer your issue, based on the official documentation:

# Using R inside python
import rpy2
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)# Install packages
packnames = ('hunspell', 'some other desired packages')
utils.install_packages(StrVector(packnames))# Load packages
hunspell = importr('hunspell')

If you want to access specific functions in this module you could check out these answer or that answer too.

https://en.xdnf.cn/q/69294.html

Related Q&A

XPath predicate with sub-paths with lxml?

Im trying to understand and XPath that was sent to me for use with ACORD XML forms (common format in insurance). The XPath they sent me is (truncated for brevity):./PersApplicationInfo/InsuredOrPrinci…

Best way to access and close a postgres database using python dataset

import dataset from sqlalchemy.pool import NullPooldb = dataset.connect(path_database, engine_kwargs={poolclass: NullPool})table_f1 = db[name_table] # Do operations on table_f1db.commit() db.execut…

Using different binds in the same class in Flask-SQLAlchemy

I currently have multiple databases with identical Tables and Columns (but different data inside). So clearly I need to use binds to access all of them, but its apparently not as simple as doing this:c…

Correctly parse date string with timezone information

Im receiving a formatted date string like this via the pivotal tracker API: "2012/06/05 17:42:29 CEST"I want to convert this string to a UTC datetime object, it looks like python-dateutil doe…

Can I add a sequence of markers on a Folium map?

Suppose I had a list, or pandas series, or latitude longitude pairs. With Folium, I can plot markers for a single pair of coordinates using coords = [46.8354, -121.7325] map_4 = folium.Map(location=[4…

Tkinter in Python 3.4 on Windows dont post internal clipboard data to the Windows clipboard on exit

I use the following code to place result of my small scripts in clipboard.from tkinter import Tk r = Tk() r.withdraw() r.clipboard_clear() r.clipboard_append("Result")It works fine on Python …

How do I group date by month using pd.Grouper?

Ive searched stackoverflow to find out how to group DateTime by month and for some reason I keep receiving this error, even after I pass the dataframe through pd.to.datetimeTypeError: Only valid with D…

Python Too many indices for array

I am reading a file in python using pandas and then saving it in a numpy array. The file has the dimension of 11303402 rows x 10 columns. I need to split the data for cross validation and for that I …

Removing named entities from a document using spacy

I have tried to remove words from a document that are considered to be named entities by spacy, so basically removing "Sweden" and "Nokia" from the string example. I could not find …

Install wxPython in osx 10.11

When I try to install wxPython, it shows an error: > The Installer could not install the software because there was no > software found to install.How can I fix it?