I would like to use rpy2 to access R from Python. I just tried to install the R, Python, and Rpy2 packages, but they don’t work. It looks like rpy2 installs its own R and it’s not looking for r packages where they are installed already and I get an error
library ‘/home/user/R/x86_64-pc-linux-gnu-library/4.3’ contains no packages
my shell.nix is
with import <nixpkgs> { };
let
py = python312Packages;
r-with-my-packages = rWrapper.override{
packages = with rPackages; [ tidyverse ggplot2 data_table metafor ]; };
in pkgs.mkShell {
propagatedBuildInputs = [
r-with-my-packages
py.python
py.numpy
py.scipy
py.scikit-learn
py.matplotlib
py.rpy2
py.pytest
];
}
Thanks for your reply. I weren’t able to make it work with the extraRpackages. I put
extraRPackages = [ metafor data_table ]
in my nix shell, but it didn’t work. I suppose that’s not how it’s supposed to be used. However just removing rpackages from the rWarpper.override, and putting them in the list with the rest of the python packages worked. So my now working shell is this
with import <nixpkgs> { };
let
py = python312Packages;
in pkgs.mkShell {
packages = [
#r-with-my-packages
R
rPackages.metafor
rPackages.data_table
py.python
py.numpy
py.scipy
py.scikit-learn
py.matplotlib
py.rpy2
py.pytest
];
try (py.rpy2.override {extraRPackages = with rPackages; [ metafor data_table ];})
PS when you say “didn’t work”, please be specific. It’s basic troubleshooting etiquette, and also gets others to understand if you applied the fix incorrectly vs if the suggested fix itself was incorrect. If there’s an error, give the full error without truncation.
Thanks. By not working I just meant that nothing changed, the error is the same, i.e., the r packages are not visible from python, with an error
rpy2.robjects.packages.PackageNotInstalledError: The R package "metafor" is not installed.
and warning
R[write to console]: library ‘/home/brick/R/x86_64-pc-linux-gnu-library/4.3’ contains no packages
I should have been more specific.
Your solution with rpy2.overide works, as well as just puting the r packages in the same list with other packages, without rWrapper.overide. The difference is that with rpy.override, the packages are not accessible from R itself, which might or might not be desirable for different situations.