Home > Software engineering >  How do I map a function to a dataframe to download images from urls in each row and save to a folder
How do I map a function to a dataframe to download images from urls in each row and save to a folder

Time:10-12

I have a dataframe of a concatenation of a product id and product image url. See example below -

"37420 //trek.scene7.com/is/image/TrekBicycleProducts/MadoneSLR9_23_37419_C_Primary?wid=1360&hei=1020&fmt=pjpeg&qlt=40,1&iccEmbed=0&cache=on,on"

I want to create a function that for each row, it downloads a png of the product and saves it in a folder as a string of "product_id" and product id. So the example above will be saved as product_id_37420.png. I believe I have created the function correctly, however I'm not sure how to map it to the dataframe and download pngs for all rows. See sample data and function below -

# sample data
library(tidyverse)

sample_tbl <- tibble(
    concat = c(
        "37420 //trek.scene7.com/is/image/TrekBicycleProducts/MadoneSLR9_23_37419_C_Primary?wid=1360&hei=1020&fmt=pjpeg&qlt=40,1&iccEmbed=0&cache=on,on",
        "37419 //trek.scene7.com/is/image/TrekBicycleProducts/MadoneSLR9eTap_23_37420_B_Primary?wid=1360&hei=1020&fmt=pjpeg&qlt=40,1&iccEmbed=0&cache=on,on"
    )
)

# function
get_bikes_images <- function() {
    
    pid  = sub(" .*", "", sample_tbl$concat)
    url  = sub(".* ", "", sample_tbl$concat)
    path = paste0("../jpg/", "product_id_", pid, ".jpg")
    
    download.file(url = paste0("https:", url), destfile = path, mode = "wb")
    
}

pid grabs the product id

url grabs the product image url

path creates the path to save the image as a png in a folder called png

how do I map this function to the dataframe. I've tried using map, future_map, apply, but I get errors.

CodePudding user response:

my proposition:

get_bikes_images <- function(x) {
    pid  = sub(" .*", "", x)
    url  = sub(".* ", "", x)
    path = paste0("../jpg/", "product_id_", pid, ".jpg")
    
    download.file(url = paste0("https:", url), destfile = path, mode = "wb")
}

#maps the function over the column.
walk(sample_tbl$concat, get_bikes_images)
  • Related