Search code examples
pythonrwarningsrpy2

rpy2 produces useless warning on script end


I'm using py2r for the first time. Everything functions fine except at the end of my script I see the following output on stderr:

R[write to console]: Warning message:

R[write to console]: In (function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,  :
R[write to console]: 
 
R[write to console]:  libraries ‘/usr/local/lib/R/site-library’, ‘/usr/lib/R/site-library’ contain no packages

Here's an MRE:

import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

df_counts = pd.DataFrame([[15, 25, 40, 23, 17, 40,],
                          [11, 22, 33, 22, 11, 33,],
                          [26, 36, 62, 48, 15, 62,]],
                         index=["A", "B", "C"],
                         columns=["ca_w", "ca_wo", "ca_n", "co_w", "co_wo", "co_n"])

r_exact2x2 = importr("exact2x2")
def get_sig_stats_from_r(cluster):
    ca_w = cluster["ca_w"]
    ca_wo = cluster["ca_wo"]
    ca_n = cluster["ca_n"]
    co_w = cluster["co_w"]
    co_wo = cluster["co_wo"]
    co_n = cluster["co_n"]

    uncond_exact = r_exact2x2.uncondExact2x2(ca_w, ca_n, co_w, co_n)
    fisher_table = ro.r.matrix(ro.vectors.IntVector([ca_w, co_w, ca_wo, co_wo]), nrow=2)
    fisher_exact = r_exact2x2.fisher_exact(fisher_table)
    uncond_p = uncond_exact.rx2("p.value")[0]
    fisher_p = fisher_exact.rx2("p.value")[0]
    fisher_OR = fisher_exact.rx2("estimate")[0]
    fisher_CI = fisher_exact.rx2("conf.int")
    fisher_CI = list(fisher_CI)

    return [uncond_p, fisher_p, fisher_OR, fisher_CI]

df_sig = df_counts.apply(get_sig_stats_from_r, axis=1, result_type="expand")
print(df_sig)
df_sig.to_csv("foo.csv")

And the output:

          0         1         2                 3
A  0.086901  0.116535  0.448134  [0.1755, 1.1045]
B  0.009209  0.013221  0.255747  [0.0891, 0.7303]
C  0.000051  0.000123  0.228655  [0.1015, 0.4998]
R[write to console]: Warning message:

R[write to console]: In (function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,  :
R[write to console]: 
 
R[write to console]:  libraries ‘/usr/local/lib/R/site-library’, ‘/usr/lib/R/site-library’ contain no packages

It doesn't seem to be a proper Warning as warnings.simplefilter("ignore") doesn't stop the message. This happens both in a virtual env and in system (user) python3.

What does this mean and how I can I stop it? Is this indicative of something that could cause an actual error on another persons system?

Thank you in advance.


Solution

  • It can be argued that the usefulness of warnings is in the eye of the beholder.

    Here what is happening is that R is emitting a warning that I understand to be related to surprisingly empty directories. It is hard to tell whether there is matter to worry without knowing more, but I would suggest to check how R was installed on that system.

    Otherwise silencing R's such messages can be done through custom callbacks (see the doc - https://rpy2.github.io/doc/v3.3.x/html/callbacks.html#write-console) or through the logger used by the default callback (see the code - https://github.com/rpy2/rpy2/blob/master/rpy2/rinterface_lib/callbacks.py#L119).