【问题标题】:downloading images using curl library in a loop over data frame在数据帧的循环中使用 curl 库下载图像
【发布时间】:2019-06-13 05:29:30
【问题描述】:

我正在尝试下载我从 Google 表单(实际上是该表单的回复表)研究的鸟类的图像。在这个简短的数据集中,我有每只鸟的 ID、捕获日期,以及从不同角度(尾巴、frontPop 等)拍摄的多张鸟的图像。

我想遍历这个数据集,下载每种类型的图像,并通过鸟的 ID、捕获日期和图像类型(对应于列名)来命名:

189307140_2019-01-14_tail.jpg

我可以使用“curl”包从电子表格中的每个 Google Drive 链接下载图像,如下所示:

library(curl)
curl_download(url = 'https://drive.google.com/uc?export=download&id=1kra8bSf4WMpoK8BTyFip2OxmUuz30Thl', 
              destfile = 'bird.jpg')

但我被困在一个更优雅的循环中(在我的数据集中的每个鸟和照片类型上)......

以下是数据示例:

dat <- structure(list(markerID = c(189307136L, 189307145L, 183337360L
), date = structure(c(17907, 17910, 17910), class = "Date"), 
    tail = c("https://drive.google.com/uc?export=download&id=13S9s_j6acfndEz4HbpG-v2ZRyT8LLgji", 
    "https://drive.google.com/uc?export=download&id=19XpBTLws94wtCtgPF6oXKM_GZ_cV4oMf", 
    "https://drive.google.com/uc?export=download&id=1I5zA8tJdEv26EzI9rwBSN5tVsASyT4Cl"
    ), frontPop = c("https://drive.google.com/uc?export=download&id=1lJgl3hin9sWQcV40aJgdYPQM6jzch2Lb", 
    "https://drive.google.com/uc?export=download&id=1QdB1KmyHrlKkTlux0fkyI1Aw-Pe15sYF", 
    "https://drive.google.com/uc?export=download&id=1xqNT9CPVfMj2ksxqMONGbWXyJKaKtLuR"
    ), backPop = c("https://drive.google.com/uc?export=download&id=1YAe4S7_LIrLsbOW2qCBuWjyOr_SgT54T", 
    "https://drive.google.com/uc?export=download&id=1QOC8rPDjWfy6PVSaXFycf4jDyUnV-Vbv", 
    "https://drive.google.com/uc?export=download&id=1Gyo4lXgp0nXbsdd0jA_kk1m2jW8RGGIY"
    )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-3L))

    # A tibble: 3 x 5
   markerID date       tail                                         frontPop                                     backPop                                     
      <int> <date>     <chr>                                        <chr>                                        <chr>                                       
1 189307136 2019-01-11 https://drive.google.com/uc?export=download… https://drive.google.com/uc?export=download… https://drive.google.com/uc?export=download…
2 189307145 2019-01-14 https://drive.google.com/uc?export=download… https://drive.google.com/uc?export=download… https://drive.google.com/uc?export=download…
3 183337360 2019-01-14 https://drive.google.com/uc?export=download… https://drive.google.com/uc?export=download… https://drive.google.com/uc?export=download…

【问题讨论】:

    标签: r for-loop curl tidyverse


    【解决方案1】:

    这是使用library(purrr) 解决问题的一种优雅方法:

    library(tidyverse)
    library(curl)
    
    dat <- structure(
      list(markerID = c(189307136L, 189307145L, 183337360L), 
           date = structure(c(17907, 17910, 17910), class = "Date"), 
           tail = c("https://drive.google.com/uc?export=download&id=13S9s_j6acfndEz4HbpG-v2ZRyT8LLgji", 
                    "https://drive.google.com/uc?export=download&id=19XpBTLws94wtCtgPF6oXKM_GZ_cV4oMf", 
                    "https://drive.google.com/uc?export=download&id=1I5zA8tJdEv26EzI9rwBSN5tVsASyT4Cl"), 
           frontPop = c("https://drive.google.com/uc?export=download&id=1lJgl3hin9sWQcV40aJgdYPQM6jzch2Lb", 
                        "https://drive.google.com/uc?export=download&id=1QdB1KmyHrlKkTlux0fkyI1Aw-Pe15sYF", 
                        "https://drive.google.com/uc?export=download&id=1xqNT9CPVfMj2ksxqMONGbWXyJKaKtLuR"), 
           backPop = c("https://drive.google.com/uc?export=download&id=1YAe4S7_LIrLsbOW2qCBuWjyOr_SgT54T", 
                       "https://drive.google.com/uc?export=download&id=1QOC8rPDjWfy6PVSaXFycf4jDyUnV-Vbv", 
                       "https://drive.google.com/uc?export=download&id=1Gyo4lXgp0nXbsdd0jA_kk1m2jW8RGGIY")), 
      class = c("tbl_df", "tbl", "data.frame"), 
      row.names = c(NA, -3L))
    
    read_and_title <- as_mapper(~curl_download(url = ..4, 
                                               destfile = paste0(..1,"_",..2,"_",..3,".png")))
    
    dat %>% 
      gather(key = "photo_type", value = "url", 3:5) %>% 
      pmap_chr(read_and_title)
    #> [1] "189307136_17907_tail.png"     "189307145_17910_tail.png"    
    #> [3] "183337360_17910_tail.png"     "189307136_17907_frontPop.png"
    #> [5] "189307145_17910_frontPop.png" "183337360_17910_frontPop.png"
    #> [7] "189307136_17907_backPop.png"  "189307145_17910_backPop.png" 
    #> [9] "183337360_17910_backPop.png"
    

    由reprex package (v0.2.1) 于 2019-01-18 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2011-06-30
      • 2020-10-15
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多