Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check programmatically whether the current installation of R is the newest one?

Tags:

r

cran

How to check, from the level of R, if current installation of R is the newest one ? Finding version of installed R is easy part, but how to check what is the number of the newest version ? Is that kind of information available through CRAN ?

like image 650
Qbik Avatar asked Feb 16 '23 00:02

Qbik


2 Answers

A quick search in my favourite search engine found this post by Yihui Xie which I turned into a function:

checkRversion <- function(){
  x = readLines("http://cran.r-project.org/sources.html")
  # the version number is in the next line of 'The latest release'
  rls = x[grep("latest release", x) + 1L]
  newver = gsub("(.*R-|\\.tar\\.gz.*)", "", rls)
  oldver = paste(getRversion(), collapse = ".")
  # new version available?
  message("Installed version: ", oldver)
  message("Latest version: ", newver)
  compareVersion(newver, oldver)
}

In use:

checkRversion()

Installed version: 3.0.1
Latest version: 3.0.1
[1] 0
like image 195
Andrie Avatar answered Feb 27 '23 11:02

Andrie


You can use approach from gtools.

The code is similar to that in Andrie's answer, but uses the /src/base/R folder explicitly - rather than sources.html file, so it's potentially more robust because relies on the actual binaries being present.

A small problem is, gtools hardcoded the folder name, so "as is" their code is wrong - but I quite like the idea, so I updated it to iterate through the available CRAN urls and find the latest one:

checkRVersion <- function (quiet = FALSE) 
{
    baseUrl <- "http://cran.r-project.org/src/base/R-"
    majorVersion <- 3
    repeat {
        url <- paste(baseUrl, majorVersion, sep = "")
        if (url.exists(url)) {
            majorVersion <- majorVersion + 1
        }
        else {
            break
        }
    }

    url <- paste(baseUrl, (majorVersion-1), sep = "")
    page <- scan(file = url, what = "", quiet = TRUE)
    matches <- grep("R-[0-9]\\.[0-9]+\\.[0-9]+", page, value = TRUE)
    versionList <- gsub("^.*R-([0-9].[0-9]+.[0-9]+).*$", "\\1", matches)
    versionList <- numeric_version(versionList)

    if (max(versionList) > getRversion()) {
        if (!quiet) {
            cat("A newer version of R is now available: ")
            cat(as.character(max(versionList)))
            cat("\n")
        }
        invisible(max(versionList))
    }
    else {
        if (!quiet) {
            cat("The latest version of R is installed: ")
            cat(as.character(max(versionList)))
            cat("\n")
        }
        invisible(NULL)
    }
}
like image 44
andreister Avatar answered Feb 27 '23 09:02

andreister