R/renv.R

Defines functions manifest_from_renv

Documented in manifest_from_renv

#' Generate a TOML manifest from a renv lockfile
#'
#' Reads a `{renv}` lockfile (JSON format) and converts it to a `manifesto`-style
#' TOML manifest. Extracts package versions and the R environment version.
#'
#' @param lockfile Path to a `renv.lock` JSON file. Defaults to `'renv.lock'`.
#' @param path Optional path to write the TOML manifest. Defaults to a temporary `.toml` file.
#' @param r_version Optional R version settings. Defaults to version found in lockfile.
#'
#' @return Path to the generated TOML file (invisibly).
#' @export
#'
#' @examples
#' path <- manifest_from_renv(system.file(package = 'manifesto', 'renv.lock'))
manifest_from_renv <- function(lockfile = 'renv.lock', path, r_version) {
  if (!file.exists(lockfile)) {
    cli::cli_abort('File {.path {lockfile}} does not exist.')
  }

  json <- jsonlite::read_json(lockfile, simplifyVector = TRUE)

  if (is.null(json$Packages)) {
    cli::cli_abort('No packages found in {.path {lockfile}}.')
  }

  deps <- stats::setNames(
    vapply(json$Packages, function(x) x$Version, FUN.VALUE = character(1)),
    names(json$Packages)
  ) |>
    as.list()

  if (missing(r_version)) {
    r_version <- json$R$Version %||% current_r_version()
  }

  if (missing(path)) {
    path <- tempfile(fileext = '.toml')
  }

  manifest_create(
    path = path,
    dependencies = deps,
    r_version = r_version
  )

  invisible(path)
}

Try the manifesto package in your browser

Any scripts or data that you put into this service are public.

manifesto documentation built on May 6, 2026, 1:06 a.m.