use_course_details: Helpers to download and unpack a ZIP file

use_course_detailsR Documentation

Helpers to download and unpack a ZIP file


Details on the internal and helper functions that power use_course() and use_zip(). Only create_download_url() is exported.





a GitHub, DropBox, or Google Drive URL, as copied from a web browser.


Path to existing local directory where the ZIP file will be stored. Defaults to current working directory, but note that use_course() has different default behavior.


Path to local ZIP file.


## function signature
tidy_download(url, destdir = getwd())

# as called inside use_course()
  url, ## after post-processing with normalize_url()
  # conspicuous_place() = `getOption('usethis.destdir')` or desktop or home
  # directory or working directory
  destdir = destdir %||% conspicuous_place()

Special-purpose function to download a ZIP file and automatically determine the file name, which often determines the folder name after unpacking. Developed with DropBox and GitHub as primary targets, possibly via shortlinks. Both platforms offer a way to download an entire folder or repo as a ZIP file, with information about the original folder or repo transmitted in the Content-Disposition header. In the absence of this header, a filename is generated from the input URL. In either case, the filename is sanitized. Returns the path to downloaded ZIP file, invisibly.

tidy_download() is setup to retry after a download failure. In an interactive session, it asks for user's consent. All retries use a longer connect timeout.


To make a folder available for ZIP download, create a shared link for it:

A shared link will have this form:

Replace the dl=0 at the end with dl=1 to create a download link:

You can use create_download_url() to do this conversion.

This download link (or a shortlink that points to it) is suitable as input for tidy_download(). After one or more redirections, this link will eventually lead to a download URL. For more details, see and


Click on the repo's "Clone or download" button, to reveal a "Download ZIP" button. Capture this URL, which will have this form:

This download link (or a shortlink that points to it) is suitable as input for tidy_download(). After one or more redirections, this link will eventually lead to a download URL. Here are other links that also lead to ZIP download, albeit with a different filenaming scheme (REF could be a branch name, a tag, or a SHA):

You can use create_download_url() to create the "Download ZIP" URL from a typical GitHub browser URL.

Google Drive

To our knowledge, it is not possible to download a Google Drive folder as a ZIP archive. It is however possible to share a ZIP file stored on Google Drive. To get its URL, click on "Get the shareable link" (within the "Share" menu). This URL doesn't allow for direct download, as it's designed to be processed in a web browser first. Such a sharing link looks like:

To be able to get the URL suitable for direct download, you need to extract the "id" element from the URL and include it in this URL format:

Use create_download_url() to perform this transformation automatically.


Special-purpose function to unpack a ZIP file and (attempt to) create the directory structure most people want. When unpacking an archive, it is easy to get one more or one less level of nesting than you expected.

It's especially important to finesse the directory structure here: we want the same local result when unzipping the same content from either GitHub or DropBox ZIP files, which pack things differently. Here is the intent:

  • If the ZIP archive does not contain a single top-level directory, i.e. it is packed as "loose parts", unzip into a directory named foo. Typical of DropBox ZIP files.

  • If the ZIP archive has a single top-level directory (which, by the way, is not necessarily called "foo"), unpack into said directory. Typical of GitHub ZIP files.

Returns path to the directory holding the unpacked files, invisibly.

DropBox: The ZIP files produced by DropBox are special. The file list tends to contain a spurious directory "/", which we ignore during unzip. Also, if the directory is a Git repo and/or RStudio Project, we unzip-ignore various hidden files, such as .RData, .Rhistory, and those below ⁠.git/⁠ and .Rproj.user.


## Not run: 

## End(Not run)

## Not run: 

## End(Not run)
# GitHub

# DropBox

# Google Drive

usethis documentation built on July 9, 2023, 7:23 p.m.