我正在使用rpy2在Python中使用R库。该库具有函数prebas(),该函数返回一个数组,其中索引为[8]的项为输出。当我在R代码段中将此输出写入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")
这是代码段返回的内容:
(Pdb) run_prebasso
<rpy2.rinterface.NULLType object at 0x7fc1b31e6b48> [RTYPES.NILSXP]
问题:为什么written_in_py.csv
和written_in_r.csv
不同?
我刚发现错误。问题出在行中
write.csv(PREBASout[8],"/home/outputs/written_in_r.csv",row.names = F)
此语句返回,而不是我想要的(PREBASout [8])。当我删除它或将其分配给变量时,一切都按预期工作。