Search code examples
rdata-analysisdata-manipulation

How to rename multiple files inside a loop in R


I have downloaded one photo of each deputy. In total, I have 513 photos (but I hosted a file with 271 photos). Each photo was named with the ID of the deputy. I want to change the name of photo to the deputy's name. This means that "66179.jpg" file would be named "norma-ayub.jpg".

I have a column with the IDs ("uri") and their names ("name_lower"). I tried to run the code with "destfile" of download.file(), but it receives only a string. I couldn't find out how to work with file.rename().

And rename_r_to_R changes only the file extension.

I am a beginner in working with R.

CSV file: https://gist.github.com/gabrielacaesar/3648cd61a02a3e407bf29b7410b92cec

Photos: https://github.com/gabrielacaesar/studyingR/blob/master/chamber-of-deputies-17jan2019-files.zip (It's not necessary to download the ZIP file; When running the code below, you also get the photos, but it takes some time to download them)

deputados <- fread("dep-legislatura56-14jan2019.csv")

i <- 1

while(i <= 514) {
  tryCatch({
    url <- deputados$uri[i]
    api_content <- rawToChar(GET(url)$content)
    pessoa_info <- jsonlite::fromJSON(api_content)
    pessoa_foto <- pessoa_info$dados$ultimoStatus$urlFoto
    download.file(pessoa_foto, basename(pessoa_foto), mode = "wb")
    Sys.sleep(0.5)
  }, error = function(e) return(NULL)
  )
  i <- i + 1
}

Solution

  • I downloaded the files you provided and directly read them into R or unzipped them into a new folder respectivly:

    df <- data.table::fread(
      "https://gist.githubusercontent.com/gabrielacaesar/3648cd61a02a3e407bf29b7410b92cec/raw/1d682d8fcdefce40ff95dbe57b05fa83a9c5e723/chamber-of-deputies-17jan2019", 
      sep = ",",
      header = TRUE)
    download.file("https://github.com/gabrielacaesar/studyingR/raw/master/chamber-of-deputies-17jan2019-files.zip",
                  destfile = "temp.zip")
    dir.create("photos")
    unzip("temp.zip", exdir = "photos")
    

    Then I use list.files to get the file names of all photos, match them with the dataset and rename the photos. This runs very fast and the last bit will report if renaming the file was succesful.

    photos <- list.files(
      path = "photos", 
      recursive = TRUE,
      full.names = TRUE
    )
    
    for (p in photos) {
      id <- basename(p)
      id <- gsub(".jpg$", "", id)
      name <- df$name_lower[match(id, basename(df$uri))]
      fname <- paste0(dirname(p), "/", name, ".jpg")
      file.rename(p, fname)
    
    # optional
      cat(
        "renaming", 
        basename(p), 
        "to", 
        name, 
        "succesful:", 
        ifelse(success, "Yes", "No"),
        "\n"
      )
    }