Skip to contents

Get Metadata for All KWB-R Github Repos

For all Github repos also includes non R packages

Get Public KWB-R Github Package

Now limit only “public” KWB-R repos which are R packages.

pkgs <- pkgmeta::get_github_packages()

Define paths

# Define paths and resolve placeholders
paths_list <- list(
  root_dir = tempdir(),
  pkglib = "<root_dir>/pkg-lib",
  pkgsource_dir = "<root_dir>/pkg-source",
  pkgnet_dir = "<root_dir>/pkgnet",
  pkgsource_zip_dir = "<pkgsource_dir>/zip",
  pkgsource_unzip_dir = "<pkgsource_dir>/unzip"
)

paths <- kwb.utils::resolve(paths_list, root_dir = tempdir())

Download dependencies

Install Packages

in own library required for “codemetar” and “pkgnet” reports

Source Packages

required for pkgnet “covr”:


fs::dir_create(
  path = c(paths$pkgsource_zip_dir, paths$pkgsource_unzip_dir), 
  recurse = TRUE
)


for (pkg in pkgs$name) {
  try(pkgmeta::download_github(
    repo = sprintf("kwb-r/%s", pkg),
    dest_dir = paths$pkgsource_zip_dir,
    use_zip = TRUE,
    auth_token = Sys.getenv("GITHUB_PAT"))
  )
}

zipfiles <- list.files(paths$pkgsource_zip_dir,full.names = TRUE)

invisible(lapply(zipfiles, function(x) {
  message(sprintf("Unzipping %s to %s", x, paths$pkgsource_unzip_dir))
  unzip(x, exdir = paths$pkgsource_unzip_dir)
}))

Perform Package Meta Analysis

With R package “codemetar”

Generating “codemetar.json” file

pkgs <-  pkgmeta::get_github_packages()

codemeta <- pkgmeta::create_pkg_codemeta(pkgs, libpath = paths$pkglib)

pkgmeta::write_pkg_codemeta_json(codemeta)

With R package “pkgnet”

Create Reports with "pkgnet

fs::dir_create(paths$pkgnet_dir, recurse = TRUE)

withr::with_libpaths(new = c(paths$pkglib, .libPaths()), 
                     code = for(pkg in pkgs$name) {
  
  print(sprintf("Write report for R package: %s", pkg ))
  
  pkg_src_unzipped <- dir(paths$pkgsource_unzip_dir, 
                          pattern = sprintf(".*%s-.*", pkg))
  
  if (length(pkg_src_unzipped) > 1) {
    
    stop(sprintf(
      "Multiple unzipped pkg sources found in %s: %s.\n%s",
      paths$pkgsource_unzip_dir,
      paste(pkg_src_unzipped, collapse = ', '),
      "Please delete the oldest one(s) manually"
    ))
    
    #pkg_path <- file.path(paths$pkgsource_unzip_dir, pkg_src_unzipped),
    #sapply(pkg_path, function(x) {max(fs::dir_info(pkg_path[x])$modification_time)})
  } 
  
  if (length(pkg_src_unzipped) == 0L) {
    stop(sprintf(
      "No unzipped pkg sources found in %s", paths$pkgsource_unzip_dir
    ))
  }
  
  try(pkgnet::CreatePackageReport(
    pkg_name = pkg ,
    pkg_path = file.path(paths$pkgsource_unzip_dir, pkg_src_unzipped),
    report_path = file.path(paths$pkgnet_dir, paste0(pkg, ".html"))
  ))
})