source_url: Download an R file from a URL and source it

Description Usage Arguments Details See Also Examples

Description

This will download a file and source it. Because it uses the download() function, it can handle https URLs.

Usage

1
source_url(url, sha = NULL, ..., prompt = TRUE, quiet = FALSE)

Arguments

url

The URL to download.

sha

A SHA-1 hash of the file at the URL.

...

Other arguments that are passed to source().

prompt

Prompt the user if no value for sha is provided.

quiet

If FALSE (the default), print out status messages about checking SHA.

Details

By default, source_url() checks the SHA-1 hash of the file. If it differs from the expected value, it will throw an error. The default expectation is that a hash is provided; if not, source_url() will prompt the user, asking if they are sure they want to continue, unless prompt=FALSE is used. In other words, if you use prompt=FALSE, it will run the remote code without checking the hash, and without asking the user.

The purpose of checking the hash is to ensure that the file has not changed. If a source_url command with a hash is posted in a public forum, then others who source the URL (with the hash) are guaranteed to run the same code every time. This means that the author doesn't need to worry about the security of the server hosting the file. It also means that the users don't have to worry about the file being replaced with a damaged or maliciously-modified version.

To find the hash of a local file, use digest(). For a simple way to find the hash of a remote file, use sha_url().

See Also

source() for more information on the arguments that can be used with this function. The sha_url() function can be used to find the SHA-1 hash of a remote file.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## Not run: 
# Source the a sample file

# This is a very long URL; break it up so it can be seen more easily in the
# examples.
test_url <- paste0("https://gist.github.com/wch/dae7c106ee99fe1fdfe7",
                   "/raw/db0c9bfe0de85d15c60b0b9bf22403c0f5e1fb15/test.r")
downloader::source_url(test_url,
                       sha = "9b8ff5213e32a871d6cb95cce0bed35c53307f61")

# Find the hash of a file
downloader::sha_url(test_url)

## End(Not run)

downloader documentation built on May 2, 2019, 8:19 a.m.