Creates a shortcut to the target Drive file, which could be a folder. A Drive shortcut functions like a symbolic or "soft" link and is primarily useful for creating a specific Drive user experience in the browser, i.e. to make a Drive file or folder appear in more than 1 place. Shortcuts are a relatively new feature in Drive; they were introduced when Drive stopped allowing a file to have more than 1 parent folder.

shortcut_create(file, path = NULL, name = NULL, overwrite = NA)

Arguments

file

Something that identifies the file of interest on your Google Drive. Can be a name or path, a file id or URL marked with as_id(), or a dribble.

path

Target destination for the new shortcut, i.e. a folder or a shared drive. Can be given as an actual path (character), a file id or URL marked with as_id(), or a dribble. Defaults to your "My Drive" root folder. If path is a shortcut to a folder, it is automatically resolved to its target folder.

name

Character, new shortcut name if not specified as part of path. This will force path to be interpreted as a folder, even if it is character and lacks a trailing slash. By default, the shortcut starts out with the same name as the target file. As a consequence, if you want to use overwrite = TRUE or overwrite = FALSE, you must explicitly specify the shortcut's name.

overwrite

Logical, indicating whether to check for a pre-existing file at the targetted "filepath". The quotes around "filepath" refer to the fact that Drive does not impose a 1-to-1 relationship between filepaths and files, like a typical file system; read more about that in drive_get().

  • NA (default): Just do the operation, even if it results in multiple files with the same filepath.

  • TRUE: Check for a pre-existing file at the filepath. If there is zero or one, move a pre-existing file to the trash, then carry on. Note that the new file does not inherit any properties from the old one, such as sharing or publishing settings. It will have a new file ID. An error is thrown if two or more pre-existing files are found.

  • FALSE: Error if there is any pre-existing file at the filepath.

Note that existence checks, based on filepath, are expensive operations, i.e. they require additional API calls.

Value

An object of class dribble, a tibble with one row per file.

See also

Examples

# Create a file to make a shortcut to
file <- drive_upload(
  drive_example("chicken.csv"),
  name = "chicken-sheet-for-shortcut",
  type = "spreadsheet"
)
#> Local file:
#>/home/runner/work/_temp/Library/googledrive/extdata/chicken.csv
#> Uploaded into Drive file:
#>chicken-sheet-for-shortcut <id: 16rpx_saNNJzqzhHjYYhd6npiJ6fIqDYn9gRUQ56MsJs>
#> With MIME type:
#>application/vnd.google-apps.spreadsheet

# Create a shortcut in the default location with the default name
sc1 <- shortcut_create(file)
#> Created Drive file:
#>chicken-sheet-for-shortcut <id: 115ZYECUVsyXnSkVAPiQfJs5qVeTIAnUO>
#> With MIME type:
#>application/vnd.google-apps.shortcut
# This shortcut could now be moved, renamed, etc., which is probably a good
# idea, since it's confusing to have a file and same-named shortcut
# in the same place!

# Create a shortcut in the default location with a custom name
sc2 <- file %>%
  shortcut_create(name = "chicken-sheet-second-shortcut")
#> Created Drive file:
#>chicken-sheet-second-shortcut <id: 1dwDoAq8BLqvaw3iyhjByYB7-Iq1SF0yw>
#> With MIME type:
#>application/vnd.google-apps.shortcut

# Create a folder, then put a shortcut there, with default name
folder <- drive_mkdir("chicken-sheet-shortcut-folder")
#> Created Drive file:
#>chicken-sheet-shortcut-folder <id: 18YbkcliTKfbx2fsxFQwBb9Eznr13D-rO>
#> With MIME type:
#>application/vnd.google-apps.folder
sc3 = file %>%
  shortcut_create(folder)
#> Created Drive file:
#>chicken-sheet-for-shortcut <id: 1pQUHuT5aBXE1Yq2wEYvEOhfAsQSdLwJW>
#> With MIME type:
#>application/vnd.google-apps.shortcut

# Look at all these shortcuts
(dat <- drive_find("chicken-sheet", type = "shortcut"))
#> # A dribble: 3 x 3
#>   name                         id                               drive_resource  
#> * <chr>                        <chr>                            <list>          
#> 1 chicken-sheet-for-shortcut   1pQUHuT5aBXE1Yq2wEYvEOhfAsQSdLw… <named list [30
#> 2 chicken-sheet-second-shortc… 1dwDoAq8BLqvaw3iyhjByYB7-Iq1SF0… <named list [30
#> 3 chicken-sheet-for-shortcut   115ZYECUVsyXnSkVAPiQfJs5qVeTIAn… <named list [30

# Get the id of the original file from the shortcuts
dat <- dat %>%
  drive_reveal("shortcut_details")
purrr::map_chr(dat$shortcut_details, "targetId")
#> [1] "16rpx_saNNJzqzhHjYYhd6npiJ6fIqDYn9gRUQ56MsJs"
#> [2] "16rpx_saNNJzqzhHjYYhd6npiJ6fIqDYn9gRUQ56MsJs"
#> [3] "16rpx_saNNJzqzhHjYYhd6npiJ6fIqDYn9gRUQ56MsJs"
as_id(file)
#> [1] "16rpx_saNNJzqzhHjYYhd6npiJ6fIqDYn9gRUQ56MsJs"
#> attr(,"class")
#> [1] "drive_id"

# Clean up
drive_rm(sc1, sc2, sc3, file, folder)
#> Files deleted:
#>chicken-sheet-for-shortcut <id: 115ZYECUVsyXnSkVAPiQfJs5qVeTIAnUO>
#>chicken-sheet-second-shortcut <id: 1dwDoAq8BLqvaw3iyhjByYB7-Iq1SF0yw>
#>chicken-sheet-for-shortcut <id: 1pQUHuT5aBXE1Yq2wEYvEOhfAsQSdLwJW>
#>chicken-sheet-for-shortcut <id: 16rpx_saNNJzqzhHjYYhd6npiJ6fIqDYn9gRUQ56MsJs>
#>chicken-sheet-shortcut-folder <id: 18YbkcliTKfbx2fsxFQwBb9Eznr13D-rO>