I'm trying to use the R (Version 3.4.3) package MICE in Python 3.6.5 with rpy2.
This is the code I use:
import os
os.environ['R_HOME'] = 'C:/Program Files/R/R-3.4.3'
os.environ['R_USER'] = 'C:/Users/username/Anaconda3/Lib/site-packages/rpy2'
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
mice = importr('mice', lib_loc ='C:\Program Files\R\R-3.4.3\library')
When I execute this, my Jupyter notebook dies. In the terminal I get the error message:
Error: package or namespace load failed for 'stats' in inDL(x, as.logical(local), as.logical(now), ...):
unable to load shared object 'C:/Program Files/R/R-3.4.3/library/stats/libs/x64/stats.dll':
LoadLibrary failure: The specified module could not be found.
During startup - Warning message:
package 'stats' in options("defaultPackages") was not found
Error: '\P' is an unrecognized escape in character string starting ""C:\P"
But I do have the package stats installed and the file 'C:/Program Files/R/R-3.4.3/library/stats/libs/x64/stats.dll' exists.
Does anybody know a solution for this problem?
I installed the package mice in RStudio. I made sure that all my installed R libraries are in the directory C:/Program Files/R/R-3.4.3/library so that dependencies of mice are found in the same folder. For this I just copied all the packages installed in another folder to that one (I hope that's not a problem?)
Related
I am trying to use pymer4 to run linear mixed models in python (in Spyder).
I installed via conda according to the following instructions:
https://eshinjolly.com/pymer4/installation.html
However, when I run:
import pymer4
I get the following error:
File "C:\Users\username\Anaconda3\lib\site-packages\rpy2\rinterface_lib\openrlib.py", line 37, in _dlopen_rlib
rlib = ffi.dlopen(lib_path)
OSError: cannot load library '\R\R-4.1.0\bin\x64\R.dll': error 0x7e
In contrast, rpy2 works fine:
import rpy2
I have already tried going to control panel > system preferences > advanced system settings > enviornment variables > system variables > path > and making sure the path to R ("C:\Users\username\R\R-4.1.0\bin\x64") is there.
When I try this solution (OSError: cannot load library 'C:\Program Files\R\R-4.0.2\bin\x64\R.dll': error 0x7e), the kernel crashes and shows "fatal error unable to open base package":
import os
os.environ["R_HOME"] = r"C:\Users\username\R\R-4.1.0"
os.environ["PATH"] = r"C:\Users\username\R\R-4.1.0\bin\x64" + ";" + os.environ["PATH"]
import pymer4
It's important to note I have used R and RStudio before on this computer so there may be confusion with that.
Any help much appreciated.
I'm attempting to run RPY2 to utilize the TTR package in R, and running python 3.8.3 and R 4.0.2. However, when attempting to run the code
os.environ['R_HOME'] = "C:\\Program Files\\R\\R-4.0.2\\bin\\x64"
from rpy2.robjects.packages import importr'
this results in :
OSError: cannot load library 'C:\Program Files\R\R-4.0.2\bin\x64\bin\x64\R.dll': error 0x7e
I proactively ran python -m rpy2.situation, yielding
C:\Users\XXXXX>python -m rpy2.situation
rpy2 version:
3.3.4
Python version:
3.8.3rc1 (tags/v3.8.3rc1:802eb67, Apr 29 2020, 21:39:14) [MSC v.1924 64 bit (AMD64)]
Looking for R's HOME:
Environment variable R_HOME: None
InstallPath in the registry: C:\Program Files\R\R-4.0.2
Environment variable R_USER: None
Environment variable R_LIBS_USER: None
R version:
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.
In the PATH:
Loading R library from rpy2: OK
Additional directories to load R packages from:
None
C extension compilation:
'sh' is not recognized as an internal or external command,
operable program or batch file.
Warning: Unable to get R compilation flags.
Any help on why RPY2 is causing this 0x7e error is greatly appreciated. I have also uninstalled and reinstalled both R, and RPY2 as I found that on a solution on some other posts.
I had the same issue trying to import the rpy2 library. I got it sorted when i added a path for R in my environment variable.
***InstallPath in the registry: C:\Program Files\R\R-4.0.2
Try creating a path on system environment variables with the above and see if it works
I had the same error and for me the problem was that SciPy was imported before rpy2. Moving the SciPy import below rpy2 solved it.
I had the exact same problem. The reason was that python was running in an Anaconda environment. The environment has its own version of R installed. (Maybe search your computer for "Rcmd.exe" to see all the R copies on your machine.)
The solution was to modify os.environ['R_HOME'] to the appropriate copy of R:
For me it worked by adding this to the top of my python script:
import os
os.environ["R_HOME"] = "C:\\Users\\<Name>\\anaconda3\\envs\\<enironment_name>\\Lib\\R\\"
But the exact path might be different for you depending on from where you are running rpy2.
And also note that just like Aidan mentioned you should not add \\bin\\x64 to your R_HOME path.
The line Loading R library from rpy2: OK when running rpy2.situation suggests that the R dll is loading properly. There is likely something different between the environment in which you are running you Python script and the terminal where you are running C:\Users\XXXXX>python -m rpy2.situation.
Try running rpy2.situation from a Python script (for example take the content of the if __name__ == '__main__': block - https://github.com/rpy2/rpy2/blob/master/rpy2/situation.py#L358)
note in your output:
OSError: cannot load library 'C:\Program Files\R\R-4.0.2\bin\x64\bin\x64\R.dll': error 0x7e
your R_Home just needs to be 'C:\Program Files\R\R-4.0.2'. In fact remove the changing of environment variables and it should just work.
You need to do both 2 things together:
set R_HOME
set environ "path" include "R bin"
e.g.
#set R_HOME dynamically
import os
os.environ['R_HOME'] = r'YOUR R HOME PATH' #e.g. r'C:\Users\STEMLab\Miniconda2\envs\myenv\Lib\R' for my case
#set R bin
os.environ['path'] += r';YOUR R BIN;' #e.g. r';C:\Users\STEMLab\Miniconda2\envs\myenv\Lib\R\bin;' for my case again
cheers :)
I was in the exact same situation and changing the environment variable settings didn't solve the problem. I had installed 32-bit python by mistake; installing 64-bit python worked fine for me!
You can check it by
import platform; platform.architecture()
I am installing Tensorflow. I encountered the following problem:
I was installing Tensorflow from Source and I am following the Tensorflow website (https://www.tensorflow.org/install/source)
I had earlier installed Bazel 3.0.0 (the latest version) but got a similar error. Upon looking at other answers I figured out this error has something to do with the version of Bazel. Hence I uninstalled Bazel 3.0.0 and installed Bazel 0.27.1. But upon typing the following command(bazel build //tensorflow/tools/pip_package:build_pip_package) in the terminal, I am getting the following error:
WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files:
/home/aviraj/tensorflow/tools/bazel.rc
Starting local Bazel server and connecting to it...
ERROR: /home/aviraj/tensorflow/WORKSPACE:3:1: name 'http_archive' is not defined
ERROR: Error evaluating WORKSPACE file
ERROR: error loading package '': Encountered error while reading extension file 'closure/defs.bzl': no such package '#io_bazel_rules_closure//closure': error loading package 'external': Could not load //external package
ERROR: error loading package '': Encountered error while reading extension file 'closure/defs.bzl': no such package '#io_bazel_rules_closure//closure': error loading package 'external': Could not load //external package
INFO: Elapsed time: 2.023s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
Any help would be much appreciated. Thanks
Starting with version 0.20.0, http_archive native rule has been deprecated, it has to be loaded from: #bazel_tools//tools/build_defs/repo:http.bzl. I.e. you need to add the following (unless you have a customized version) in WORKSPACE or bzl files using it:
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
I have used rpy2 to call R package successfully in python. However, I just install elasticnet today in R and I checked I can load the library in R without problem, also the package name folder elasticnet is in my directory.
I want to do the same call as other packages using importr, but it keeps giving me error message:
Error in loadNamepace(name): there is no package called 'elasticnet'.
But I have go to that address and actually see the package, also in R I can see that package is installed. Anyone know how to solve this problem?
This is how I called other packages in python, for example the pls package:
import rpy2
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
utils = importr("utils")
d = {'print.me': 'print_dot_me', 'print_me': 'print_uscore_me'}
pls = importr('pls', robject_translations = d, lib_loc = "/Users/Viii/Anaconda3/envs/rstudio/lib/R/library")
But when I replace that with elasticnet, it doesn't work.
And it gives me the error message saying
"RRuntimeWarning: 'package 'lars' was installed by an R version with
different internals; it needs to be reinstalled for use with this R
version'.
I also try to install glmnet instead, it also gives me the error message
en = importr('glmnet', robject_translations = d, lib_loc = "/Users/Viii/Anaconda3/envs/rstudio/lib/R/library")
Traceback (most recent call last):
File , line 1, in en =
importr('glmnet', robject_translations = d, lib_loc =
"/Users/Viii/Anaconda3/envs/rstudio/lib/R/library")
File
"C:\Users\Viii\Anaconda3\lib\site-packages\rpy2-2.9.4-py3.6-win-amd64.egg\rpy2\robjects\packages.py",
line 453, in importr env = _get_namespace(rname)
RRuntimeError: Error in loadNamespace(name) : there is no package
called 'glmnet'
The sessioninfo in R:
R version 3.5.0 (2018-04-23)
Platform: x86_64-conda_cos6-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS
Matrix products: default
BLAS/LAPACK: /home/viii/anaconda3/envs/rstudio/lib/R lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods [7] base
other attached packages:
[1] elasticnet_1.1.1 lars_1.2
loaded via a namespace (and not attached):
[1] compiler_3.5.0 tools_3.5.0 yaml_2.1.18
I'm running the following commands in python:
from rpy2.robjects.packages import importr
importr( 'ggplot2' )
This works fine in python-2.7.6 and rpy2-2.8.3. However it gives the following error when run on python-2.7.8 and rpy2-2.8.5 :
/usr/local/lib/python2.7/dist-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/usr/local/lib/R/site-library/Rcpp/libs/Rcpp.so':
/usr/local/lib/R/site-library/Rcpp/libs/Rcpp.so: undefined symbol: R_ClassSymbol
warnings.warn(x, RRuntimeWarning)
Segmentation fault (core dumped)
rpy2 is a package which provide access to R when you are using python. So firstly make sure that you enabled R-shared libs when you compiles you R as follows:
./configure --prefix=* --with-x=no --enable-R-shlib=yes
make sure you have specified the following environment variable before you distribute your rpy2 package:
export C_INCLUDE_PATH=/path/to/R/lib64/R/include
Last but not least, only those packages installed when all of this requirements are satisfied can be rightly imported in python by importr. And if not, you may need to reinstall the R packages.