Skip to content

Commit

Permalink
Merge pull request #3 from dchiu911/use-ymlthis
Browse files Browse the repository at this point in the history
Use ymlthis
  • Loading branch information
dchiu911 authored Oct 5, 2019
2 parents 1b64d46 + e1a6eda commit c4383db
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 118 deletions.
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rsf
Title: Report of Statistical Findings in 'bookdown'
Version: 0.1.0.9001
Version: 0.2.0
Authors@R:
person(given = "Derek",
family = "Chiu",
Expand All @@ -17,10 +17,12 @@ URL: https://github.com/dchiu911/rsf,
BugReports: https://github.com/dchiu911/rsf/issues
Imports:
bookdown,
stringr,
knitr,
magrittr,
rlang,
usethis,
whoami,
yaml
yaml,
ymlthis
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
export(git_ignore_outputs)
export(use_references)
export(use_rsf)
importFrom(magrittr,"%>%")
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# rsf 0.1.0.9001
# rsf 0.2.0

## API changes

* Use the `ymlthis` package for writing YAML

## LaTeX changes

Expand Down
14 changes: 2 additions & 12 deletions R/git_ignore_outputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ git_ignore_outputs <- function(path = ".", figs_only = TRUE) {
output_dir <-
file.path(output_dir, paste0(bookdown_yml[["book_filename"]], "_files"))
}
outputs <- c(output_dir, "_bookdown_files")
if (all(outputs %in% git_ignore)) {
usethis::ui_done("Outputs already git ignored")
return(invisible())
}
writeLines(text = c(git_ignore, outputs), con = git_ignore_path)
usethis::ui_done(paste(
"Adding",
usethis::ui_value(outputs),
"to",
usethis::ui_path(".gitignore")
))
usethis::write_union(path = git_ignore_path,
lines = c(git_ignore, output_dir, "_bookdown_files"))
}
5 changes: 5 additions & 0 deletions R/rsf-package.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom magrittr %>%
## usethis namespace: end
NULL
60 changes: 37 additions & 23 deletions R/use_references.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,59 @@ use_references <- function(path = ".", number = 99) {
stop("One or more input files are missing.")
}

# Check if references already exists
if ("bibliography" %in% names(yaml::read_yaml(index_rmd_path))) {
usethis::ui_done("References already added")
return(invisible())
}

# Create references Rmd source file
# Specify references Rmd file path
src_path <- file.path(path, "src")
if (!dir.exists(src_path)) dir.create(src_path)
rmd_name <- paste0(stringr::str_pad(number, 2, pad = 0), "-references.Rmd")
write_rmd(src_path, "References", rmd_name)
rmd_name <- paste0(sprintf("%02d", number), "-references.Rmd")
ref_rmd_path <- file.path(src_path, rmd_name)

# Add header and generate packages.bib
header <- "`r if (knitr::is_html_output()) '# References {-}'`\n"
chunk_start <- "```{r include=FALSE}"
pkg_list <- "knitr::write_bib(c(.packages(), 'bookdown'), 'packages.bib')"
chunk_end <- "```"
rmd_path <- file.path(src_path, rmd_name)
writeLines(text = c(header, chunk_start, pkg_list, chunk_end), con = rmd_path)
usethis::ui_done(paste(usethis::ui_path(rmd_path), "added"))
if (file.exists(ref_rmd_path)) {
# Do nothing if references Rmd already added
usethis::ui_done(paste("Already added", usethis::ui_path(ref_rmd_path)))
return(invisible())
} else {
# Rename file if it exists
old_ref_rmd_path <-
list.files(src_path, "^[0-9]{2}-references\\.Rmd", full.names = TRUE)
if (length(old_ref_rmd_path) == 1) {
file.rename(old_ref_rmd_path, ref_rmd_path)
usethis::ui_done(paste(
"Renaming",
usethis::ui_path(old_ref_rmd_path),
"to",
usethis::ui_path(ref_rmd_path)
))
return(invisible())
} else {
# Write header and generate packages.bib otherwise
header <- "`r if (knitr::is_html_output()) '# References {-}'`"
chunk <- ymlthis::code_chunk(
chunk_code = knitr::write_bib(c(.packages(), "bookdown"), "packages.bib"),
chunk_args = list(include = FALSE)
)
if (!dir.exists(src_path)) dir.create(src_path)
writeLines(text = c(header, "", chunk), con = ref_rmd_path)
usethis::ui_done(paste("Writing", usethis::ui_path(ref_rmd_path)))
}
}

# Call packages.bib from index.Rmd
index_rmd <- readLines(index_rmd_path)
before_style <- grep("biblio-style", index_rmd) - 1
index_rmd <- append(index_rmd, "bibliography: packages.bib", before_style)
writeLines(text = index_rmd, con = index_rmd_path)
usethis::ui_done(paste(usethis::ui_path(index_rmd_path), "modified"))
usethis::ui_done(paste("Writing", usethis::ui_path(index_rmd_path)))

# Add bib options to bookdown::pdf_book output
output_yml <- yaml::read_yaml(output_yml_path)
bib_opts <- list(citation_package = "natbib", toc_bib = TRUE)
output_yml[["bookdown::pdf_book"]] <-
c(output_yml[["bookdown::pdf_book"]], bib_opts)
yaml::write_yaml(x = output_yml, file = output_yml_path)
usethis::ui_done(paste(usethis::ui_path(output_yml_path), "modified"))
usethis::ui_done(paste("Writing", usethis::ui_path(output_yml_path)))

# Rename Bibliography to References in tex
preamble_tex <- readLines(preamble_tex_path)
bib_rename <- "\n\\renewcommand{\\bibname}{References}"
writeLines(text = c(preamble_tex, bib_rename), con = preamble_tex_path)
usethis::ui_done(paste(usethis::ui_path(preamble_tex_path), "modified"))
bib_rename <- "\\renewcommand{\\bibname}{References}"
writeLines(text = c(preamble_tex, "", bib_rename), con = preamble_tex_path)
usethis::ui_done(paste("Writing", usethis::ui_path(preamble_tex_path)))
}
1 change: 1 addition & 0 deletions R/use_rsf.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ use_rsf <- function(path, ...) {
write_index(path)
write_src(path)
write_preamble(path)
write_gitignore(path)
}
26 changes: 14 additions & 12 deletions R/write_bookdown_yml.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# _bookdown.yml
#' Write `_bookdown.yml`
#' @noRd
write_bookdown_yml <- function(path, params) {
bookdown_yml <- c(
list(
book_filename = paste(basename(path), "RSF", sep = "_"),
rmd_subdir = "src",
before_chapter_script = "src/utils.R"
),
params
)
yaml::write_yaml(
x = bookdown_yml,
file = file.path(path, "_bookdown.yml")
bookdown_yml <- rlang::exec(
ymlthis::yml_bookdown_opts,
.yml = ymlthis::yml_empty(),
book_filename = paste(basename(path), "RSF", sep = "_"),
before_chapter_script = "src/utils.R",
rmd_subdir = "src",
!!!params
)
# TODO: quiet arg not passed
tmp <- utils::capture.output(ymlthis::use_bookdown_yml(bookdown_yml, path, quiet = TRUE))
# TODO: extra final line return
tmp_path <- gsub(".*'(.*)'.*", "\\1", tmp)
writeLines(utils::head(readLines(tmp_path), -1), tmp_path)
}
8 changes: 8 additions & 0 deletions R/write_gitignore.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#' Write `.gitignore`
#' @noRd
write_gitignore <- function(path) {
writeLines(
c(".Rproj.user", ".Rhistory", ".RData", ".Ruserdata"),
file.path(path, ".gitignore")
)
}
37 changes: 20 additions & 17 deletions R/write_index.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# index.Rmd
#' Write `index.Rmd`
#' @noRd
write_index <- function(path) {
index_yml <- list(
title = paste(basename(path), "Report of Statistical Findings", sep = ": "),
author = whoami::fullname(fallback = "Author"),
date = "`r Sys.Date()`",
site = "bookdown::bookdown_site",
documentclass = "report",
geometry = "margin=1in",
`biblio-style` = "apalike",
`link-citations` = TRUE,
colorlinks = TRUE,
lot = TRUE,
lof = TRUE
)
index_yml <- gsub("\n$", "", yaml::as.yaml(index_yml))
writeLines(text = c("---", index_yml, "---\n\n# Preface {-}"),
con = file.path(path, "index.Rmd"))
index_yml <- ymlthis::yml_empty() %>%
ymlthis::yml_title(paste(basename(path),
"Report of Statistical Findings", sep = ": ")) %>%
ymlthis::yml() %>%
ymlthis::yml_bookdown_site() %>%
ymlthis::yml_latex_opts(
documentclass = "report",
geometry = "margin=1in",
colorlinks = TRUE,
lof = TRUE,
lot = TRUE,
biblio_style = "apalike"
) %>%
ymlthis::yml_citations(link_citations = TRUE)
writeLines(
text = c(utils::capture.output(print(index_yml)), "\n# Preface {-}"),
con = file.path(path, "index.Rmd")
) # TODO: use_rmarkdown() forces index.Rmd to open, use_index_rmd doesn't pass body param
}
23 changes: 12 additions & 11 deletions R/write_output_yml.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# _output.yml
#' Write `_output.yml`
#' @noRd
write_output_yml <- function(path) {
output_yml <- list(
`bookdown::gitbook` = list(
output_yml <- ymlthis::yml_output(
.yml = ymlthis::yml_empty(),
bookdown::gitbook(
lib_dir = "assets",
split_by = "chapter+number"
),
`bookdown::pdf_book` = list(
bookdown::pdf_book(
keep_tex = TRUE,
includes = list(
in_header = "preamble.tex"
)
includes = ymlthis::includes2(in_header = "preamble.tex")
)
)
yaml::write_yaml(
x = output_yml,
file = file.path(path, "_output.yml")
)
# TODO: quiet arg not passed
tmp <- utils::capture.output(ymlthis::use_output_yml(output_yml, path, quiet = TRUE))
# TODO: extra final line return
tmp_path <- gsub(".*'(.*)'.*", "\\1", tmp)
writeLines(utils::head(readLines(tmp_path), -1), tmp_path)
}
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ environment:
# env vars that may need to be set, at least temporarily, from time to time
# see https://github.com/krlmlr/r-appveyor#readme for details
# USE_RTOOLS: true
# R_REMOTES_STANDALONE: true
R_REMOTES_STANDALONE: true

# Adapt as necessary starting from here

Expand Down
24 changes: 4 additions & 20 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
## Resubmission

This is a resubmission. In this version I have:

* Renamed installed file '.gitignore to 'gitignore' so it can be found in examples
* Removed package 'here' from Imports
* Used files from inst/extdata for examples
* Improved documentation for use_rsf()
* Ensured examples are executable by adding conditional checks
* Reset the working directory at the end of examples
* Wrapped 'bookdown' and 'YAML' in Description
* Replaced dontrun{} with donttest{} in examples
* Written to tempdir() in examples

## Test environments
* local OS X install, R 3.5.3
* ubuntu 14.04 (on travis-ci), R 3.5.3
* win-builder (devel and release)
* local macOS install, R 3.5.3
* ubuntu 16.04 (on travis-ci), R-devel, R 3.6.1
* win-builder (devel)

## R CMD check results

0 errors | 0 warnings | 1 note

* This is a new release.
0 errors | 0 warnings | 0 notes
4 changes: 2 additions & 2 deletions inst/extdata/_bookdown.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
book_filename: RSF
output_dir: reports
rmd_subdir: src
before_chapter_script: src/utils.R
rmd_subdir: src
output_dir: reports
2 changes: 1 addition & 1 deletion inst/extdata/_output.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ bookdown::gitbook:
lib_dir: assets
split_by: chapter+number
bookdown::pdf_book:
keep_tex: yes
keep_tex: true
includes:
in_header: preamble.tex
11 changes: 6 additions & 5 deletions inst/extdata/index.Rmd
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
title: 'Report of Statistical Findings'
author: Derek Chiu
date: '`r Sys.Date()`'
date: '`r format(Sys.Date())`'
site: bookdown::bookdown_site
documentclass: report
geometry: margin=1in
colorlinks: true
lof: true
lot: true
biblio-style: apalike
link-citations: yes
colorlinks: yes
lot: yes
lof: yes
link-citations: true
---

# Preface {-}
11 changes: 2 additions & 9 deletions inst/extdata/preamble.tex
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
\usepackage{booktabs}
\usepackage{pdflscape}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}
\usepackage{float}
\floatplacement{figure}{H}
\floatstyle{ruled}
\restylefloat{figure}

\let\paragraph\oldparagraph
\let\subparagraph\oldsubparagraph

Expand All @@ -23,3 +14,5 @@
{}
{0pt}
{}

\titlespacing*{\chapter}{0pt}{0pt}{40pt}

0 comments on commit c4383db

Please sign in to comment.