【发布时间】:2021-02-03 03:03:27
【问题描述】:
我正在运行一个 for 循环来尝试查看并从我的 aws s3 帐户中提取信息。由于某种原因,当我运行 for 循环时,循环将在整个循环中随机停止。当我说随机时,每次运行循环时,循环都会在不同的位置停止。我得到的是与进程超时有关的错误。如果循环每次都停在同一个桶上,那么我想知道特定桶是否有问题。我注意到当我手动运行命令来列出存储桶中的文件时,有时会出现超时错误。如果我运行相同的命令,比如 3 次,可能会出现 3 次中的 2 次超时错误,但第 3 次会通过。我想知道是否可以在循环中添加 trycatch 或 withTimeout,但我正在努力弄清楚如何将它们实现到我的 for 循环中。当我尝试添加它们时,似乎在我收到超时错误之前仍然只有一次尝试。这是我要运行的循环
files <- NULL
#sample.id <- NULL
for(i in 1:length(sample.id.path2[[1]])){
a <- as.data.frame(system(paste0("mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE))
colnames(a)[1] <- "V1"
a$V1 <- gsub("^.{0,34}", "", a$V1)
a <- a %>% filter(str_detect(V1, 'fastq'))
#print(a)
b <- as.data.frame(paste0("", sample.id.path2[['V2']][i], sep = ""))
colnames(b)[1] <- "V1"
c <- as.data.frame(paste0(b$V1, a$V1, sep = ""))
colnames(c)[1] <- "V2"
d <- cbind(a,c)
print(d)
files <- rbind(files, d)
}
这是我尝试运行的一些示例
files <- NULL
#sample.id <- NULL
for(i in 1:length(sample.id.path2[[1]])){
a <- withTimeout(as.data.frame(system(paste0("mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE)), timeout = 100, cpu = 100, elapsed = 100, onTimeout = "error")
colnames(a)[1] <- "V1"
a$V1 <- gsub("^.{0,34}", "", a$V1)
a <- a %>% filter(str_detect(V1, 'fastq'))
#print(a)
b <- as.data.frame(paste0("", sample.id.path2[['V2']][i], sep = ""))
colnames(b)[1] <- "V1"
c <- as.data.frame(paste0(b$V1, a$V1, sep = ""))
colnames(c)[1] <- "V2"
d <- cbind(a,c)
print(d)
files <- rbind(files, d)
}
还有一个
files <- NULL
#sample.id <- NULL
for(i in 1:length(sample.id.path2[[1]])){
while(TRUE){
a <- try(as.data.frame(system(paste0("mcli ls s3/path/to/bucket/", sample.id.path2[['V2']][i], sep = ""), intern=TRUE)), silent=TRUE)
if(!is(df, 'try-error')) break
colnames(a)[1] <- "V1"
a$V1 <- gsub("^.{0,34}", "", a$V1)
a <- a %>% filter(str_detect(V1, 'fastq'))
}
#print(a)
b <- as.data.frame(paste0("", sample.id.path2[['V2']][i], sep = ""))
colnames(b)[1] <- "V1"
c <- as.data.frame(paste0(b$V1, a$V1, sep = ""))
colnames(c)[1] <- "V2"
d <- cbind(a,c)
print(d)
files <- rbind(files, d)
}
我觉得有一个简单的解决方案,但我正在努力找出我需要在循环中添加 trycatch 或 withTimeout 的确切位置
【问题讨论】:
-
不知道有没有办法在系统命令中添加
until?或类似的东西 -
@DaveArmstrong 哇.. 那是正确的。这解决了问题。我不知道我错过了。如果您想将其放入答案中,我会接受它..这样您就可以获得更多的街头信誉。我想出了另一个解决方案,它更多地涉及系统命令而不是 R。我将发布它以帮助其他任何人在这篇文章中的树桩