Skip to content

Commit 2cc40db

Browse files
Merge pull request #510 from UBC-DSCI/download-file
download.file in chapter 2
2 parents 67e8218 + 05f3cb3 commit 2cc40db

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

source/reading.Rmd

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ canlang_data
387387
```
388388

389389
### Reading tabular data directly from a URL
390-
We can also use `read_csv`, `read_tsv` or `read_delim` (and related functions)
390+
We can also use `read_csv`, `read_tsv`, or `read_delim` (and related functions)
391391
to read in data directly from a **U**niform **R**esource **L**ocator (URL) that
392392
contains tabular data. \index{URL!reading from} Here, we provide the URL of a remote file to
393393
`read_*`, instead of a path to a local file on our
@@ -402,6 +402,24 @@ canlang_data <- read_csv(url)
402402
canlang_data
403403
```
404404

405+
### Downloading data from a URL
406+
Occasionally the data available at a URL is not formatted nicely enough to use
407+
`read_csv`, `read_tsv`, `read_delim`, or other related functions to read the data
408+
directly into R. In situations where it is necessary to download a file
409+
to our local computer prior to working with it in R, we can use the `download.file`
410+
function. The first argument is the URL, and the second is a path where we would
411+
like to store the downloaded file.
412+
413+
```r
414+
download.file(url, "data/can_lang.csv")
415+
canlang_data <- read_csv("data/can_lang.csv")
416+
canlang_data
417+
```
418+
419+
```{r echo=FALSE, warning = FALSE}
420+
canlang_data
421+
```
422+
405423
### Previewing a data file before reading it into R
406424

407425
In many of the examples above, we gave you previews of the data file before we read

0 commit comments

Comments
 (0)