【问题标题】:rpy2 code snippet returns an empty objectrpy2 代码片段返回一个空对象
【发布时间】:2020-02-02 19:34:41
【问题描述】:

我正在使用 rpy2 在 Python 中使用 R 库。该库有一个函数 prebas() 返回一个数组,其中索引为 [8] 的项目是输出。当我在 R 代码 sn-p 中将此输出写入 CSV 时,一切都按预期工作(输出是超过 200kB 的 CSV)。但是,当我返回同一个对象 (PREBASout[8]) 时,它返回一个空对象。所以,很明显,当我将该对象写入 CSV 时,文件是空的。

run_prebasso = robjects.r('''
        weather <- read.csv("/home/example_inputs/weather.csv",header = T)
        PAR = c(weather$PAR,weather$PAR,weather$PAR)
        TAir = c(weather$TAir,weather$TAir,weather$TAir)
        Precip = c(weather$Precip,weather$Precip,weather$Precip)
        VPD = c(weather$VPD,weather$VPD,weather$VPD)
        CO2 = c(weather$CO2,weather$CO2,weather$CO2)
        DOY = c(weather$DOY,weather$DOY,weather$DOY)
        library(Rprebasso)

        PREBASout = prebas(nYears = 100, PAR=PAR,TAir=TAir,VPD=VPD,Precip=Precip,CO2=CO2)
        write.csv(PREBASout[8],"/home/outputs/written_in_r.csv",row.names = F)

        PREBASout[8]

''')

r_write_csv = robjects.r['write.csv']

r_write_csv(run_prebasso, "/home/outputs/written_in_py.csv")

这是sn-p返回的代码:

(Pdb) run_prebasso
<rpy2.rinterface.NULLType object at 0x7fc1b31e6b48> [RTYPES.NILSXP]

问题:为什么written_in_py.csvwritten_in_r.csv 不一样?

【问题讨论】:

    标签: python r rpy2


    【解决方案1】:

    我刚刚发现了这个错误。问题出在线路上

    write.csv(PREBASout[8],"/home/outputs/written_in_r.csv",row.names = F)
    

    返回此语句而不是我想要的 (PREBASout[8])。当我删除它或将其分配给变量时,一切都按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2014-07-15
      • 2018-08-25
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多