From 71e4249a88658a1b08f60036f9a20ee2f61677c8 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Thu, 20 Apr 2023 08:26:50 +0200 Subject: [PATCH 1/4] Include file and line numbers when rendering (Closes #2232) --- R/output.R | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/R/output.R b/R/output.R index 0004924c34..d833b5449b 100644 --- a/R/output.R +++ b/R/output.R @@ -291,9 +291,19 @@ process_file = function(text, output) { labels = unlist(lapply(groups, function(g) { if (is.list(g$params)) g[[c('params', 'label')]] else '' })) + linenums <- unlist(lapply(seq_along(groups), function(i) { + curr_lines <- current_lines(i) + sprintf( + "%s:%d-%d", + knit_concord$get('infile'), + curr_lines[1], + curr_lines[2] + ) + })) + labels_linenums <- paste(labels, linenums, sep = " @ ") if (progress) { pb_fun = getOption('knitr.progress.fun', txt_pb) - pb = if (is.function(pb_fun)) pb_fun(n, labels) + pb = if (is.function(pb_fun)) pb_fun(n, labels_linenums) on.exit(if (!is.null(pb)) pb$done(), add = TRUE) } wd = getwd() From ea94e8197d9bb9b16c2ff0ca7856e9fb5c3e9441 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Thu, 20 Apr 2023 08:28:52 +0200 Subject: [PATCH 2/4] Add NEWS entry --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 38ee44cbdc..7a62963de8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,8 @@ - `imgur_upload()` requires fewer package dependencies now. It only requires the **curl** package; **httr** is no longer required, and **xml2** has become optional. +- Progress bar includes the chunk location (`chunk-name @ file:line`) (@zeehio, #2232) + # CHANGES IN knitr VERSION 1.42 ## NEW FEATURES From d9d1613a07f10c4912a91eb405c43c6de50938bc Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Tue, 25 Apr 2023 15:59:19 +0200 Subject: [PATCH 3/4] Show line numbers in progress bar when options(knitr.progress.linenums = TRUE) --- NEWS.md | 2 +- R/output.R | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7a62963de8..a7f1add2bb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,7 +26,7 @@ - `imgur_upload()` requires fewer package dependencies now. It only requires the **curl** package; **httr** is no longer required, and **xml2** has become optional. -- Progress bar includes the chunk location (`chunk-name @ file:line`) (@zeehio, #2232) +- Progress bar includes the chunk location (`chunk-name @ file:line`) when `options(knitr.progress.linenums = TRUE)` is set. (@zeehio, #2232) # CHANGES IN knitr VERSION 1.42 diff --git a/R/output.R b/R/output.R index d833b5449b..d361b6a1d2 100644 --- a/R/output.R +++ b/R/output.R @@ -291,16 +291,20 @@ process_file = function(text, output) { labels = unlist(lapply(groups, function(g) { if (is.list(g$params)) g[[c('params', 'label')]] else '' })) - linenums <- unlist(lapply(seq_along(groups), function(i) { - curr_lines <- current_lines(i) - sprintf( - "%s:%d-%d", - knit_concord$get('infile'), - curr_lines[1], - curr_lines[2] - ) - })) - labels_linenums <- paste(labels, linenums, sep = " @ ") + if (getOption("knitr.progress.linenums", default = FALSE)) { + linenums <- unlist(lapply(seq_along(groups), function(i) { + curr_lines <- current_lines(i) + sprintf( + "%s:%d-%d", + knit_concord$get('infile'), + curr_lines[1], + curr_lines[2] + ) + })) + labels_linenums <- paste(labels, linenums, sep = " @ ") + } else { + labels_linenums <- labels + } if (progress) { pb_fun = getOption('knitr.progress.fun', txt_pb) pb = if (is.function(pb_fun)) pb_fun(n, labels_linenums) From 3c9666c0cd0c2b8a23e4785579fa7d664105b2e0 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Tue, 25 Apr 2023 12:07:37 -0500 Subject: [PATCH 4/4] move the code to add line numbers into txt_pb() (the default progress bar function) --- DESCRIPTION | 2 +- NEWS.md | 6 ++++-- R/output.R | 16 +--------------- R/utils.R | 9 +++++++++ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5e55eba218..a461864b12 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: knitr Type: Package Title: A General-Purpose Package for Dynamic Report Generation in R -Version: 1.42.7 +Version: 1.42.8 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"), diff --git a/NEWS.md b/NEWS.md index a7f1add2bb..89a19bda33 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # CHANGES IN knitr VERSION 1.43 +## NEW FEATURES + +- Progress bar includes the chunk location (`chunk-name @ file:line`) when `options(knitr.progress.linenums = TRUE)` is set (thanks, @zeehio, #2232). + ## BUG FIXES - The chunk option `collapse = TRUE` works with HTML widgets now (thanks, @dmurdoch, #2212). @@ -26,8 +30,6 @@ - `imgur_upload()` requires fewer package dependencies now. It only requires the **curl** package; **httr** is no longer required, and **xml2** has become optional. -- Progress bar includes the chunk location (`chunk-name @ file:line`) when `options(knitr.progress.linenums = TRUE)` is set. (@zeehio, #2232) - # CHANGES IN knitr VERSION 1.42 ## NEW FEATURES diff --git a/R/output.R b/R/output.R index d361b6a1d2..0004924c34 100644 --- a/R/output.R +++ b/R/output.R @@ -291,23 +291,9 @@ process_file = function(text, output) { labels = unlist(lapply(groups, function(g) { if (is.list(g$params)) g[[c('params', 'label')]] else '' })) - if (getOption("knitr.progress.linenums", default = FALSE)) { - linenums <- unlist(lapply(seq_along(groups), function(i) { - curr_lines <- current_lines(i) - sprintf( - "%s:%d-%d", - knit_concord$get('infile'), - curr_lines[1], - curr_lines[2] - ) - })) - labels_linenums <- paste(labels, linenums, sep = " @ ") - } else { - labels_linenums <- labels - } if (progress) { pb_fun = getOption('knitr.progress.fun', txt_pb) - pb = if (is.function(pb_fun)) pb_fun(n, labels_linenums) + pb = if (is.function(pb_fun)) pb_fun(n, labels) on.exit(if (!is.null(pb)) pb$done(), add = TRUE) } wd = getwd() diff --git a/R/utils.R b/R/utils.R index 8ae830a684..77bc556769 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1115,6 +1115,15 @@ str_split = function(x, split, ...) { # default progress bar function in knitr: create a text progress bar, and return # methods to update/close it txt_pb = function(total, labels) { + if (getOption('knitr.progress.linenums', FALSE)) { + nums = sapply(seq_along(labels), function(i) { + paste(current_lines(i), collapse = '-') + }) + labels = sprintf( + '%s%s%s:%s', labels, ifelse(labels == '', '', ' @ '), + knit_concord$get('infile'), nums + ) + } s = ifelse(labels == '', '', sprintf(' [%s]', labels)) # chunk labels in [] w = nchar(s) # widths of labels n = max(w)