我从R中的一小块更大的代码开始;该代码不需要计算,但接下来的步骤需要计算计算.当我以交互模式运行该代码时,该代码的行为符合预期.当将代码作为批处理作业(job)运行时,根据slurm,它"完成"并且.out文件为空,但未创建预期的输出- a .CSV -.
我想了解如何让R将其错误消息、警告等保存到文本文件中,我可以在代码作为批处理文件运行后访问该文件,这样我就可以在这种情况下以及在运行更复杂的作业(job)时诊断出了什么问题.
遵循this solution 进行类似的查询,但关于从命令行运行R,我try 使用sink().我的工作现在以错误告终:
"rsor:"msg-文件中出现意外输入("/niguanak/nigSp_1.Rout",Open ="WT") 停止执行
以错误结束的当前代码如下.前一个"完成"但没有输出预期的.CSV文件,是相同的,但缺乏与接收器相关的顶部两行和底部两行.niguanak是我运行作业(job)的文件夹,我在那里创建了一个空白文件nigSp_1.Rout,运行以下代码后该文件保持空白.
msg <- file("/niguanak/nigSp_1.Rout", open = "wt")
sink(msg, type = "message")
library(tidyverse)
#combine sp download and Dryas integrifolia missed from sp download
Ni.sp.dl <- read.csv("/hpcfs/users/a1233466/niguanak/data/0039174-240321170329656.csv")
DI.dl <- read.csv("/hpcfs/users/a1233466/niguanak/data/0045407-240321170329656.csv")
Ni.sp.dl <- rbind(Ni.sp.dl, DI.dl, make.row.names = FALSE)
rm(DI.dl)
#get rid of occurrenceStatus == Absent
Ni.sp.dl <- Ni.sp.dl %>%
filter(occurrenceStatus == "PRESENT")
#get rid of any less than 15º for invasive
Ni.sp.dl <- Ni.sp.dl %>%
filter(decimalLatitude > 15)
#Checked this with coordinates cleaner on laptop. None seemed problematic for this analysis.
###reshape and save
Ni.sp.dl_df <- as.data.frame(cbind(Ni.sp.dl$gbifID, Ni.sp.dl$species,
Ni.sp.dl$decimalLatitude, Ni.sp.dl$decimalLongitude))
colnames(Ni.sp.dl_df) <- c("ind_id", "tax", "lat", "lon")
#Make sure the numeric columns are numeric!
Ni.sp.dl_df$lat <- as.numeric(Ni.sp.dl_df$lat)
Ni.sp.dl_df$lon <- as.numeric(Ni.sp.dl_df$lon)
Ni.sp.dl_df$ind_id <- as.numeric(Ni.sp.dl_df$ind_id)
#write out
write.csv(Ni.sp.dl_df, file = "/hpcfs/users/a1233466/niguanak/Ni.sp.dl_df_b.csv")
sink(type="message")
close(msg)
我已经阅读了sink()和file()的示例和帮助文件,但也没有从中找出我做错了什么.
此外,如上所述具有接收器等的完整代码在交互式运行时似乎也能按预期工作.
编辑 :
在try 各种(绝望)解决方案时,我最终得到了以下代码.
#capture output
sink("nigSp_1.out", type = "messages")
#load required packages
library(tidyverse)
#combine downlaod and Dryas integrifolia missed from sp download
Ni.sp.dl <- read.csv("/hpcfs/users/a1233466/niguanak/data/0039174-240321170329656.csv")
DI.dl <- read.csv("/hpcfs/users/a1233466/niguanak/data/0045407-240321170329656.csv")
Ni.sp.dl <- rbind(Ni.sp.dl, DI.dl, make.row.names = FALSE)
rm(DI.dl)
#get rid of occurrenceStatus == Absent
Ni.sp.dl <- Ni.sp.dl %>%
filter(occurrenceStatus == "PRESENT")
#get rid of any less than 15 for invasive
Ni.sp.dl <- Ni.sp.dl %>%
filter(decimalLatitude > 15)
#Checked this with coordinates cleaner on laptop. None seemed problematic for this analysis.
###reshape and save
Ni.sp.dl_df <- as.data.frame(cbind(Ni.sp.dl$gbifID, Ni.sp.dl$species,
Ni.sp.dl$decimalLatitude, Ni.sp.dl$decimalLongitude))
colnames(Ni.sp.dl_df) <- c("ind_id", "tax", "lat", "lon")
#Make sure the numeric columns are numeric!
Ni.sp.dl_df$lat <- as.numeric(Ni.sp.dl_df$lat)
Ni.sp.dl_df$lon <- as.numeric(Ni.sp.dl_df$lon)
Ni.sp.dl_df$ind_id <- as.numeric(Ni.sp.dl_df$ind_id)
#write out in case of issues
write.csv(Ni.sp.dl_df, file = "/hpcfs/users/a1233466/niguanak/Ni.sp.dl_df.csv")
#return output to console
sink()
此操作在1秒内"完成",但.err文件中没有任何错误,.out文件中没有输出,没有生成"nigSp_1.out",也没有生成"Ni. sp. dl_DF.CSV"