Search code examples
rpackagecran

Can I get the URL of what will be used by install.packages?


When running install.packages("any_package") on windows I get the message :

trying URL

'somepath.zip'

I would like to get this path without downloading, is it possible ?

In other terms I'd like to get the CRAN link to the windows binary of the latest release (the best would actually be to be able to call a new function with the same parameters as install.packages and get the proper url(s) as an output).

I would need a way that works from the R console (no manual checking of the CRAN page etc).


Solution

  • I am not sure if this is what you are looking for. This build the URL from the repository information and building the file name of the list of available packages.

    #get repository name
    repos<- getOption("repos")
    
    #Get url for the binary package
    #contrib.url(repos, "both")
    contriburl<-contrib.url(repos, "binary")
    #"https://mirrors.nics.utk.edu/cran/bin/windows/contrib/3.5"
    
    #make data.frame of avaialbe packages
    df<-as.data.frame(available.packages())
    
    #find package of interest
    pkg <- "tidyr"  #example
    #ofinterest<-grep(pkg, df$Package)
    ofinterest<-match(pkg, df$Package)   #returns a single value
    
    #assemble name, assumes it is always a zip file
    name<-paste0(df[ofinterest,]$Package, "_", df[ofinterest,]$Version, ".zip")
    
    #make final URL 
    finalurl<-paste0(contriburl, "/", name)