python - rpy2 在脚本结束时产生无用的警告
问题描述
我是第一次使用 py2r。一切正常,除了在我的脚本结束时,我在 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
这是一个 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")
和输出:
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
这似乎不是一个正确的警告,因为 warnings.simplefilter("ignore") 不会停止该消息。这发生在虚拟环境和系统(用户)python3中。
这是什么意思,我该如何阻止它?这是否表明某些事情可能会导致其他人的系统出现实际错误?
先感谢您。
解决方案
可以说,警告的用处在于旁观者的眼中。
这里发生的事情是,R 发出了一个警告,据我所知,它与令人惊讶的空目录有关。在不知道更多信息的情况下很难判断是否有需要担心的问题,但我建议检查 R 是如何安装在该系统上的。
否则,可以通过自定义回调(请参阅文档 - https://rpy2.github.io/doc/v3.3.x/html/callbacks.html#write-console)或通过使用的记录器来使 R 的此类消息静音默认回调(参见代码 - https://github.com/rpy2/rpy2/blob/master/rpy2/rinterface_lib/callbacks.py#L119)。
推荐阅读
- react-native-android - 无法从 react-native 中的 asyncStorage 获取价值
- azure - 每个订阅的单个消费计划可以创建多少个功能应用/插槽?
- sqlite - PRAGMA incremental_vacuum(1000) 只会删除一页,为什么?
- python - 赛车游戏:根据是否在赛道上改变汽车运动
- typescript - 如何在不导入库的情况下获得 VS Code / typescript 自动完成
- java - 我们如何为多参数构建字符串?
- r - 如何在 R 中反应性地做多个情节?
- javascript - 当 Vuex 存储状态发生变化时,Vue 应用程序状态不会更新
- matlab - 在 matlab 程序中一次调用多个具有不同名称的文本文件。这些文本文件保存在一个文件夹中
- javascript - 将 javascript 提示文本设为只读