我从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"

推荐答案

为了回答我最初的问题,将以下内容添加到我的提交脚本中确实会输出错误和控制台输出(只要您的RScript不仅仅是一条大 comments !)

#SBATCH -o /hpcfs/users/username/folder/%j.out
#SBATCH -e /hpcfs/users/username/folder/%j.err

为什么这一点一开始似乎不起作用,根本问题是nano隐藏了一个格式问题.我的代码是在我的计算机上编写的,并粘贴到nano中;当我习惯了新的高性能计算系统时,我使用了一个简短的例子.它在nano中看起来很好,但当我的同事在vim中查看它时,它使用的是Windows/dos行源,而不是posix.正因为如此,它"将我的整个脚本视为 comments ,因此没有错误或输出.纠正后,它现在的行为和输出符合预期.

我还发现您可以在Rstudio中控制行提要 工具全局选项代码保存行结束转换. 已切换为posix.我现在可以剪切和粘贴我在PC上使用的代码.

R相关问答推荐

使用case_when和Mutate搜索多个列以寻找条件

使用na.locf在长格式数据集中输入具有多个时间点的数据集

如何判断某列中由某些行组成的百分比

基于shiny 应用程序中的日期范围子集xts索引

如何使用stat_extract_all正确提取我的目标值?

隐藏e_mark_line的工具提示

基于多列将值链接到NA

修改用R编写的用户定义函数

Rplotly中的Sankey Diagram:意外连接&

是否可以创建一个ggplot与整洁判断的交互作用

根据日期从参考帧中创建不同的帧

多个模拟序列间的一种预测回归关系

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

根据约束随机填充向量的元素

如何根据未知数的多列排除重复行

删除在R中的write.table()函数期间创建的附加行

是否有可能从边界中找到一个点值?

如何构建一个for循环来循环处理动物ID?

在shiny 表格中输入的文本在第一次后未更新

为什么R列名称忽略具有指定名称的向量,而只关注索引?