您可以使用httr
向API发送请求. 这里有一个例子:
library(move2)
library(httr)
# Credentials from the example page
username <- '8b536ed8-a681-4786-aa01-b26040106f9f'
token <- 'bdl@2nq@9SD!i31a1M7HxG7GQG93IQ0a'
# Insert username into URL
url <- sprintf("https://www.moveapps.org/web-partner/v1/workflowInstances/%s/artifacts/index", username)
# Send GET request
r <- GET(url = url, authenticate(username, token))
# Parse the response
res <- r |>
content(as = "parsed", simplifyDataFrame = TRUE)
这会产生工作流信息的嵌套列表:
str(res)
List of 3
$ workflowInstanceId : chr "8b536ed8-a681-4786-aa01-b26040106f9f"
$ workflowInstanceTitle: chr "Workflow Instance 001"
$ results :'data.frame': 9 obs. of 7 variables:
..$ appPositionInWorkflow: int [1:9] 0 1 2 3 3 4 4 5 5
..$ appTitle : chr [1:9] "Movebank Location move1" "Remove Outliers move1" "Filter Last X Days move1" "Animated Map (moveVis) move1" ...
..$ fileName : chr [1:9] "app-output.rds" "app-output.rds" "app-output.rds" "animation_moveVis.mp4" ...
..$ mimeType : chr [1:9] "application/r-rds" "application/r-rds" "application/r-rds" "video/quicktime" ...
..$ fileSize : int [1:9] 3194278 2932993 2932986 134280 2932986 15047 3683428 3683428 6017277
..$ modifiedAt : chr [1:9] "2023-03-17T10:00:59.892611Z" "2023-03-10T03:15:53.323066Z" "2023-03-10T03:16:02.329737Z" "2023-03-10T03:18:35.108558Z" ...
..$ links :'data.frame': 9 obs. of 1 variable:
.. ..$ self: chr [1:9] "https://www.moveapps.org/web-partner/v1/workflowInstances/8b536ed8-a681-4786-aa01-b26040106f9f/apps/0/results/app-output.rds" "https://www.moveapps.org/web-partner/v1/workflowInstances/8b536ed8-a681-4786-aa01-b26040106f9f/apps/1/results/app-output.rds" "https://www.moveapps.org/web-partner/v1/workflowInstances/8b536ed8-a681-4786-aa01-b26040106f9f/apps/2/results/app-output.rds" "https://www.moveapps.org/web-partner/v1/workflowInstances/8b536ed8-a681-4786-aa01-b26040106f9f/apps/3/results/a"| __truncated__ ...
现在要访问文件:
# Download first file
tmp <- tempfile()
w1 <- GET(res$results$links$self[1], authenticate(username, token), write_disk(tmp))
# Read downloaded file
movedat <- readRDS(tmp)
# Plot
plot(movedat)