-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement tutorial_package_dependencies() (#329)
* implement tutorial_dependencies() * add unit test * rename file * Move files into one place and unify renv call to a single function * allow for tutorial_package_dependencies to work like available_packages * document available tutorials. Add new column package_dependencies. Have tutorial_package_dependencies call available_tutorials if needed. Skip tests if learnr isn't installed * return NULL for test and older R versions * Correct the news item and add the PR * Remove unneeded skip check * Trim white space * Reflow tutorial_package_dependencies() docs. Fix incorrect word * Preempt if there are no deps found. R <= 3.4 can not `sort(NULL)` Co-authored-by: Barret Schloerke <[email protected]>
- Loading branch information
1 parent
6620c67
commit 89c1d14
Showing
9 changed files
with
120 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
get_needed_pkgs <- function(dir) { | ||
|
||
pkgs <- tutorial_dir_package_dependencies(dir) | ||
|
||
pkgs[!pkgs %in% utils::installed.packages()] | ||
} | ||
|
||
format_needed_pkgs <- function(needed_pkgs) { | ||
paste(" -", needed_pkgs, collapse = "\n") | ||
} | ||
|
||
ask_pkgs_install <- function(needed_pkgs) { | ||
question <- sprintf("Would you like to install the following packages?\n%s", | ||
format_needed_pkgs(needed_pkgs)) | ||
|
||
utils::menu(choices = c("yes", "no"), | ||
title = question) | ||
} | ||
|
||
install_tutorial_dependencies <- function(dir) { | ||
needed_pkgs <- get_needed_pkgs(dir) | ||
|
||
if(length(needed_pkgs) == 0) { | ||
return(invisible(NULL)) | ||
} | ||
|
||
if(!interactive()) { | ||
stop("The following packages need to be installed:\n", | ||
format_needed_pkgs(needed_pkgs)) | ||
} | ||
|
||
answer <- ask_pkgs_install(needed_pkgs) | ||
|
||
if(answer == 2) { | ||
stop("The tutorial is missing required packages and cannot be rendered.") | ||
} | ||
|
||
utils::install.packages(needed_pkgs) | ||
} | ||
|
||
|
||
|
||
|
||
#' List Tutorial Dependencies | ||
#' | ||
#' List the \R packages required to run a particular tutorial. | ||
#' | ||
#' @param name The tutorial name. If \code{name} is \code{NULL}, then all | ||
#' tutorials within \code{package} will be searched. | ||
#' @param package The \R package providing the tutorial. If \code{package} is | ||
#' \code{NULL}, then all tutorials will be searched. | ||
#' | ||
#' @export | ||
#' @return A character vector of package names that are required for execution. | ||
#' @examples | ||
#' tutorial_package_dependencies(package = "learnr") | ||
tutorial_package_dependencies <- function(name = NULL, package = NULL) { | ||
if (identical(name, NULL)) { | ||
info <- available_tutorials(package = package) | ||
return( | ||
sort(unique(unlist(info$package_dependencies))) | ||
) | ||
} | ||
|
||
tutorial_dir_package_dependencies( | ||
get_tutorial_path(name = name, package = package) | ||
) | ||
} | ||
|
||
tutorial_dir_package_dependencies <- function(dir) { | ||
# enumerate tutorial package dependencies | ||
deps <- renv::dependencies(dir, quiet = TRUE) | ||
|
||
# R <= 3.4 can not sort(NULL) | ||
# if no deps are found, renv::dependencies returns NULL | ||
if (is.null(deps)) { | ||
return(NULL) | ||
} | ||
|
||
sort(unique(deps$Package)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters