cluelessresearch.com

political methodology, brazilian politics, etc.

Archive for August, 2008

Find the fastest R mirror

Some of the mirrors at CRAN can be very slow, even if geographically close. When updating many packages, or downloading a new version of R, this can be very annoying. Below is a function I wrote that: a) gets the list of R mirrors from CRAN; b) downloads the same (arm, ~65k) package from all mirrors; and c) displays the list of R mirrors from fastest to slowest.


fastestmirrors <- function(package="arm_1.1-15.tar.gz",size="small") {
    tmp <- url("http://cran.r-project.org/mirrors.html")
    mirrors <- readLines(tmp)
    close(tmp)
    mirrors <- grep("http",mirrors[grep("href",mirrors,value=FALSE)+1],
                    value=TRUE)[1:3]
    urls <- paste(mirrors,"src/contrib/",package,sep="")
    times <- sapply(urls,function(x) {
        tm <- try(system.time(download.file(x,"tmp"))[3])
        ifelse(class(tm)=="numeric",tm,NA)
    }
                    )
    tmp <- data.frame(mirror=mirrors,time=round(times,1))[order(times),]
    rownames(tmp) <- NULL
    tmp
}
No comments