-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from jhudsl/cansavvy/borrow-chapters
Borrow chapters function
- Loading branch information
Showing
21 changed files
with
283 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
Package: cow | ||
Title: Helps Manage Git Course Repos | ||
Title: Course Organizer and Wrangler | ||
Version: 0.0.0.9000 | ||
Authors@R: | ||
person(given = "Candace", | ||
family = "Savonen", | ||
role = c("aut", "cre"), | ||
email = "[email protected]") | ||
Description: Accesses GitHub API from R and performs some course | ||
management functions, including retrieving chapter names, learning objectives, | ||
Description: Performs some course management functions through interactions with | ||
GitHub API, including retrieving chapter names, learning objectives, | ||
and keywords for courses hosted on GitHub repositories. | ||
License: GPL-3 | ||
Imports: | ||
|
@@ -24,6 +24,8 @@ Imports: | |
textrank, | ||
udpipe, | ||
xml2, | ||
knitr, | ||
igraph, | ||
Encoding: UTF-8 | ||
LazyData: true | ||
Roxygen: list(markdown = TRUE) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#' Borrow/link a chapter from another bookdown course | ||
#' | ||
#' @param doc_path A file path of markdown or R Markdown | ||
#' document of the chapter in the repository you are retrieving it from that | ||
#' you would like to include in the current document. e.g "docs/intro.md" or "intro.md" | ||
#' @param repo_name A character vector indicating the repo name of where you are | ||
#' borrowing from. e.g. "jhudsl/DaSL_Course_Template_Bookdown/". | ||
#' For a Wiki of a repo, use "wiki/jhudsl/DaSL_Course_Template_Bookdown/" | ||
#' If nothing is provided, will look for local file. | ||
#' @param branch Default is to pull from main branch, but need to declare if other branch is needed. | ||
#' @param git_pat A personal access token from GitHub. Only necessary if the | ||
#' repository being checked is a private repository. | ||
#' @param base_url it's assumed this is coming from github so it is by default 'https://raw.githubusercontent.com/' | ||
#' @param dest_dir A file path where the file should be stored upon arrival to | ||
#' the current repository. | ||
#' | ||
#' @return An Rmarkdown or markdown is knitted into the document from another repository | ||
#' | ||
#' @importFrom knitr knit_child | ||
#' @importFrom utils download.file | ||
#' @export | ||
#' | ||
#' @examples \dontrun{ | ||
#' | ||
#' # In an Rmarkdown document: | ||
#' | ||
#' # For a file in another repository: | ||
#' # ```{r, echo=FALSE, results='asis'} | ||
#' borrow_chapter( | ||
#' doc_path = "docs/02-chapter_of_course.md", | ||
#' repo_name = "jhudsl/DaSL_Course_Template_Bookdown" | ||
#' ) | ||
#' # ``` | ||
#' | ||
#' # For a local file: | ||
#' # ```{r, echo=FALSE, results='asis'} | ||
#' borrow_chapter(doc_path = "02-chapter_of_course.Rmd") | ||
#' # ``` | ||
#' } | ||
borrow_chapter <- function(doc_path, | ||
repo_name = NULL, | ||
branch = "main", | ||
git_pat = NULL, | ||
base_url = "https://raw.githubusercontent.com", | ||
dest_dir = file.path("resources", "other_chapters")) { | ||
|
||
|
||
# Declare file names | ||
doc_path <- file.path(doc_path) | ||
doc_name <- basename(doc_path) | ||
|
||
if (is.null(repo_name)) { | ||
exists <- check_git_repo( | ||
repo_name = repo_name, | ||
git_pat = git_pat, | ||
verbose = FALSE, | ||
silent = TRUE | ||
) | ||
|
||
# Create folder if it doesn't exist | ||
if (!dir.exists(dest_dir)) { | ||
dir.create(dest_dir, recursive = TRUE) | ||
} | ||
|
||
dest_file <- file.path(dest_dir, doc_name) | ||
|
||
full_url <- file.path(base_url, repo_name, branch, doc_path) | ||
|
||
# Progress message | ||
message(full_url) | ||
|
||
# Download it | ||
download.file(full_url, destfile = dest_file) | ||
} else { | ||
# If the file is local we don't need to download anything | ||
dest_file <- doc_path | ||
} | ||
|
||
# Knit it in | ||
result <- knitr::knit_child(dest_file, quiet = TRUE) | ||
cat(result, sep = "\n") | ||
} |
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 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
Oops, something went wrong.