Skip to content

Commit

Permalink
Merge branch 'master' into error-formatting-enhance
Browse files Browse the repository at this point in the history
* master:
  make negative times 0
  replace highr:::spaces() with strrep() in base R
  support chunk options message/warning = NA
  close #2204: drop the support for cairoDevice
  amend 1a0f2cc: make line_count() a few times faster
  Replace several stringr-based function calls with base equivalents (#2202, #1549)
  see if this fix the ruby gems issue https://github.com/yihui/knitr/actions/runs/3682745993/jobs/6230638404
  stop importing xfun::isFALSE() and define isFALSE() only for R < 3.5.0
  Add labels to the default progress bar and allow users to provide a custom progress bar (#2196)
  • Loading branch information
rich-iannone committed Dec 22, 2022
2 parents 582a5fd + e66c1d8 commit b3a8365
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/knitr-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ jobs:
if: runner.os == 'Linux'
run: |
sudo apt-get install -y libpoppler-cpp-dev graphviz asymptote highlight coffeescript scala optipng ghostscript
# downgrade ruby to remove deprecated
# https://github.com/rubygems/rubygems/issues/3068
sudo gem update --system 3.0.8
- name: Install CTAN dependencies
run: |
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.41.1
Version: 1.41.5
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down Expand Up @@ -196,4 +196,4 @@ Collate:
'utils-upload.R'
'utils-vignettes.R'
'zzz.R'
RoxygenNote: 7.2.2
RoxygenNote: 7.2.3
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ import(utils)
importFrom(xfun,attr)
importFrom(xfun,file_ext)
importFrom(xfun,file_string)
importFrom(xfun,isFALSE)
importFrom(xfun,is_R_CMD_check)
importFrom(xfun,is_abs_path)
importFrom(xfun,is_windows)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# CHANGES IN knitr VERSION 1.42

## NEW FEATURES

- Users can specify a custom progress bar for `knit()` now. The default is still a text progress bar created from `utils::txtProgressBar()`. To specify a custom progress bar, set `options(knitr.progress.fun = function(total, labels) {})`. This function should take arguments `total` (the total number of chunks) and `labels` (the vector of chunk labels), create a progress bar, and return a list of two methods: `list(update = function(i) {}, done = function() {})`. The `update()` method takes `i` (index of the current chunk) as the input and updates the progress bar. The `done()` method closes the progress bar. See https://yihui.org/knitr/options/#global-r-options for documentation and examples.

- The default text progress bar is still written to `stdout()` by default, but can also be written to other connections or `stderr()` now. To do so, set `options(knitr.progress.output = )` to a connection or `stderr()`.

## MAJOR CHANGES

- `knit()` no longer prints out chunk options beneath the text progress bar while knitting a document (thanks, @hadley, #1880).

- Due to a change in the **evaluate** package, the chunk options `message = FALSE` and `warning = FALSE` will completely suppress the messages/warnings now, instead of sending them to the console. To get back to the old behavior, you can use `NA` instead of `FALSE` (thanks, @gadenbuie, https://github.com/yihui/yihui.org/discussions/1458).

## MINOR CHANGES

- If the chunk option `child` is used for a code chunk but the chunk is not empty, you will get a warning indicating that this non-empty code will not be executed when you knit the document. Now this warning can be silenced by `options(knitr.child.warning = FALSE)` (thanks, @jwijffels, #2191).

- Devices from the package **cairoDevice** (`Cairo_pdf`, `Cairo_png`, `Cairo_ps`, `Cairo_svg`) are no longer supported since the package has been archived on CRAN for more than a year (thanks, @jessekps, #2204).

# CHANGES IN knitr VERSION 1.41

## NEW FEATURES
Expand Down
8 changes: 4 additions & 4 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ eng_r = function(options) {
} else in_input_dir(
evaluate(
code, envir = env, new_device = FALSE,
keep_warning = !isFALSE(options$warning),
keep_message = !isFALSE(options$message),
keep_warning = if (is.numeric(options$warning)) TRUE else options$warning,
keep_message = if (is.numeric(options$message)) TRUE else options$message,
stop_on_error = if (is.numeric(options$error)) options$error else {
if (options$error && options$include) 0L else 2L
},
Expand Down Expand Up @@ -364,7 +364,7 @@ purge_cache = function(options) {

cache_globals = function(option, code) {
if (is.character(option)) option else {
(if (xfun::isFALSE(option)) find_symbols else find_globals)(code)
(if (isFALSE(option)) find_symbols else find_globals)(code)
}
}

Expand Down Expand Up @@ -616,7 +616,7 @@ process_tangle.inline = function(x) {
# add a label [and extra chunk options] to a code chunk
label_code = function(code, label) {
code = one_string(c('', code, ''))
paste0('## ----', stringr::str_pad(label, max(getOption('width') - 11L, 0L), 'right', '-'),
paste0('## ----', label, strrep('-', max(getOption('width') - 11L - nchar(label), 0L)),
'----', code)
}

Expand Down
2 changes: 1 addition & 1 deletion R/citation.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ write_bib = function(
bib = c(bib, unlist(bib2, recursive = FALSE))
bib = lapply(bib, function(b) {
idx = which(names(b) == '')
if (!is.null(width)) b[-idx] = stringr::str_wrap(b[-idx], width, 2, 4)
if (!is.null(width)) b[-idx] = str_wrap(b[-idx], width, 2, 4)
structure(c(b[idx[1L]], b[-idx], b[idx[2L]], ''), class = 'Bibtex')
})
if (!is.null(file) && length(x)) write_utf8(unlist(bib), file)
Expand Down
9 changes: 5 additions & 4 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ process_file = function(text, output) {
labels = unlist(lapply(groups, function(g) {
if (is.list(g$params)) g[[c('params', 'label')]] else ''
}))
pb = txtProgressBar(0, n, char = '.', style = 3)
on.exit(close(pb), add = TRUE)
pb_fun = getOption('knitr.progress.fun', txt_pb)
pb = if (is.function(pb_fun)) pb_fun(n, labels)
on.exit(if (!is.null(pb)) pb$done(), add = TRUE)
}
wd = getwd()
for (i in 1:n) {
Expand All @@ -302,7 +303,7 @@ process_file = function(text, output) {
}
break # must have called knit_exit(), so exit early
}
if (progress) setTxtProgressBar(pb, i)
if (progress && !is.null(pb)) pb$update(i)
group = groups[[i]]
res[i] = withCallingHandlers(
if (tangle) process_tangle(group) else process_group(group),
Expand Down Expand Up @@ -495,7 +496,7 @@ sew.source = function(x, options, ...) {
msg_wrap = function(message, type, options) {
# when the output format is LaTeX, do not wrap messages (let LaTeX deal with wrapping)
if (!length(grep('\n', message)) && !out_format(c('latex', 'listings', 'sweave')))
message = stringr::str_wrap(message, width = getOption('width'))
message = str_wrap(message, width = getOption('width'))
knit_log$set(setNames(
list(c(knit_log$get(type), paste0('Chunk ', options$label, ':\n ', message))),
type
Expand Down
2 changes: 1 addition & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' \code{knit} + \code{R} (while \code{Sweave} = \code{S} + \code{weave}).
#' @references Full documentation and demos: \url{https://yihui.org/knitr/};
#' FAQ's: \url{https://yihui.org/knitr/faq/}
#' @importFrom xfun attr file_ext isFALSE is_windows loadable parse_only
#' @importFrom xfun attr file_ext is_windows loadable parse_only
#' sans_ext try_silent with_ext read_utf8 write_utf8 file_string
#' is_R_CMD_check is_abs_path
NULL
Expand Down
1 change: 0 additions & 1 deletion R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ get_option_comment = function(engine) {

print.block = function(x, ...) {
params = x$params
cat(' chunk:', params$label, '\n')
if (opts_knit$get('verbose')) {
code = knit_code$get(params$label)
if (length(code) && !is_blank(code)) {
Expand Down
2 changes: 1 addition & 1 deletion R/pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ detect_pattern = function(text, ext) {
for (p in names(all_patterns)) {
for (i in c('chunk.begin', 'inline.code')) {
pat = all_patterns[[p]][[i]]
if (length(pat) && any(stringr::str_detect(text, pat))) return(p)
if (length(pat) && any(grepl(pat, text, perl = TRUE))) return(p)
}
}
# *.Rtex indicates the tex syntax in knitr, but Rnw syntax in traditional
Expand Down
11 changes: 2 additions & 9 deletions R/plot.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# graphics devices in base R, plus those in Cairo, cairoDevice, tikzDevice
# graphics devices in base R, plus those in Cairo, tikzDevice
auto_exts = c(
bmp = 'bmp', postscript = 'eps', pdf = 'pdf', png = 'png', svg = 'svg',
jpeg = 'jpeg', pictex = 'tex', tiff = 'tiff', win.metafile = 'wmf',
Expand All @@ -11,8 +11,6 @@ auto_exts = c(
CairoJPEG = 'jpeg', CairoPNG = 'png', CairoPS = 'eps', CairoPDF = 'pdf',
CairoSVG = 'svg', CairoTIFF = 'tiff',

Cairo_pdf = 'pdf', Cairo_png = 'png', Cairo_ps = 'eps', Cairo_svg = 'svg',

svglite = 'svg', gridSVG = 'svg',

ragg_png = 'png',
Expand Down Expand Up @@ -88,11 +86,6 @@ dev_get = function(dev, options = opts_current$get(), dpi = options$dpi[1]) {
CairoPDF = load_device('CairoPDF', 'Cairo'),
CairoSVG = load_device('CairoSVG', 'Cairo'),

Cairo_pdf = load_device('Cairo_pdf', 'cairoDevice'),
Cairo_png = load_device('Cairo_png', 'cairoDevice'),
Cairo_ps = load_device('Cairo_ps', 'cairoDevice'),
Cairo_svg = load_device('Cairo_svg', 'cairoDevice'),

svglite = load_device('svglite', 'svglite'),

# similar to load_device(), but the `dpi` argument is named `res`
Expand Down Expand Up @@ -217,7 +210,7 @@ get_dargs = function(dargs, dev) {
dargs
}

# this is mainly for Cairo and cairoDevice
# this is mainly for Cairo
load_device = function(name, package, dpi = NULL) {
dev = getFromNamespace(name, package)
# dpi is for bitmap devices; units must be inches!
Expand Down
4 changes: 2 additions & 2 deletions R/spin.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ spin = function(
l = attr(gregexpr('`+', x)[[1]], 'match.length')
l = max(l, 0)
if (length(l) > 0) {
i = highr:::spaces(l + 1, '`')
b = highr:::spaces(max(l + 1, 3), '`')
i = strrep('`', l + 1)
b = strrep('`', max(l + 1, 3))
} else {
i = '`'
b = '```'
Expand Down
9 changes: 2 additions & 7 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,7 @@ pad_width = function(x, width, side) {
stop("'side' must be 'left', 'right', or 'both'")
w = width - nchar(x, 'width')
w1 = floor(w / 2) # the left half of spaces when side = 'both'
s1 = v_spaces(w * (side == 'left') + w1 * (side == 'both'))
s2 = v_spaces(w * (side == 'right') + (w - w1) * (side == 'both'))
s1 = strrep(' ', pmax(0, w * (side == 'left') + w1 * (side == 'both')))
s2 = strrep(' ', pmax(0, w * (side == 'right') + (w - w1) * (side == 'both')))
paste0(s1, x, s2)
}

# vectorized over n to generate sequences of spaces
v_spaces = function(n) {
unlist(lapply(n, highr:::spaces))
}
8 changes: 8 additions & 0 deletions R/utils-string.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ str_replace = function(x, pos, value) {
y = substring(x, m[1, ], m[2, ])
paste(rbind(y, c(value, '')), collapse = '')
}

# a wrapper function to make strwrap() return a character vector of the same
# length as the input vector; each element of the output vector is a string
# formed by concatenating wrapped strings by \n
str_wrap = function(...) {
res = strwrap(..., simplify = FALSE)
unlist(lapply(res, one_string))
}
38 changes: 37 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ comment_to_var = function(x, varname, pattern, envir) {
FALSE
}

# TODO: remove this when we don't support R < 3.5.0
if (getRversion() < '3.5.0') isFALSE = function(x) identical(x, FALSE)

is_blank = function(x) {
if (length(x)) all(grepl('^\\s*$', x)) else TRUE
}
Expand Down Expand Up @@ -577,7 +580,13 @@ print_knitlog = function() {
}

# count the number of lines
line_count = function(x) stringr::str_count(x, '\n') + 1L
line_count = function(x) {
n = lengths(strsplit(x, '\n', fixed = TRUE))
i = grep('\n$', x)
n[i] = n[i] + 1L # add an extra count for lines ending with \n
n[n == 0] = 1L # should be at least one line
n
}

has_package = function(pkg) loadable(pkg, FALSE)
has_packages = function(pkgs) {
Expand Down Expand Up @@ -1085,3 +1094,30 @@ str_split = function(x, split, ...) {
y[x == ''] = list('')
y
}

# default progress bar function in knitr: create a text progress bar, and return
# methods to update/close it
txt_pb = function(total, labels) {
s = ifelse(labels == '', '', sprintf(' (%s)', labels)) # chunk labels in ()
w = nchar(s) # widths of labels
n = max(w)
# right-pad spaces for same width of all labels so a wider label of the
# progress bar in a previous step could be completely wiped (by spaces)
s = paste0(s, strrep(' ', n - w))
w2 = getOption('width')
con = getOption('knitr.progress.output', '')
pb = txtProgressBar(
0, total, 0, '.', width = max(w2 - 10 - n, 10), style = 3, file = con
)
list(
update = function(i) {
setTxtProgressBar(pb, i)
cat(s[i], file = con) # append chunk label to the progress bar
},
done = function() {
# wipe the progress bar
cat(paste0('\r', strrep(' ', max(w2, 10) + 10 + n)), file = con)
close(pb)
}
)
}

0 comments on commit b3a8365

Please sign in to comment.