Skip to content

Commit

Permalink
fix #2308: don't trim trailing spaces escaped by \
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Dec 5, 2023
1 parent 4b47d2a commit 076f2c7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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.45.6
Version: 1.45.7
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

- The `family` argument was not passed to the `pdf` device (thanks, @sebkopf, rstudio/rmarkdown#2526).

- Trailing spaces escaped by `\` should not be trimmed in `kable()` (thanks, @mjsmith037, #2308).

## MAJOR CHANGES

- Unbalanced chunk delimiters (fences) in R Markdown documents are no longer allowed, as announced two years ago at <https://yihui.org/en/2021/10/unbalanced-delimiters/> (#2306). This means the opening delimiter must strictly match the closing delimiter, e.g., if a code chunk starts with four backticks, it must also end with four; or if a chunk header is indented by two spaces, the closing fence must be indented by exactly two spaces. For authors who cannot update their R Markdown documents for any reason at the moment, setting `options(knitr.unbalanced.chunk = TRUE)` (e.g., in `.Rprofile`) can temporarily prevent **knitr** from throwing an error, but it is strongly recommended that you fix the problems as soon as possible, because this workaround will be removed in future.
Expand Down
3 changes: 2 additions & 1 deletion R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ kable = function(
n = nrow(x)
x = replace_na(to_character(x), is.na(x))
if (!is.matrix(x)) x = matrix(x, nrow = n)
x = trimws(x)
# trim white spaces except those escaped by \ at the end (#2308)
x = gsub('^\\s+|(?<!\\\\)\\s+$', '', x, perl = TRUE)
colnames(x) = col.names
if (format != 'latex' && length(align) && !all(align %in% c('l', 'r', 'c')))
stop("'align' must be a character vector of possible values 'l', 'r', and 'c'")
Expand Down
11 changes: 11 additions & 0 deletions tests/testit/test-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ x\\\\
\\end{tabular}')
})

assert('kable() does not trim escaped spaces at the end', {
(kable2(data.frame(x = '\\ '), 'latex', escape =FALSE ) %==% '
\\begin{tabular}{l}
\\hline
x\\\\
\\hline
\\ \\\\
\\hline
\\end{tabular}')
})

assert('kable() escapes HTML special characters by default', {
(kable2(data.frame(x = c('10<>', '5&2'), y = c('3>8', '"40"')), 'html') %==%
'<table>\n <thead>\n <tr>\n <th style="text-align:left;"> x </th>\n <th style="text-align:left;"> y </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style="text-align:left;"> 10&lt;&gt; </td>\n <td style="text-align:left;"> 3&gt;8 </td>\n </tr>\n <tr>\n <td style="text-align:left;"> 5&amp;2 </td>\n <td style="text-align:left;"> &quot;40&quot; </td>\n </tr>\n</tbody>\n</table>')
Expand Down

0 comments on commit 076f2c7

Please sign in to comment.