Yes, you're right. That is only valid for loaded packages.
This should be more in line with what you wish (obviously it does nothing to RStudio ..
, but I thought you were also interested in a "script" solution):
library(utils, quietly = T)
library(data.table)
#> Warning: package 'data.table' was built under R version 3.4.2
suppressPackageStartupMessages(library(dplyr, quietly = T))
#> Warning: package 'dplyr' was built under R version 3.4.2
get_pkginfo <- function(pkg){
description <- utils::packageDescription(pkg)
# get date
if ("Date/Publication" %in% names(description)) {
date <- as.Date(description$"Date/Publication")
} else {
if ("Date" %in% names(description)) {
date <- as.Date(description$Date)
} else {
if ("Built" %in% names(description)) {
date <- as.Date(stringr::str_split_fixed(description$Date, ";", 3)[2])
}
}
}
repo <- description$Repository
# return info
data.frame(Name = pkg,
Version = description$Version,
Repo = ifelse(!is.null(repo), repo, NA),
date = date)
}
# get list of installed packages
pkgs <- as.character(installed.packages()[,1])
# get some info
pkginfo <- lapply(pkgs, FUN = function(x) get_pkginfo(x)) %>%
data.table::rbindlist() %>%
arrange(desc(date))
head(pkginfo)
#> Name Version Repo date
#> 1 purrr 0.2.4 CRAN 2017-10-18
#> 2 RcppArmadillo 0.8.100.1.0 CRAN 2017-10-11
#> 3 tidyselect 0.2.2 CRAN 2017-10-10
#> 4 data.table 1.10.4-1 CRAN 2017-10-09
#> 5 rgdal 1.2-13 CRAN 2017-10-07
#> 6 curl 3.0 CRAN 2017-10-06
summary(pkginfo)
#> Name Version Repo date
#> boot : 2 3.4.1 : 15 CRAN:246 Min. :2011-04-13
#> foreign : 2 1.0.0 : 12 NA's: 67 1st Qu.:2016-08-05
#> Matrix : 2 0.0.0.9000: 10 Median :2017-03-29
#> mgcv : 2 0.2.0 : 9 Mean :2016-11-03
#> abind : 1 0.1.0 : 7 3rd Qu.:2017-07-23
#> addinmanager: 1 1.1.1 : 7 Max. :2017-10-18
#> (Other) :303 (Other) :253 NA's :59
Since in the description sometimes the "Date/Publication" field is not available, it tries also with the "Date" and the "Built" fields, in succession.
Still have 59 packages without dates, and double entries for four packages which I can't explain, but could be a good starting point.
HTH.