You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言如何编写可从上次中断位置继续执行的下载循环

问题原因
  • 索引重复叠加amount_done:你已经将循环起始值设为1+amount_done,循环体内不需要再额外加amount_done,两次叠加会导致直接跳过未下载的首个剩余项,甚至出现索引越界。
  • 赋值索引错位:新下载的内容应该按顺序追加到data的对应位置,你用i+amount_done作为索引会导致data中间出现空位。
修正后的代码
# data = 存储所有下载结果的列表
data = list()

# 昨天已经完成1个下载任务,已完成数量为1
data[[1]] <- "1"
amount_done <- length(data)

# 待下载的全部资源列表
item_list <- c("1", "2", "3", "4")

# 修正后的断点续传循环
for (i in (amount_done + 1):length(item_list)){
  data[[i]] <- item_list[i] 
  print(data[[i]])
}
运行验证

执行代码后输出内容为:

[1] "2"
[1] "3"
[1] "4"

查看data最终值完全符合预期:

> data
[[1]]
[1] "1"

[[2]]
[1] "2"

[[3]]
[1] "3"

[[4]]
[1] "4"

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 19:24:00