Skip to content

Commit ed66cb0

Browse files
authored
Merge pull request #121 from jfisher-usgs/master
merge with upstream
2 parents fce8da9 + e45ff71 commit ed66cb0

12 files changed

Lines changed: 133 additions & 47 deletions

File tree

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
fix parsing bug on identifying code blocks; tidy main header for each help topic;
1111
add `internal` argument, used to print help pages flagged with keyword `internal`;
1212
replace `hr` argument with `sep` argument;
13-
add `replace_title` argument, used to replace the help-topic "title" with its "name".
13+
add `title_to_name` argument, used to replace the help-topic "title" with its "name";
14+
add `notrun` argument, used to remove `## Not run` comments;
15+
include internal links for package by default.
1416

1517
- In `Grid2Polygons`, add `check_validity` argument, used to check the validity of polygons.
1618

R/ExtractAlongTransect.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@
5959
#' xlim = xlim, ylim = ylim, type = "n")
6060
#' cols <- GetColors(length(segs), scheme = "bright")
6161
#' for (i in seq_along(segs))
62-
#' lines(segs[[i]]@data[, c("dist", "value")], col = cols[i])
62+
#' lines(segs[[i]]@data[, c("dist", "value")],
63+
#' col = cols[i], lwd = 2)
6364
#' coords <- sp::coordinates(transect)
6465
#' n <- length(transect)
6566
#' d <- cumsum(c(0, as.matrix(dist((coords)))[cbind(1:(n - 1), 2:n)]))
66-
#' abline(v = d, col = "green", lty = 2)
67+
#' abline(v = d, lty = 2)
6768
#' mtext(sprintf("(%d, %d)", coords[1, 1], coords[1, 2]),
6869
#' line = -1, adj = 0, cex = 0.7)
6970
#' mtext(sprintf("(%d, %d)", coords[n, 1], coords[n, 2]),

R/PrintPackageHelp.R

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#' Print Package Help Documentation
22
#'
3-
#' Print the HTML code associated with the help documentation of one or more add-on packages.
3+
#' Print the HTML code associated with the help documentation of one or more \R packages.
44
#'
55
#' @param pkg 'character' vector.
66
#' Package name(s)
77
#' @param file 'connection' or 'character' string.
88
#' Names the file to append output to.
99
#' Prints to the standard output connection by default.
1010
#' @param internal 'logical' flag.
11-
#' Whether to print help topics flagged with the keyword "internal".
11+
#' Whether to print help topics flagged with the keyword \dQuote{internal}.
1212
#' @param toc 'logical' flag.
1313
#' Whether to format level-2 headers (help-topic titles) using a Markdown syntax.
1414
#' This is required when specifying the table-of-contents (toc) format option in R Markdown,
1515
#' see \code{\link[rmarkdown:render]{rmarkdown::render}} function for details.
16-
#' @param replace_title 'logical' flag.
16+
#' @param title_to_name 'logical' flag.
1717
#' Whether to replace the help-topic \dQuote{title} with its \dQuote{name}.
1818
#' @param sep 'character' string.
1919
#' HTML to separate help topics, a horizontal line by default.
20-
#' @param links 'character' vector (experimental).
21-
#' Names of packages searched when creating internal hyperlinks to help topics.
20+
#' @param notrun 'logical' flag.
21+
#' Whether to inlcude \code{## Not run} comments in the Examples section of help documentation.
22+
#' @param links 'character' vector.
23+
#' Names of packages searched (level 0) when creating internal hyperlinks to functions and datasets.
2224
#' @param ...
2325
#' Not used
2426
#'
@@ -41,7 +43,8 @@
4143
#' "---",
4244
#' sep = "\n", file = "help-example.Rmd")
4345
#' PrintPackageHelp("inlmisc", file = "help-example.Rmd",
44-
#' toc = TRUE, replace_title = TRUE)
46+
#' toc = TRUE, title_to_name = TRUE,
47+
#' notrun = FALSE)
4548
#' rmarkdown::render("help-example.Rmd")
4649
#' url <- file.path("file:/", getwd(), "help-example.html")
4750
#' utils::browseURL(url)
@@ -50,14 +53,15 @@
5053
#' }
5154
#'
5255

53-
PrintPackageHelp <- function(pkg, file="", internal=FALSE,
54-
toc=FALSE, replace_title=FALSE,
55-
sep="<hr>", links=NULL, ...) {
56+
PrintPackageHelp <- function(pkg, file="", internal=FALSE, toc=FALSE,
57+
title_to_name=FALSE, notrun=TRUE, sep="<hr>",
58+
links=pkg, ...) {
5659

5760
checkmate::assertCharacter(pkg, unique=TRUE)
5861
checkmate::assertFlag(internal)
5962
checkmate::assertFlag(toc)
60-
checkmate::assertFlag(replace_title)
63+
checkmate::assertFlag(title_to_name)
64+
checkmate::assertFlag(notrun)
6165
checkmate::assertString(sep, null.ok=TRUE)
6266
checkmate::assertCharacter(links, unique=TRUE, null.ok=TRUE)
6367

@@ -88,42 +92,65 @@ PrintPackageHelp <- function(pkg, file="", internal=FALSE,
8892
# identify links
8993
if (!is.null(links)) {
9094
d <- .GetHelpMeta(links)
91-
links <- paste0("#", d$name)
95+
links <- paste0("#", tolower(gsub(" ", "-", d$name)))
9296
names(links) <- d$name
9397
}
9498

9599
# loop through help items
96100
for (i in seq_along(rd)) {
97101

98102
# convert rd to html
99-
htm <- utils::capture.output(tools::Rd2HTML(rd[[i]],
100-
no_links=is.null(links),
101-
Links=links,
102-
Links2=links))
103+
htm <- utils::capture.output({
104+
suppressWarnings(tools::Rd2HTML(rd[[i]],
105+
no_links=is.null(links),
106+
Links=links,
107+
Links2=links))
108+
})
103109

104-
# update level-2 header (title of help documentation)
105-
idx <- pmatch("<h2>", htm)
110+
# remove broken internal links
111+
pattern <- "<a href=\\\"\\.\\./\\.\\./.*\\\">.*</a>"
112+
for (j in grep(pattern, htm)) {
113+
repeat {
114+
st <- htm[j]
115+
re <- regexpr(pattern, st)
116+
if (re == -1) break
117+
st <- substr(st, re, re + attr(re, "match") - 1L)
118+
st <- sub("<a.*\">", "", sub("</a>", "", st))
119+
htm[j] <- sub(pattern, st, htm[j])
120+
}
121+
}
122+
123+
# substitute level-2 header (title of help documentation)
124+
idx <- grep("<h2>", htm)
106125
ti <- gsub("<.*?>", "", htm[idx])
107126
nm <- names(rd)[i]
127+
id <- tolower(gsub(" ", "-", nm))
108128
if (toc) {
109-
if (replace_title)
110-
txt <- sprintf("\n## %s\n\n*%s*", nm, ti)
129+
if (title_to_name)
130+
txt <- sprintf("\n## %s {#%s}\n\n*%s*", nm, id, ti)
111131
else
112-
txt <- sprintf("\n## %s {#%s}", ti, nm)
132+
txt <- sprintf("\n## %s {#%s}", ti, id)
113133
cat(txt, file=file, sep="\n\n", append=TRUE)
114-
} else if (replace_title) {
115-
htm[idx] <- sprintf("<h2>%s</h2>\n\n<em>%s</em>\n", nm, ti)
134+
} else if (title_to_name) {
135+
htm[idx] <- sprintf("<h2 id=\"%s\">%s</h2>\n\n<em>%s</em>\n",
136+
id, nm, ti)
116137
}
117138

118139
# remove extraneous lines at beginning and end of help page
119140
htm <- htm[-c(seq_len(idx - !toc), length(htm))]
120141

142+
# remove 'not run' commented lines
143+
if (!notrun) {
144+
idx <- which(htm %in% c("## Not run: ", "## End(Not run)"))
145+
if (length(idx) > 0) htm <- htm[-idx]
146+
}
147+
121148
# edit code-chunk tags to use syntax highlighting
122149
htm_trim <- trimws(htm)
123150
htm[htm_trim == "</pre>"] <- "</code></pre>"
124151
idx <- which(htm_trim == "<pre>")
125-
htm[idx + 1L] <- sprintf("<pre class=\"lang-r\"><code class=\"lang-r\">%s",
126-
htm[idx + 1L])
152+
fmt <- "<pre class=\"lang-r\"><code class=\"lang-r\">%s"
153+
htm[idx + 1L] <- sprintf(fmt, htm[idx + 1L])
127154
htm[idx] <- ""
128155

129156
# remove empty lines everywhere but in examples section
@@ -149,11 +176,13 @@ PrintPackageHelp <- function(pkg, file="", internal=FALSE,
149176
src <- sub("..", system.file(package=meta$package[i]), src)
150177
for (f in src) checkmate::assertFileExists(f, access="r")
151178
uri <- vapply(src, function(f) knitr::image_uri(f), "")
152-
htm[is] <- sprintf("<p><img src=\"%s\" alt=\"%s\" />", uri, basename(src))
179+
fmt <- "<p><img src=\"%s\" alt=\"%s\" />"
180+
htm[is] <- sprintf(fmt, uri, basename(src))
153181
}
154182

155183
# add seperator
156-
if (!is.null(sep) && i < nrow(meta)) htm <- c(htm, sprintf("\n%s\n", sep))
184+
if (!is.null(sep) && i < nrow(meta))
185+
htm <- c(htm, sprintf("\n%s\n", sep))
157186

158187
# preserve html
159188
htm <- htmltools::htmlPreserve(htm)
@@ -194,9 +223,10 @@ PrintPackageHelp <- function(pkg, file="", internal=FALSE,
194223
stop(gettextf("invalid %s argument", sQuote("file")), domain=NA)
195224
pkgname <- basename(dirpath)
196225
rddb <- file.path(path, pkgname)
197-
if (!file.exists(paste0(rddb, ".rdx")))
198-
stop(gettextf("package %s exists but was not installed under R >= 2.10.0 so help cannot be accessed",
199-
sQuote(pkgname)), domain=NA)
226+
if (!file.exists(paste0(rddb, ".rdx"))) {
227+
fmt <- "package %s exists but was not installed under R >= 2.10.0 so help cannot be accessed"
228+
stop(gettextf(fmt, sQuote(pkgname)), domain=NA)
229+
}
200230
.FetchRdDB(rddb, basename(file))
201231
}
202232

R/PrintTable.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#' \dontrun{
8686
#' sink("table-example.tex")
8787
#' cat("\\documentclass{article}",
88+
#' "\\usepackage{geometry}",
8889
#' "\\usepackage[labelsep = period, labelfont = bf]{caption}",
8990
#' "\\usepackage{siunitx}",
9091
#' "\\sisetup{input-ignore = {,}, input-decimal-markers = {.},",
@@ -122,7 +123,7 @@
122123
#' PrintTable(d, colheadings, align)
123124
#' cat("\\end{document}\n")
124125
#' sink()
125-
#' tools::texi2pdf("table-example.tex", clean = TRUE) # requires TeX
126+
#' tinytex::pdflatex("table-example.tex") # requires TeX
126127
#' system("open table-example.pdf")
127128
#'
128129
#' file.remove("table-example.tex", "table-example.pdf")

R/usgs_article.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
#'
1616
#' @examples
1717
#' \dontrun{
18+
#' # install LaTeX distribution
19+
#' if (Sys.which("pdflatex") == "" && !tinytex:::is_tinytex())
20+
#' tinytex::install_tinytex()
21+
#'
22+
#' # install LaTeX packages
23+
#' if (tinytex:::is_tinytex())
24+
#' inlmisc:::InstallLatexPackages()
25+
#'
1826
#' rmarkdown::draft("myarticle.Rmd",
1927
#' template = "usgs_article",
2028
#' package = "inlmisc")
@@ -113,3 +121,12 @@ usgs_article <- function(...) {
113121

114122
base
115123
}
124+
125+
126+
# install required LaTeX packages into TinyTeX
127+
InstallLatexPackages <- function() {
128+
file <- system.file("misc", "latex-packages.txt", package="inlmisc")
129+
pkgs <- readLines(file)
130+
pkgs <- pkgs[!pkgs %in% tinytex::tl_pkgs()]
131+
tinytex::tlmgr_install(pkgs)
132+
}

inst/misc/latex-packages.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
biblatex
2+
enumitem
3+
fancyhdr
4+
footmisc
5+
listofitems
6+
logreq
7+
makecell
8+
ms
9+
multirow
10+
psnfss
11+
ragged2e
12+
readarray
13+
rsfs
14+
sectsty
15+
siunitx
16+
symbol
17+
tocloft
18+
verbatimbox
19+
xcolor
20+
xpatch

inst/misc/preamble.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
% Layout
19-
\usepackage[tmargin=1.00in, bmargin=0.67in, lmargin=0.58in, rmargin=0.75in]{geometry} % define margins
19+
\usepackage[tmargin=1.000in, bmargin=0.667in, lmargin=0.750in, rmargin=0.583in]{geometry} % define margins
2020
\usepackage[pdftex]{lscape} % place selected parts of a document in landscape
2121

2222

@@ -113,8 +113,8 @@
113113
\DeclareFieldFormat{title}{#1}
114114
\DeclareFieldFormat[article,thesis,report,incollection,inproceedings,misc]{title}{#1}
115115
\DeclareFieldFormat[incollection,inproceedings]{booktitle}{#1}
116-
\DeclareNameAlias{sortname}{last-first}
117-
\DeclareNameAlias{default}{last-first}
116+
\DeclareNameAlias{sortname}{family-given}
117+
\DeclareNameAlias{default}{family-given}
118118
\DefineBibliographyStrings{english}{pagetotal={p\adddot}, pagetotals={p\adddot}}
119119
\DefineBibliographyStrings{english}{references={References Cited}}
120120
\renewcommand*{\newunitpunct}{\addcomma\space}

inst/rmarkdown/templates/usgs_article/skeleton/usgs.sty

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
% Layout -----------------------------------------------------------------------
2424

25-
\RequirePackage[tmargin=1.00in, bmargin=0.67in, lmargin=0.58in, rmargin=0.75in]{geometry} % define margins
25+
\RequirePackage[tmargin=1.000in, bmargin=0.667in, lmargin=0.750in, rmargin=0.583in]{geometry} % define margins
2626
\RequirePackage[pdftex]{lscape} % place selected parts of a document in landscape
2727

2828
% Headers ----------------------------------------------------------------------

man/ExtractAlongTransect.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/PrintPackageHelp.Rd

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)