3

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])
Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87

1 Answers1

1

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).

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • Just to add, the following thread helped me switch to the right python version: https://stackoverflow.com/questions/36705878/rstudio-python-version-change-on-mac – Nikita Vlasenko May 03 '18 at 18:28
  • However, `umap` can not still be found. – Nikita Vlasenko May 03 '18 at 18:29
  • `pip3` is not installed probably since it is writing that `command not found`. The error is the same `No module named umap` – Nikita Vlasenko May 03 '18 at 18:34
  • I ran successfully `pip install umap` but did not fix the error. – Nikita Vlasenko May 03 '18 at 18:43
  • R version is `3.5.0` – Nikita Vlasenko May 03 '18 at 18:44
  • @NikitaVlasenko Thank you. I see now that I'm not going to be able to replicate your situation because my Windows version is different from your macOS version. OK. Can you please run this in Python via R? `import sys` then `print (sys.path)` We want to verify that it can hit the library folder. – Hack-R May 03 '18 at 18:46
  • It is definitely trying to use `Python 2.7`, not the right one. So, in `Rstudio` the `python` version is correct, but `rPython` is switching the version. – Nikita Vlasenko May 03 '18 at 18:52
  • @NikitaVlasenko OK that's what it sounded like. So we gotta get the PYTHONPATH changed then (or put the library in 2.7). So the linked solution didn't work? I can help you try more but I'm not on macOS so I cant test it https://stackoverflow.com/questions/3387695/add-to-python-path-mac-os-x – Hack-R May 03 '18 at 18:53
  • The linked solution worked. Output of `system("python --version")` is correct – Nikita Vlasenko May 03 '18 at 18:55
  • @NikitaVlasenko Oh I see, so the python executable is right but the sys path for modules is wrong. OK lets just update that part. – Hack-R May 03 '18 at 18:55