Skip to content

Commit

Permalink
Merge branch 'haozhu233-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
warnes committed Dec 2, 2023
2 parents 3886bc7 + 64b838b commit 281fcb5
Show file tree
Hide file tree
Showing 73 changed files with 1,665 additions and 733 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
^tests$
^docs$
^.github$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Think about using `reprex` to generate an example in R.
# code to reproduce the bug.
```

If you need to attach the entire rmarkdown doc, try to use ```` to quote your entire doc.
If you need to attach the entire R Markdown doc, try to use ```` to quote your entire doc.

````
---
title: "A reproducible example in rmarkdown"
title: "A reproducible example in R Markdown"
output: html_document
---
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-tinytex@v2
- run: tlmgr --version
- run: tlmgr install colortbl grfext ae
- run: tlmgr list --only-installed
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual", "--compact-vignettes=both")'
11 changes: 5 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: kableExtra
Type: Package
Title: Construct Complex Table with 'kable' and Pipe Syntax
Version: 1.3.4.9000
Version: 1.3.9.9000
Authors@R: c(
person('Hao', 'Zhu', email = 'haozhu233@gmail.com', role = c('aut', 'cre'),
comment = c(ORCID = '0000-0002-3386-6076')),
Expand Down Expand Up @@ -35,26 +35,25 @@ Imports:
magrittr,
stringr (>= 1.0),
xml2 (>= 1.1.1),
rvest,
rmarkdown (>= 1.6.0),
scales,
viridisLite,
stats,
grDevices,
htmltools,
rstudioapi,
glue,
tools,
webshot,
digest,
graphics,
svglite
Suggests:
testthat,
magick,
formattable,
sparkline
sparkline,
webshot2
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.2.0
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
Language: en-US
4 changes: 1 addition & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(knit_print,kableExtra)
S3method(print,kableExtra)
S3method(print,kableExtraInlinePlots)
export("%>%")
export(add_footnote)
export(add_header_above)
Expand Down Expand Up @@ -58,7 +59,6 @@ export(xml_as_kable)
export(xtable2kable)
import(htmltools)
importFrom(digest,digest)
importFrom(glue,glue)
importFrom(grDevices,col2rgb)
importFrom(grDevices,png)
importFrom(grDevices,svg)
Expand All @@ -79,7 +79,6 @@ importFrom(rmarkdown,metadata)
importFrom(rmarkdown,pandoc_self_contained_html)
importFrom(rstudioapi,isAvailable)
importFrom(rstudioapi,viewer)
importFrom(rvest,html_table)
importFrom(scales,rescale)
importFrom(stats,ave)
importFrom(stats,density)
Expand All @@ -105,7 +104,6 @@ importFrom(utils,capture.output)
importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(viridisLite,viridis)
importFrom(webshot,webshot)
importFrom(xml2,"xml_attr<-")
importFrom(xml2,"xml_text<-")
importFrom(xml2,read_html)
Expand Down
2 changes: 1 addition & 1 deletion R/add_footnote.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ add_footnote <- function(input, label = NULL,
"[0-9]{1,2}")))

# Pandoc ---------------------------
# Footnote solution for markdown and pandoc. It is not perfect as
# Footnote solution for markdown and Pandoc. It is not perfect as
# markdown doesn't support complex table formats but this solution
# should be able to satisfy people who don't want to spend extra
# time to define their `kable` format.
Expand Down
14 changes: 9 additions & 5 deletions R/add_header_above.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
#' Alternatively, a data frame with two columns can be provided: The first
#' column should contain the header names (character vector) and the second
#' column should contain the colspan (numeric vector). This input can be used
#' if there are problems with unicode characters in the headers.
#' if there are problems with Unicode characters in the headers.
#' @param bold A T/F value to control whether the text should be bolded.
#' @param italic A T/F value to control whether the text should to be emphasized.
#' @param monospace A T/F value to control whether the text of the selected column
#' need to be monospaced (verbatim)
#' @param underline A T/F value to control whether the text of the selected row
#' need to be underlined
#' @param strikeout A T/F value to control whether the text of the selected row
#' need to be stricked out.
#' need to be struck out.
#' @param align A character string for cell alignment. For HTML, possible values could
#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
#' while for LaTeX, you can only choose from `l`, `c` & `r`.
Expand Down Expand Up @@ -68,6 +68,10 @@ add_header_above <- function(kable_input, header = NULL,
if (is.null(header)) return(kable_input)

kable_format <- attr(kable_input, "format")
if (kable_format %in% c("pipe", "markdown")) {
kable_input <- md_table_parser(kable_input)
kable_format <- attr(kable_input, "format")
}
if (!kable_format %in% c("html", "latex")) {
warning("Please specify format in kable. kableExtra can customize either ",
"HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
Expand Down Expand Up @@ -257,7 +261,8 @@ htmlTable_new_header_generator <- function(header_df, bold, italic, monospace,
}

line_sep <- ez_rep(line_sep, nrow(header_df))
line_sep <- glue::glue('padding-left:{line_sep}px;padding-right:{line_sep}px;')
line_sep <- paste0('padding-left:', line_sep, 'px;',
'padding-right:', line_sep, 'px;')

row_style <- sprintf(row_style, align)

Expand Down Expand Up @@ -405,8 +410,7 @@ cline_gen <- function(header_df, booktabs, line_sep) {
cline_type <- switch(
booktabs + 1,
"\\\\cline{",
glue::glue("\\\\cmidrule(l{[line_sep]pt}r{[line_sep]pt}){",
.open = "[", .close = "]"))
paste0("\\\\cmidrule(l{", line_sep, "pt}r{", line_sep, "pt}){"))
cline <- paste0(cline_type, cline_start, "-", cline_end, "}")
cline <- cline[trimws(header_df$header) != ""]
cline <- paste(cline, collapse = " ")
Expand Down
5 changes: 5 additions & 0 deletions R/add_indent.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ add_indent <- function(kable_input, positions,
stop("Positions can only take numeric row numbers (excluding header rows).")
}
kable_format <- attr(kable_input, "format")
if (kable_format %in% c("pipe", "markdown")) {
kable_input <- md_table_parser(kable_input)
kable_format <- attr(kable_input, "format")
}

if (!kable_format %in% c("html", "latex")) {
warning("Please specify format in kable. kableExtra can customize either ",
"HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
Expand Down
10 changes: 5 additions & 5 deletions R/as_image.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#' Render the table as an format-independent image and use it in rmarkdown
#' Render the table as an format-independent image and use it in R Markdown
#'
#' @description This function generates a temporary png file using `save_kable`
#' and then try to put it in an rmarkdown document using
#' and then try to put it in an R Markdown document using
#' `knitr::include_graphics`.
#'
#' @param x kable input. Either HTML or LaTeX
#' @param width Image width in inches. (1 inch = 2.54 cm)
#' @param height Image height in inches. (1 inch = 2.54 cm)
#' @param file By default, as_image saves to an temp file, which works for
#' normal rmarkdown. However if you are using things like xaringan, which can't
#' be a standalone html, you can specify this file be the path you need, eg.
#' "img/something.png"
#' normal R Markdown. However if you are using things like xaringan, which can't
#' be a standalone html, you can specify this file be the path you need, e.g.
#' `"img/something.png"`
#'
#' @param ... Additional arguments passed to save_kable.
#'
Expand Down
4 changes: 2 additions & 2 deletions R/cell_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @description Specify Cell format before it gets into kable
#'
#' @param x Things to be formated. It could be a vector of numbers or strings.
#' @param x Things to be formatted. It could be a vector of numbers or strings.
#' @param format Either "html" or "latex". It can also be set through
#' `option(knitr.table.format)`, same as `knitr::kable()`.
#' @param bold T/F for font bold.
Expand All @@ -11,7 +11,7 @@
#' @param underline A T/F value to control whether the text of the selected row
#' need to be underlined
#' @param strikeout A T/F value to control whether the text of the selected row
#' need to be stricked out.
#' need to be struck out.
#' @param color A character string for text color. Here please pay
#' attention to the differences in color codes between HTML and LaTeX.
#' @param background A character string for background color. Here please
Expand Down
12 changes: 5 additions & 7 deletions R/collapse_rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ collapse_rows <- function(kable_input, columns = NULL,
col_names = TRUE,
longtable_clean_cut = TRUE) {
kable_format <- attr(kable_input, "format")
if (kable_format %in% c("pipe", "markdown")) {
kable_input <- md_table_parser(kable_input)
kable_format <- attr(kable_input, "format")
}
if (!kable_format %in% c("html", "latex")) {
warning("Please specify format in kable. kableExtra can customize either ",
"HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
Expand Down Expand Up @@ -87,8 +91,7 @@ collapse_rows_html <- function(kable_input, columns, valign, target) {
kable_xml <- kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")

kable_dt <- rvest::html_table(xml2::read_html(as.character(kable_input)))[[1]]
kable_dt <- as.data.frame(kable_dt)
kable_dt <- read_table_data_from_xml(kable_xml)
if (is.null(columns)) {
columns <- seq(1, ncol(kable_dt))
}
Expand All @@ -97,11 +100,6 @@ collapse_rows_html <- function(kable_input, columns, valign, target) {
stop("target has to be within the range of columns")
}
}
if (!is.null(kable_attrs$header_above)) {
kable_dt_col_names <- unlist(kable_dt[kable_attrs$header_above, ])
kable_dt <- kable_dt[-(1:kable_attrs$header_above),]
names(kable_dt) <- kable_dt_col_names
}
collapse_matrix <- collapse_row_matrix(kable_dt, columns, target = target)

for (i in 1:nrow(collapse_matrix)) {
Expand Down
14 changes: 11 additions & 3 deletions R/column_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' @param underline T/F value or vector to control whether the text of the
#' selected row need to be underlined
#' @param strikeout T/F value or vector to control whether the text of the
#' selected row need to be striked out.
#' selected row need to be struck out.
#' @param color A character string or vector for column text color. Here please
#' pay attention to the differences in color codes between HTML and LaTeX.
#' @param background A character string or vector for column background color. Here please
Expand All @@ -35,7 +35,7 @@
#' of table columns.
#' @param extra_css A vector of extra css text to be passed into the cells of
#' the column.
#' @param include_thead T/F. A HTML only feature to contoll whether the
#' @param include_thead T/F. A HTML only feature to control whether the
#' header row will be manipulated. Default is `FALSE`.
#' @param latex_column_spec Only for LaTeX tables. Code to replace the column
#' specification. If not `NULL`, will override all other arguments.
Expand Down Expand Up @@ -79,6 +79,11 @@ column_spec <- function(kable_input, column,
stop("column must be numeric. ")
}
kable_format <- attr(kable_input, "format")
if (kable_format %in% c("pipe", "markdown")) {
kable_input <- md_table_parser(kable_input)
kable_format <- attr(kable_input, "format")
}

if (!kable_format %in% c("html", "latex")) {
warning("Please specify format in kable. kableExtra can customize either ",
"HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
Expand Down Expand Up @@ -189,7 +194,7 @@ column_spec_html <- function(kable_input, column, width,
for (i in seq(length(all_contents_rows))) {
for (j in column) {
io <- i + off
target_cell <- xml_child(xml_child(kable_tbody, all_contents_rows[io]), j)
target_cell <- xml_child(xml_child(kable_tbody, all_contents_rows[i]), j)
column_spec_html_cell(
target_cell, width, width_min, width_max,
bold[io], italic[io], monospace[io], underline[io], strikeout[io],
Expand Down Expand Up @@ -394,6 +399,9 @@ column_spec_latex <- function(kable_input, column, width,
rows <- seq(1 + off, nrows)
}

# issue #658: offset generates bad indices with single row tables
rows <- intersect(rows, seq_along(nrows))

for (i in rows) {
target_row <- table_info$contents[i]
new_row <- latex_cell_builder(
Expand Down
8 changes: 6 additions & 2 deletions R/footnote.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Add footnote (new)
#'
#' @description `footnote` provides a more flexible way to add footnote. You
#' can add mutiple sets of footnote using different notation systems. It is
#' can add multiple sets of footnote using different notation systems. It is
#' also possible to specify footnote section header one by one and print
#' footnotes as a chunk of texts.
#'
Expand All @@ -11,7 +11,7 @@
#' @param number A vector of footnote texts. Footnotes here will be numbered.
#' There is no upper cap for the number of footnotes here
#' @param alphabet A vector of footnote texts, Footnotes here will be labeled
#' with abc. The vector here should not have more than 26 elements.
#' with "a,b,c". The vector here should not have more than 26 elements.
#' @param symbol A vector of footnote texts, Footnotes here will be labeled
#' with special symbols. The vector here should not have more than 20 elements.
#' @param footnote_order The order of how to arrange `general`, `number`,
Expand Down Expand Up @@ -63,6 +63,10 @@ footnote <- function(kable_input,
symbol_manual = NULL
) {
kable_format <- attr(kable_input, "format")
if (kable_format %in% c("pipe", "markdown")) {
kable_input <- md_table_parser(kable_input)
kable_format <- attr(kable_input, "format")
}
if (!kable_format %in% c("html", "latex")) {
warning("Please specify format in kable. kableExtra can customize either ",
"HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
Expand Down
Loading

0 comments on commit 281fcb5

Please sign in to comment.