#' List dependencies of a file
#'
#' `deps()` returns a character vector of the direct dependencies for the
#' specified R or Rmd file.
#'
#' @param filepath File location.
#'
#' @returns Character vector of dependencies.
#'
#' @export
deps <- function(filepath) {
if(grepl("\\.[Rr]$", filepath, perl = TRUE)) {
rdeps(filepath)
} else if (grepl("\\.[Rr]md$", filepath, perl = TRUE)) {
rmddeps(filepath)
} else {
stop(".deps only works for files with extensions .Rmd, .rmd, .R and .r")
}
}
# find direct dependencies of rscript
rdeps <- function(filepath) {
dat <- paste(as.character(parse(filepath)), collapse = "\n")
colon_string <- r"---{([a-zA-Z][\w.]*)(?=:{2,3})}---"
colon_greg <- gregexpr(colon_string, dat, perl = TRUE)
colon_deps <- unlist(regmatches(dat, colon_greg), use.names = FALSE)
lib_string <- r"{(?<=library\(|require\()([a-zA-Z][\w.]*)}"
lib_greg <- gregexpr(lib_string, dat, perl = TRUE)
lib_deps <- unlist(regmatches(dat, lib_greg), use.names = FALSE)
unique(c(lib_deps, colon_deps))
}
# find direct dependencies of Rmd file.
rmddeps <- function(filepath) {
op <- options(knitr.purl.inline = TRUE)
on.exit(options(op))
f <- tempfile()
on.exit(unlink(f), add = TRUE)
knitr::purl(input = filepath, output = f, quiet = TRUE, documentation = 0)
c("rmarkdown", rdeps(f))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.