简体   繁体   English

等待rgbif下载完成,然后继续

[英]Wait for rgbif download to complete before proceeding

I am developing a small application in R Shiny. 我正在R Shiny中开发一个小型应用程序。 Part of the application will need to query GBIF to download species occurrence data. 该应用程序的一部分将需要查询GBIF以下载物种发生数据。 This is possible using rgbif . 使用rgbif可以rgbif The function rgbif::occ_download() will download the data and rgbif::occ_download_meta() will check whether GBIF has fulfilled your request. 函数rgbif::occ_download()将下载数据,而rgbif::occ_download_meta()将检查GBIF是否已满足您的要求。 For example: 例如:

geometry <- "POLYGON((30.1 10.1,40 40,20 40,10 20,30.1 10.1))"
res <- occ_download(paste0("geometry within ", geometry), type = "within", format = "SPECIES_LIST")
occ_download_meta(res)

<<gbif download metadata>>
  Status: RUNNING
  Format: SPECIES_LIST
  Download key: 0004089-190415153152247
  Created: 2019-04-25T09:18:20.952+0000
  Modified: 2019-04-25T09:18:21.045+0000
  Download link: http://api.gbif.org/v1/occurrence/download/request/0004089-190415153152247.zip
  Total records: 0

So far, so good. 到现在为止还挺好。 However, the following function rgbif::occ_download_get() can't download the data for downstream analysis until occ_download_meta(res) has completed (when Status = SUCCEEDED). 但是,直到occ_download_meta(res)完成(状态= SUCCEEDED时),以下函数rgbif::occ_download_get()才能下载数据进行下游分析。

How can I make the session wait until the download from GBIF has been completed? 如何使会话等待直到从GBIF下载完成? I cannot hard code a wait time into the script as different sized extents will take GBIF longer or shorter amounts of time to process. 我无法将等待时间硬编码到脚本中,因为不同大小的扩展区将花费GBIF更长或更短的时间。 Also, the number of other active users querying the service could also alter wait times. 同样,查询该服务的其他活动用户的数量也可能会更改等待时间。 I therefore need some sort of flag where Status == Succeeded before proceeding. 因此,在继续操作之前,我需要某种状态==成功的标志。

I have copied some skeleton code with comments below. 我在下面复制了一些带有注释的框架代码。

library(rgbif)

geometry <- "POLYGON((30.1 10.1,40 40,20 40,10 20,30.1 10.1))" # Define boundary
res <- occ_download(paste0("geometry within ", geometry), type = "within", format = "SPECIES_LIST")

# WAIT HERE UNTIL Status == SUCCEEDED
occ_download_meta(res)

x <- occ_download_get(res, overwrite = TRUE) # Download data 
data<-occ_download_import(x) # Import into R

rgbif maintainer here. rgbif维护者在这里。 You could do something like we have within the occ_download_queue() function: 您可以执行occ_download_queue()函数中类似的occ_download_queue()

res <- occ_download(paste0("geometry within ", geometry), type = "within", format = "SPECIES_LIST")
still_running <- TRUE
status_ping <- 3
while (still_running) {
  meta <- occ_download_meta(res)
  status <- meta$status
  still_running <- status %in% c("succeeded", "killed")
  Sys.sleep(status_ping) # sleep between pings
}

you probably want to check for succeeded and killed, and do something different if killed 您可能想检查是否成功并被杀死,如果被杀死,则可以做一些不同的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM