ThinkR-open / attachment

Tools to deal with dependencies in scripts, Rmd and packages
https://thinkr-open.github.io/attachment/
Other
108 stars 13 forks source link

As user, I'd like attachment to be able to read deps from 'examples' and add them to "suggests" #103

Closed statnmap closed 1 month ago

statnmap commented 1 year ago

Validation

Run :

  tmpdir <- tempfile("dummysuggestexamples")
  dir.create(tmpdir)
  file.copy(system.file("dummypackage",package = "attachment"), tmpdir, recursive = TRUE)
  dummypackage <- file.path(tmpdir, "dummypackage")

attachment::att_amend_desc(path = dummypackage, check_if_suggests_is_installed = FALSE)
readLines(file.path(dummypackage, "DESCRIPTION"))

{utils} is used in example of my_mean.R : => {utils} has added in Suggest

image

Tech

# Get examples from R files
#' @export
get_from_examples <- function(dir.r = "R") {
  rfiles <- list.files(dir.r, full.names = TRUE)

  roxy_file <- tempfile("roxy.examples", fileext = ".R")

  all_examples <- unlist(lapply(rfiles, function(the_file) {
     file_roxytags <- roxygen2::parse_file(the_file)
    res <- unlist(
      lapply(file_roxytags, 
             function(x) roxygen2::block_get_tag_value(block = x, tag = "examples"))
    )
    res
  }))
  # Clean \dontrun and \donttest, and replace with '{' on next line
  all_examples_clean <- 
    gsub(pattern = "\\\\dontrun\\s*\\{|\\\\donttest\\s*\\{", replacement = "#ICI\n{", x = all_examples)
  cat(all_examples_clean, file = roxy_file, sep = "\n")
  all_deps_examples <- attachment::att_from_rscript(roxy_file)
  file.remove(roxy_file)
  return(all_deps_examples)
}

# Add this in att_amend_desc()
if (dir.r != "") {
  get_from_examples(dir.r = dir.r)
}
MurielleDelmotte commented 1 month ago

Validate