简体   繁体   中英

What does the mode= argument of download.file do?

This is the second time that I searched online for help with download.file and solved my problem by calling the mode argument but I don't know what is the reason or when/why to use them. Just saw the suggestion and passed the argument to get my problem solved.

The R help file on download.file() is a bit too brief and dose not tell me when to use particular mode

mode character. The mode with which to write the file. Useful values are "w", "wb" (binary), "a" (append) and "ab". Only used for the "internal" method. (See also 'Details'.)

For me to get correct answer I had to pass the mode="wb" below; but why (maybe something to do with the s in https or should i just go with trial and error for now).

fileUrl <-"https://d396qusza40orc.cloudfront.net/getdata%2Fjeff.jpg"
download.file(fileUrl, destfile = "./data/leekjpg.jpg", mode="wb")

I would like get at least some rudimentary understanding of the method and mode argument in download.file and would appreciate your explanations or suggestive reads.

I am downloading more files, and it bothers me that I don't know when to pass some the related arguments.

This is what the docs say:

If mode is not supplied and url ends in one of .gz, .bz2, .xz, .tgz, .zip, .rda or .RData a binary transfer is done. Since Windows (unlike Unix-alikes) does distinguish between text and binary files, care is needed that other binary file types are transferred with mode = "wb". unix A progress bar tracks the transfer. If the file length is known, an equals sign represents 2% of the transfer completed: otherwise a dot represents 10Kb. Code written to download binary files must use mode = "wb", but the problems incurred by a text transfer will only be seen on Window

Basically it is saying that "w" and "wb" are the same when using on Unix-like OS, because they do not differentiate between text and binary files, but Windows does.

In Windows the line endings are slightly different. To be safe I use "w" when opening text files, where as "wb" when files are not supposed to be text, like jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM