如何使用 R 函数 download.file?

How can I use R function download.file?

我试过 运行 这个命令:

url <- 'http://www.rob-mcculloch.org/chm/nonlinvarsel_0.0.1.9001.tar.gz'
download.file(url, destfile = 'temp')
install.packages('temp', repos = NULL, type='source')

但起初,当我 运行 download.file 时,返回了我:

Warning messages: 1: In download.file(url, destfile = "temp") : URL http://www.rob-mcculloch.org/chm/nonlinvarsel_0.0.1.9001.tar.gz: cannot open destfile 'temp', reason 'Is a directory' 2: In download.file(url, destfile = "temp") : download had nonzero exit status

所以我尝试 运行 install.packages 结果返回了我:

Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) Warning: invalid package ‘temp’ Error: ERROR: no packages specified Warning message: In install.packages("temp", repos = NULL, type = "source") : installation of package ‘temp’ had non-zero exit status

您不必从 R 内部执行此操作(尽管这是可行的,只需更仔细地查看选项并参见下文)。我实际上喜欢 shell 这个:

/tmp> wget http://www.rob-mcculloch.org/chm/nonlinvarsel_0.0.1.9001.tar.gz
--2022-04-11 03:03:59--  http://www.rob-mcculloch.org/chm/nonlinvarsel_0.0.1.9001.tar.gz
Resolving www.rob-mcculloch.org (www.rob-mcculloch.org)... 162.241.219.65
Connecting to www.rob-mcculloch.org (www.rob-mcculloch.org)|162.241.219.65|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20001 (20K) [application/x-gzip]
Saving to: ‘nonlinvarsel_0.0.1.9001.tar.gz’

nonlinvarsel_0.0.1.9001.tar.gz                       100%[=====================================================================================================================>]  19.53K  --.-KB/s    in 0.03s   

2022-04-11 03:03:59 (634 KB/s) - ‘nonlinvarsel_0.0.1.9001.tar.gz’ saved [20001/20001]

/tmp> R CMD INSTALL nonlinvarsel_0.0.1.9001.tar.gz
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘nonlinvarsel’ ...
** using staged installation
** R
** data
*** moving datasets to lazyload DB
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (nonlinvarsel)
/tmp> 

或者同样在 R 中(我也先切换到 /tmp):

> setwd("/tmp")
> download.file("http://www.rob-mcculloch.org/chm/nonlinvarsel_0.0.1.9001.tar.gz", "nonlinvarsel_0.0.1.9001.tar.gz")
trying URL 'http://www.rob-mcculloch.org/chm/nonlinvarsel_0.0.1.9001.tar.gz'
Content type 'application/x-gzip' length 20001 bytes (19 KB)
==================================================
downloaded 19 KB

> install.packages("nonlinvarsel_0.0.1.9001.tar.gz", repos = NULL)
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
* installing *source* package ‘nonlinvarsel’ ...
** using staged installation
** R
** data
*** moving datasets to lazyload DB
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (nonlinvarsel)
>