I am trying to run stitchr in R. For programs that run in Python, I use reticulate. I create a conda environment named r-reticulate, where I want to install stitchr and run it.
I try the following:
if (!('r-reticulate' %in% reticulate::conda_list()[,1])){
reticulate::conda_create(envname = 'r-reticulate', packages = 'python=3.10')
}
reticulate::use_condaenv('r-reticulate')
reticulate::py_install("stitchr", pip = TRUE)
system("stitchr -h") # this does not work
But obviously enough, the system() call does not work, with the message error in running command.
What would be the right way to do this?
I had success in the past with anndata, for example. But this is an R package wrapper, so I can just do:
reticulate::use_condaenv('r-reticulate')
reticulate::py_install("anndata", pip = TRUE)
data_h5ad <- anndata::read_h5ad("file.h5ad")
How can I approach the stitchr case?
EDIT:
So I retrieved stitchr.py location during the package installation: /usr/local/Caskroom/miniconda/base/envs/r-reticulate/lib/python3.10/site-packages/Stitchr/stitchr.py
I tried all the following but nothing works (see error messages):
pyloc="/usr/local/Caskroom/miniconda/base/envs/r-reticulate/lib/python3.10/site-packages/Stitchr/stitchr.py"
reticulate::source_python(pyloc)
Error in py_run_file_impl(file, local, convert) : ImportError: attempted relative import with no known parent package Run
reticulate::py_last_error()for details.
reticulate::py_run_file(pyloc)
Error in py_run_file_impl(file, local, convert) : ImportError: attempted relative import with no known parent package Run
reticulate::py_last_error()for details.
reticulate::py_run_string(paste(pyloc, "-h"))
Error in py_run_string_impl(code, local, convert) : File "", line 1 /usr/local/Caskroom/miniconda/base/envs/r-reticulate/lib/python3.10/site-packages/Stitchr/stitchr.py -h SyntaxError: invalid syntax Run
reticulate::py_last_error()for details.
I am absolutely clueless on how to proceed here.
Here is maybe what you expect.
shell:
shell stitchr part, read from the doc of stitchr
It works with command line.
shell
It works with command line.
Create
~/teststitchr2.pyfilled by the content of https://jamieheather.github.io/stitchr/importing.html~/teststitchr2.py:python in the shell
In R:
reticulate::py_run_file()populates the variablepy: https://rstudio.github.io/reticulate/articles/calling_python.html#executing-codeHere is, by
names(py), all functions and variables from reticulate prefixed bypy$In R:
It works :)
You can type
myvar=py$stitchedto have it in a variable and use it later.You can also try this: In R:
Be careful I mixed R variable,
tcr_bits2, and reticulate environment (py$). You can typemyvar2=py$st$stitch(bla bla)to have it in a variable and use it later.It works again :)
Edit:
And a bad trick, in the Python side, if you have an issue of import, before
from Stitchr importBut look at also How can I import a module dynamically given the full path?
This trick (
os.chdir()) is only for test, but try to not use it.