Skip to content

Commit

Permalink
More standardization.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaneplusplus committed Jan 4, 2021
1 parent 450ed73 commit 18f61fd
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ importFrom(checkmate,check_class)
importFrom(checkmate,check_list)
importFrom(checkmate,check_null)
importFrom(checkmate,check_numeric)
importFrom(fs,path_abs)
importFrom(fs,path_rel)
importFrom(rmarkdown,render)
importFrom(rmarkdown,render_site)
Expand Down
5 changes: 3 additions & 2 deletions R/build-site.r
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ recycle <- function(ind, len) {
#' @param ... options to send to the rmarkdown::render() function.
#' @importFrom checkmate assert check_class
#' @importFrom rmarkdown render
#' @importFrom fs path_rel
#' @importFrom fs path_rel path_abs
#' @export
ld_create_doc <-
function(
Expand Down Expand Up @@ -359,7 +359,8 @@ ld_create_doc <-
cc_file_name, " will be used.")
}
data_path <- file.path(rmd_dir, data_dir, cc_file_name)
saveRDS(ldb$cc, data_path)
make_dirs_as_needed(dirname(path_abs(data_path)))
saveRDS(ldb$cc, path_abs(data_path))
ldb$ld$load_cc_expr <-
create_load_cc_expr(
paste0('readRDS("', file.path(data_dir, cc_file_name), '")'))
Expand Down
39 changes: 35 additions & 4 deletions R/chunk-writer.r
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ print.listdown <- function(x, ...) {
#' output.
#' @seealso \code{\link{listdown}}
#' @export
ld_make_chunks <- function(ld) {
ld_make_chunks <- function(ld, rmd_dir) {
UseMethod("ld_make_chunks", ld)
}


ld_make_chunks.default <- function(ld) {
ld_make_chunks.default <- function(ld, rmd_dir) {
stop("Don't know how to render an object of class ",
paste(class(ld), collapse = ":"), ".", sep = "")
}
Expand All @@ -230,15 +230,46 @@ expr_to_string <- function(expr) {
}

#' @export
ld_make_chunks.listdown <- function(ld) {
ld_make_chunks.listdown <- function(ld, rmd_dir = ".") {
if (is.null(ld$load_cc_expr)) {
stop("The load_cc_expr needs to be specified. ",
"Use `create_load_cc_expr()` to set it.")
}

wd <- getwd()
make_dirs_as_needed(path_abs(rmd_dir))
cc_list <- tryCatch(
{
setwd(rmd_dir)
ret <- eval(ld$load_cc_expr)
setwd(wd)
ret
},
error = function(e) {
setwd(wd)
stop("Can't evaluate ", expr_to_string(ld$load_cc_expr),
" from directory ", rmd_dir)
})

if (is.character(cc_list)) {
cc_list <- tryCatch(
{
setwd(rmd_dir)
ret <- eval(parse(text = cc_list))
setwd(wd)
ret
},
error = function(e) {
setwd(wd)
stop("Can't evaluate", ld$load_cc_expr, "from directory", rmd_dir)
})
cc_list <- eval(parse(text = cc_list))
}

if (inherits(ld$load_cc_expr, "call")) {
cc_list <- expr_to_string(ld$load_cc_expr)
} else {
stop("The load_cc_exp element should be a call object.")
stop("The load_cc_expr element should be a call object.")
}
ret_string <- ""
if (length(ld$setup_expr)) {
Expand Down
2 changes: 1 addition & 1 deletion R/ld-write-file.r
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ld_write_file <- function(rmd_header, ld, file_name) {
}

if (inherits(ld, "listdown")) {
ld <- ld_make_chunks(ld)
ld <- ld_make_chunks(ld, rmd_dir = dirname(file_name))
}

writeLines(c(rmd_header, ld), file_name)
Expand Down
2 changes: 1 addition & 1 deletion man/ld_make_chunks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-create-html-site.r
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ site_yaml <-
site_path <- ld_build_html_site(
pages,
site_yaml,
view = FALSE,
view = TRUE,
quiet = TRUE)

test_that("Site can be built.", {
Expand Down

0 comments on commit 18f61fd

Please sign in to comment.