8

是否可以从 R 中获取 CRAN 包的发布日期?我想获取最近发布的 k 个 CRAN 包的列表,或者在日期 dd-mm-yy 之后发布的所有包。类似于 available_packages_by_date.html 上的信息

available.packages() 命令有一个“字段”参数,但这仅从说明中提取字段。包裹描述上的日期字段并不总是最新的。

我可以使用html 页面中的智能正则表达式来获取它,但我不确定这个 html 文件的可靠性和最新性......在某些时候,Kurt 可能会决定对布局进行改造,这会破坏脚本。另一种方法是使用来自CRAN FTP的时间戳,但我也不确定这个解决方案有多好。我不确定是否有正式结构化的文件与发布日期?我假设 HTML 页面是从某个数据库自动生成的。

4

5 回答 5

5

原来有一个未记录的文件“packages.rds”,其中包含所有包的发布日期(不是时间)。我想这些数据每天都用来重新创建 HTML 文件。

下面是一个从该文件中提取发布日期的简单函数:

recent.packages.rds <- function(){
    mytemp <- tempfile();
    download.file("http://cran.r-project.org/web/packages/packages.rds", mytemp);
    mydata <- as.data.frame(readRDS(mytemp), row.names=NA);
    mydata$Published <- as.Date(mydata[["Published"]]);

    #sort and get the fields you like:
    mydata <- mydata[order(mydata$Published),c("Package", "Version", "Published")];
}
于 2012-01-12T05:13:43.530 回答
3

最好的方法是利用包DESCRIPTION发布在 cran 镜像上的事实,并且由于DESCRIPTION来自构建包,它包含有关打包时间的信息:

pkgs <- unname(available.packages()[, 1])[1:20]
desc_urls <- paste("http://cran.r-project.org/web/packages/", pkgs, "/DESCRIPTION", sep = "")
desc <- lapply(desc_urls, function(x) read.dcf(url(x)))

sapply(desc, function(x) x[, "Packaged"])
sapply(desc, function(x) x[, "Date/Publication"])

(我在这里将其限制为前 20 个包以说明基本思想)

于 2012-01-05T21:09:12.130 回答
2

这是一个使用 HTML 和正则表达式的函数。我仍然宁愿从更正式的地方获取信息,以防 HTML 改变布局。

recent.packages <- function(number=10){

    #html is malformed
    maxlines <- number*2 + 11
    mytemp <- tempfile()
    if(getOption("repos") == "@CRAN@"){
        repo <- "http://cran.r-project.org"
    } else {
        repo <- getOption("repos");
    }
    newurl <- paste(repo,"/web/packages/available_packages_by_date.html", sep="");
    download.file(newurl, mytemp);
    datastring <- readLines(mytemp, n=maxlines)[12:maxlines];

    #we only find packages from after 2010-01-01
    myexpr1 <- '201[0-9]-[0-9]{2}-[0-9]{2} </td> <td> <a href="../../web/packages/[a-zA-Z0-9\\.]{2,}/'
    myexpr2 <- '^201[0-9]-[0-9]{2}-[0-9]{2}'
    myexpr3 <- '[a-zA-Z0-9\\.]{2,}/$'
    newpackages <- unlist(regmatches(datastring, gregexpr(myexpr1, datastring)));
    newdates <- unlist(regmatches(newpackages, gregexpr(myexpr2, newpackages)));
    newnames <- unlist(regmatches(newpackages, gregexpr(myexpr3, newpackages)));

    newdates <- as.Date(newdates);
    newnames <- substring(newnames, 1, nchar(newnames)-1);
    returndata <- data.frame(name=newnames, date=newdates);
    return(head(returndata, number));
}
于 2012-01-04T06:20:01.810 回答
1

所以这里有一个使用来自 FTP 的目录列表的解决方案。这有点棘手,因为 FTP 以 linux 格式提供带有时间戳或年份的日期。除此之外,它确实是它的工作。不过,我仍然不相信这是可靠的。如果将包复制到另一台服务器,则所有时间图都可能会被重置。

recent.packages.ftp <- function(){
    setwd(tempdir())
    download.file("ftp://cran.r-project.org/pub/R/src/contrib/", destfile=tempfile(), method="wget", extra="--no-htmlify");

    #because of --no-htmlify the destfile argument does not work
    datastring <- readLines(".listing");
    unlink(".listing");

    myexpr1 <- "(?<date>[A-Z][a-z]{2} [0-9]{2} [0-9]{2}:[0-9]{2}) (?<name>[a-zA-Z0-9\\.]{2,})_(?<version>[0-9\\.-]*).tar.gz$"
    matches <- gregexpr(myexpr1, datastring, perl=TRUE);
    packagelines <- as.logical(sapply(regmatches(datastring, matches), length));

    #subset proper lines
    matches <- matches[packagelines];
    datastring <- datastring[packagelines];
    N <- length(matches)

    #from the ?regexpr manual       
    parse.one <- function(res, result) {
        m <- do.call(rbind, lapply(seq_along(res), function(i) {
            if(result[i] == -1) return("")
            st <- attr(result, "capture.start")[i, ]
            substring(res[i], st, st + attr(result, "capture.length")[i, ] - 1)
        }))
        colnames(m) <- attr(result, "capture.names")
        m
    }

    #parse all records
    mydf <- data.frame(date=rep(NA, N), name=rep(NA, N), version=rep(NA,N))
    for(i in 1:N){
        mydf[i,] <- parse.one(datastring[i], matches[[i]]);
    }
    row.names(mydf) <- NULL;
    #convert dates
    mydf$date <- strptime(mydf$date, format="%b %d %H:%M");

    #So linux only displays dates for packages of less then six months old. 
    #However strptime will assume the current year for packages that don't have a timestamp
    #Therefore for dates that are in the future, we subtract a year. We can use some margin for timezones. 
    infuture <- (mydf$date > Sys.time() + 31*24*60*60);
    mydf$date[infuture] <- mydf$date[infuture] - 365*24*60*60;

    #sort and return
    mydf <- mydf[order(mydf$date),];
    row.names(mydf) <- NULL;
    return(mydf);
}
于 2012-01-04T23:55:48.390 回答
0

您可以处理 page http://cran.r-project.org/src/contrib/,并用空格分割字段以获得完全指定的包源文件名,其中包括版本 # 和 .gz 后缀。

列表中还有一些其他项目不是包文件,例如 .rds 文件、各种子目录等。

除非目录结构的呈现方式或文件的位置发生变化,否则我想不出比这更权威的了。

于 2012-01-04T23:26:23.303 回答