Skip to content

Commit

Permalink
improve defaults for swirl2html and course2html
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarchedi committed Aug 11, 2014
1 parent 2ec48cb commit c6cb125
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 21 deletions.
51 changes: 37 additions & 14 deletions R/swirl2html.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,27 @@ makemd.script <- function(unit) {
#' Default is \code{FALSE}.
#' @param keep_rmd should the Rmd file be kept after the html is
#' is produced? Default is \code{FALSE}.
#' @param quiet should the rmd rendering output be silenced? Default
#' is \code{FALSE}.
#' @param install_course This is for internal use only. Should the course
#' be installed? Default is \code{TRUE}.
#'
#' @importFrom yaml yaml.load_file
#' @export
swirl2html <- function(dest_dir = NULL, open_html = FALSE, keep_rmd = FALSE) {
swirl2html <- function(dest_dir = NULL, open_html = FALSE,
keep_rmd = FALSE, quiet = FALSE,
install_course = TRUE) {
if(!is.logical(open_html)) {
stop("Argument 'open_html' must be TRUE or FALSE!")
}
if(!is.logical(keep_rmd)) {
stop("Argument keep_rmd must be TRUE or FALSE!")
stop("Argument 'keep_rmd' must be TRUE or FALSE!")
}
if(!is.logical(quiet)) {
stop("Argument 'quiet' must be TRUE or FALSE!")
}
if(!is.logical(install_course)) {
stop("Argument 'install_course' must be TRUE or FALSE!")
}
if(!require(rmarkdown)) {
stop("You must install the rmarkdown package to use this feature!")
Expand All @@ -102,7 +117,7 @@ swirl2html <- function(dest_dir = NULL, open_html = FALSE, keep_rmd = FALSE) {
# Expand path
dest_dir <- normalizePath(dest_dir)
# Install course
install_course_directory(course_dir)
if(install_course) install_course_directory(course_dir)
# Set path to lesson file
lessonPath <- getOption('swirlify_lesson_file_path')
# Set rmd file name
Expand Down Expand Up @@ -140,14 +155,16 @@ swirl2html <- function(dest_dir = NULL, open_html = FALSE, keep_rmd = FALSE) {
# message("Opening R Markdown file...")
# file.edit(destrmd)
message("Knitting html...")
rmarkdown::render(destrmd)
rmarkdown::render(destrmd, quiet = quiet)
# Path to html document
html_filename <- paste0(getOption("swirlify_lesson_dir_name"), ".html")
desthtml <- file.path(dest_dir, html_filename)
# If keep_rmd is FALSE, remove rmd file
if(!keep_rmd) file.remove(destrmd)
message("Opening html document...")
browseURL(desthtml)
if(open_html) {
message("Opening html document...")
browseURL(desthtml)
}
}

#' @rdname swirl2html
Expand All @@ -157,23 +174,29 @@ swirl2html <- function(dest_dir = NULL, open_html = FALSE, keep_rmd = FALSE) {
#' working on.
#' @export
course2html <- function(course_dir = NULL, dest_dir = NULL,
open_html = FALSE, keep_rmd = FALSE) {
open_html = FALSE, keep_rmd = FALSE,
quiet = FALSE) {
if(is.null(course_dir)) {
lesson_file_check()
course_dir <- getOption("swirlify_course_dir_path")
}
if(!file.exists(course_dir)) {
stop(course_dir, " does not exist!")
}
# Get course paths
# Install course
install_course_directory(course_dir)
# Get lesson paths
course_dir <- normalizePath(course_dir)
courses <- list.files(course_dir, full.names = TRUE)
lessons <- list.files(course_dir, full.names = TRUE)
# Remove MANIFEST if one exists
manifest_path <- file.path(course_dir, "MANIFEST")
courses <- setdiff(courses, manifest_path)
for(crs in courses) {
lesson_path <- file.path(crs, 'lesson.yaml')
set_lesson(lesson_path)
swirl2html(dest_dir = dest_dir, keep_rmd = keep_rmd)
lessons <- setdiff(lessons, manifest_path)
for(les in lessons) {
message("\nWorking on ", basename(les), "...")
lesson_path <- file.path(les, 'lesson.yaml')
set_lesson(lesson_path, open_lesson = FALSE, silent = TRUE)
swirl2html(dest_dir = dest_dir, open_html = open_html,
keep_rmd = keep_rmd, quiet = quiet,
install_course = FALSE)
}
}
23 changes: 19 additions & 4 deletions R/yaml_writer.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,28 @@ qtxt <- function(){
#'
#' @param path2yaml Optional, full path to YAML lesson file. If not
#' specified, then you will be prompted to select file interactively.
#' @param open_lesson Should the lesson be opened automatically?
#' Default is \code{TRUE}.
#' @param silent Should the lesson be set silently? Default is
#' \code{FALSE}.
#' @export
set_lesson <- function(path2yaml = NULL) {
set_lesson <- function(path2yaml = NULL, open_lesson = TRUE,
silent = FALSE) {
if(!is.logical(open_lesson)) {
stop("Argument 'open_lesson' must be logical!")
}
if(!is.logical(silent)) {
stop("Argument 'silent' must be logical!")
}
options(swirlify_lesson_file_path = NULL)
lesson_file_check(path2yaml)
message("\nThis lesson is located at ", getOption("swirlify_lesson_file_path"))
message("\nIf the lesson file doesn't open automatically, you can open it now to begin editing...\n")
file.edit(getOption("swirlify_lesson_file_path"))
if(!silent) {
message("\nThis lesson is located at ", getOption("swirlify_lesson_file_path"))
message("\nIf the lesson file doesn't open automatically, you can open it now to begin editing...\n")
}
if(open_lesson) {
file.edit(getOption("swirlify_lesson_file_path"))
}
invisible()
}

Expand Down
8 changes: 7 additions & 1 deletion man/set_lesson.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
\alias{set_lesson}
\title{Select an existing lesson you want to work on}
\usage{
set_lesson(path2yaml = NULL)
set_lesson(path2yaml = NULL, open_lesson = TRUE, silent = FALSE)
}
\arguments{
\item{path2yaml}{Optional, full path to YAML lesson file. If not
specified, then you will be prompted to select file interactively.}

\item{open_lesson}{Should the lesson be opened automatically?
Default is \code{TRUE}.}

\item{silent}{Should the lesson be set silently? Default is
\code{FALSE}.}
}
\description{
Select an existing lesson you want to work on
Expand Down
11 changes: 9 additions & 2 deletions man/swirl2html.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
\alias{swirl2html}
\title{Turn a swirl lesson into a pretty webpage}
\usage{
swirl2html(dest_dir = NULL, open_html = FALSE, keep_rmd = FALSE)
swirl2html(dest_dir = NULL, open_html = FALSE, keep_rmd = FALSE,
quiet = FALSE, install_course = TRUE)

course2html(course_dir = NULL, dest_dir = NULL, open_html = FALSE,
keep_rmd = FALSE)
keep_rmd = FALSE, quiet = FALSE)
}
\arguments{
\item{dest_dir}{destination directory (i.e. where to put the output files).
Expand All @@ -19,6 +20,12 @@ Default is \code{FALSE}.}
\item{keep_rmd}{should the Rmd file be kept after the html is
is produced? Default is \code{FALSE}.}

\item{quiet}{should the rmd rendering output be silenced? Default
is \code{FALSE}.}

\item{install_course}{This is for internal use only. Should the course
be installed? Default is \code{TRUE}.}

\item{course_dir}{path to course directory. If none is specified,
default is the course directory for the lesson you are currently
working on.}
Expand Down

0 comments on commit c6cb125

Please sign in to comment.