can't import package with rPython

1.5k Views Asked by At

I installed umap-learn on mac OS and tried to use it in r markdown file using rPython the way it is explained here:

http://blog.schochastics.net/post/using-umap-in-r-with-rpython/#fn1

But when I run following code:

```{r}
umap <- function(x,n_neighbors=10,min_dist=0.1,metric="euclidean"){
  x <- as.matrix(x)
  colnames(x) <- NULL
  rPython::python.exec( c( "def umap(data,n,mdist,metric):",
              "\timport umap" ,
              "\timport numpy",
              "\tembedding = umap.UMAP(n_neighbors=n,min_dist=mdist,metric=metric).fit_transform(data)",
              "\tres = embedding.tolist()",
              "\treturn res"))

  res <- rPython::python.call( "umap", x,n_neighbors,min_dist,metric)
  do.call("rbind",res)
}

data(iris)
res <- umap(iris[,1:4])
```

I get the error:

Error in python.exec(python.command) : No module named umap

So, apparently Rstudio does not see the umap. I checked that the package is installed by conda list:

umap-learn                0.2.3                    py36_0    conda-forge

How could I fix that?

Update

The version of python was wrong, so I added .Rprofile and made it point to the right version, however, the error persisted.

system("python --version")
Python 3.6.5 :: Anaconda, Inc.

Update

More detailed error (stack trace):

 Error in python.exec(python.command) : No module named umap
 4.stop(ret$error.desc)
 3.python.exec(python.command)
 2.rPython::python.call("umap", x, n_neighbors, min_dist, metric)
 1.umap(iris[, 1:4])
1

There are 1 best solutions below

10
Hack-R On

You need to make sure that umap is available to the same Python used in R, which is not going to be your Anaconda installation by default.

You can check your Python with system("python --version") and if needed go on the cmd line and do pip install umap or pip3 install umap etc (to use the currently available Python; alternately you can switch the Python path to your Anaconda Python).