Some googledrive functions are built to naturally handle multiple files, while others operate on a single file.

Functions that expect a single file:

Functions that allow multiple files:

In general, the principle is: if there are multiple parameters that are likely to vary across multiple files, the function is designed to take a single input. In order to use these function with multiple inputs, use them together with your favorite approach for iteration in R. Below is a worked example, focusing on tools in the tidyverse, namely the map() functions in purrr.

Upload multiple files, then rename them

Scenario: we have multiple local files we want to upload into a folder on Drive. Then we regret their original names and want to rename them.

Load packages.

Upload

Use the example files that ship with googledrive. This looks a bit odd, but the first call returns their names and the second returns full paths on the local system.

local_files <- drive_example() %>% 
  drive_example()
basename(local_files)
#> [1] "chicken.csv" "chicken.jpg" "chicken.pdf" "chicken.txt"

Create a folder on your Drive and upload all files into this folder by iterating over the local_files using purrr::map().

folder <- drive_mkdir("upload-into-me-article-demo")
#> Created Drive file:
#> • 'upload-into-me-article-demo' <id: 1pOY1uzYzeQnU-F8kLIhhEHCQyRZKCODy>
#> With MIME type:
#> • 'application/vnd.google-apps.folder'
with_drive_quiet(
  files <- map(local_files, ~ drive_upload(.x, path = folder))
)

First, let’s confirm that we uploaded the files into the new folder.

drive_ls(folder)
#> # A dribble: 4 x 3
#>   name        id                                drive_resource   
#> * <chr>       <chr>                             <list>           
#> 1 chicken.txt 1y101l2pzbe-ti_KyiGEoAenl9a0L3a1f <named list [38]>
#> 2 chicken.pdf 1nSooXgDGJY0_8Zcy71iGE7G-HZYoiuH6 <named list [38]>
#> 3 chicken.jpg 1pL-3dJLga9eDRN5gmmTg0ZWV2LbqiNa1 <named list [40]>
#> 4 chicken.csv 1E1dZ56mhibA66sKsQ8JDw1eiASG1YJaK <named list [38]>

Now let’s reflect on the files object returned by this operation. files is a list of dribbles, one per uploaded file.

str(files, max.level = 1)
#> List of 4
#>  $ : dribble [1 × 3] (S3: dribble/tbl_df/tbl/data.frame)
#>  $ : dribble [1 × 3] (S3: dribble/tbl_df/tbl/data.frame)
#>  $ : dribble [1 × 3] (S3: dribble/tbl_df/tbl/data.frame)
#>  $ : dribble [1 × 3] (S3: dribble/tbl_df/tbl/data.frame)

This would be a favorable data structure if you’ve got more map()ing to do, as you’ll see below.

But what if not? You can always row bind individual dribbles into one big dribble yourself with, e.g., dplyr::bind_rows().

bind_rows(files)
#> # A dribble: 4 x 3
#>   name        id                                drive_resource   
#> * <chr>       <chr>                             <list>           
#> 1 chicken.csv 1E1dZ56mhibA66sKsQ8JDw1eiASG1YJaK <named list [38]>
#> 2 chicken.jpg 1pL-3dJLga9eDRN5gmmTg0ZWV2LbqiNa1 <named list [40]>
#> 3 chicken.pdf 1nSooXgDGJY0_8Zcy71iGE7G-HZYoiuH6 <named list [38]>
#> 4 chicken.txt 1y101l2pzbe-ti_KyiGEoAenl9a0L3a1f <named list [38]>

Below we show another way to finesse this by using a variant of purrr::map() that does this for us, namely map_dfr().

Rename

Imagine that we now wish these file names had a date prefix. First, form the new names. We use glue::glue() for string interpolation but you could also use paste(). Second, we map over two inputs: the list of dribbles from above and the vector of new names.

(new_names <- glue("{Sys.Date()}_{basename(local_files)}"))
#> 2021-06-12_chicken.csv
#> 2021-06-12_chicken.jpg
#> 2021-06-12_chicken.pdf
#> 2021-06-12_chicken.txt
files_dribble <- map2_dfr(files, new_names, drive_rename)
#> Original file:
#> • 'chicken.csv' <id: 1E1dZ56mhibA66sKsQ8JDw1eiASG1YJaK>
#> Has been renamed:
#> • '2021-06-12_chicken.csv' <id: 1E1dZ56mhibA66sKsQ8JDw1eiASG1YJaK>
#> Original file:
#> • 'chicken.jpg' <id: 1pL-3dJLga9eDRN5gmmTg0ZWV2LbqiNa1>
#> Has been renamed:
#> • '2021-06-12_chicken.jpg' <id: 1pL-3dJLga9eDRN5gmmTg0ZWV2LbqiNa1>
#> Original file:
#> • 'chicken.pdf' <id: 1nSooXgDGJY0_8Zcy71iGE7G-HZYoiuH6>
#> Has been renamed:
#> • '2021-06-12_chicken.pdf' <id: 1nSooXgDGJY0_8Zcy71iGE7G-HZYoiuH6>
#> Original file:
#> • 'chicken.txt' <id: 1y101l2pzbe-ti_KyiGEoAenl9a0L3a1f>
#> Has been renamed:
#> • '2021-06-12_chicken.txt' <id: 1y101l2pzbe-ti_KyiGEoAenl9a0L3a1f>

We use purrr::map2_dfr() to work through files, the list of dribbles (= Drive files), and new_names, the vector of new names, and row bind the returned dribbles into a single dribble holding all files.

Let’s check on the contents of this folder again to confirm the new names:

drive_ls(folder)
#> # A dribble: 4 x 3
#>   name                   id                                drive_resource   
#> * <chr>                  <chr>                             <list>           
#> 1 2021-06-12_chicken.txt 1y101l2pzbe-ti_KyiGEoAenl9a0L3a1f <named list [39]>
#> 2 2021-06-12_chicken.pdf 1nSooXgDGJY0_8Zcy71iGE7G-HZYoiuH6 <named list [39]>
#> 3 2021-06-12_chicken.jpg 1pL-3dJLga9eDRN5gmmTg0ZWV2LbqiNa1 <named list [40]>
#> 4 2021-06-12_chicken.csv 1E1dZ56mhibA66sKsQ8JDw1eiASG1YJaK <named list [38]>

Let’s confirm that, by using map2_df2() instead of map2(), we got a single dribble back, instead of a list of one-row dribbles:

files_dribble
#> # A dribble: 4 x 3
#>   name                   id                                drive_resource   
#> * <chr>                  <chr>                             <list>           
#> 1 2021-06-12_chicken.csv 1E1dZ56mhibA66sKsQ8JDw1eiASG1YJaK <named list [38]>
#> 2 2021-06-12_chicken.jpg 1pL-3dJLga9eDRN5gmmTg0ZWV2LbqiNa1 <named list [40]>
#> 3 2021-06-12_chicken.pdf 1nSooXgDGJY0_8Zcy71iGE7G-HZYoiuH6 <named list [39]>
#> 4 2021-06-12_chicken.txt 1y101l2pzbe-ti_KyiGEoAenl9a0L3a1f <named list [39]>

What if you wanted to get a list back, because your downstream operations include yet more map()ing? Then you would use map2().

files_list <- map2(files, new_names, drive_rename)

Clean up

Our trashing function, drive_trash() is vectorized and can therefore operate on a multi-file dribble. We could trash these files like so:

drive_trash(files_dribble)

If you’re absolutely sure of yourself and happy to do something irreversible, you could truly delete these files with drive_rm(), which is also vectorized:

drive_rm(files_dribble)

Finally – and this is the code we will actually execute – the easiest way to delete these files is to delete their enclosing folder.

drive_rm(folder)
#> File deleted:
#> • 'upload-into-me-article-demo' <id: 1pOY1uzYzeQnU-F8kLIhhEHCQyRZKCODy>