Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor edits #910

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 0.20.2.6
Version: 0.20.2.7
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Fixed issue in `get_variance()` that could lead to recursive calls for
*brms* models, resulting in "infinite" resampling of the model.

* Fixed issue in `check_if_installed()` that errorneously tried to guess the
* Fixed issue in `check_if_installed()` that erroneously tried to guess the
minimum required package version based on the SUGGEST field of the _insight_
package, instead of the package that was calling the function.

Expand Down
44 changes: 22 additions & 22 deletions R/export_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
#' export_table(d, width = c(x = 5, z = 10))
#' export_table(d, width = c(x = 5, y = 5, z = 10), align = "lcr")
#' @export
export_table <- function(x,

Check warning on line 105 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=105,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 79 to at most 40.
sep = " | ",
header = "-",
cross = NULL,
Expand Down Expand Up @@ -368,22 +368,21 @@
col_width <- NULL
}

# round all numerics
# round all numerics, and convert to character
col_names <- names(tabledata)
tabledata <- as.data.frame(sapply(tabledata, function(i) {
tabledata[] <- lapply(tabledata, function(i) {
if (is.numeric(i)) {
format_value(i,
out <- format_value(i,
digits = digits, protect_integers = protect_integers,
missing = missing, width = col_width, zap_small = zap_small
)
} else {
i
out <- i
}
}, simplify = FALSE), stringsAsFactors = FALSE)

as.character(out)
})

# Convert to character.
tabledata <- as.data.frame(sapply(tabledata, as.character, simplify = FALSE), stringsAsFactors = FALSE)
# add back column names
names(tabledata) <- col_names
tabledata[is.na(tabledata)] <- as.character(missing)

Expand Down Expand Up @@ -412,7 +411,7 @@
# default alignment
col_align <- rep("right", ncol(tabledata))

# first column definitely right alignment, fixed width
# first row definitely right alignment, fixed width
first_row <- as.character(aligned[1, ])
for (i in seq_along(first_row)) {
aligned[1, i] <- format(
Expand Down Expand Up @@ -475,7 +474,7 @@
# plain text formatting ------------------------


.format_basic_table <- function(final,

Check warning on line 477 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=477,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 70 to at most 40.
header,
sep,
cross = NULL,
Expand All @@ -490,15 +489,16 @@
col_width = NULL,
col_align = NULL,
table_width = NULL) {
# align table, if requested. unlike the generic aligments that have
# been done previously, we now look for column-specific alignments
# align table, if requested. unlike the generic aligments that have been done
# previously, we now look for column-specific alignments. we furthermore save
# the alignments in "col_align", which we may need later when we set a fixed
# column width for specific columns across multiple tables, and have to
# re-align the columns again.

if (!is.null(align) && length(align) == 1) {
for (i in seq_len(ncol(final))) {
align_char <- ""
if (align %in% c("left", "right", "center", "firstleft")) {
align_char <- ""
} else {
if (!align %in% c("left", "right", "center", "firstleft")) {
align_char <- substr(align, i, i)
}

Expand Down Expand Up @@ -533,7 +533,7 @@
}


# check for fixed column widths. usually, column widht is aligned to the
# check for fixed column widths. usually, column width is aligned to the
# widest element in that column. for multiple tables, this may result in
# columns which do not have the the same width across tables, despite
# having the same "meaning" (e.g., zero-inflated models that have a table
Expand Down Expand Up @@ -577,15 +577,18 @@

# width of first table row of complete table. Currently, "final" is still
# a matrix, so we need to paste the columns of the first row into a string
row_width <- nchar(paste0(final[1, ], collapse = sep), type = "width")

Check warning on line 580 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=580,col=24,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.

# possibly first split - all table columns longer than "line_width"
# (i.e. first table row) go into a second string
if (row_width > line_width) {
i <- 1
# determine how many columns fit into the first line
while (nchar(paste0(final[1, 1:i], collapse = sep), type = "width") < line_width) {

Check warning on line 587 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=587,col=20,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.
i <- i + 1
}
# copy first column, and all columns that did not fit into the first line
# into the second table matrix
if (i > 2 && i < ncol(final)) {
final2 <- final[, c(1, i:ncol(final))]
final <- final[, 1:(i - 1)]
Expand All @@ -593,13 +596,14 @@
}

# width of first table row of remaing second table part
row_width <- nchar(paste0(final2[1, ], collapse = sep), type = "width")

Check warning on line 599 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=599,col=24,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.

# possibly second split - all table columns longer than "line_width"
# (i.e. first table row) go into a third string
# (i.e. first table row) go into a third string - we repeat the same
# procedure as above
if (row_width > line_width) {
i <- 1
while (nchar(paste0(final2[1, 1:i], collapse = sep), type = "width") < line_width) {

Check warning on line 606 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=606,col=20,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.
i <- i + 1
}
if (i > 2 && i < ncol(final2)) {
Expand Down Expand Up @@ -670,7 +674,7 @@
for (row in seq_len(nrow(final))) {
# create a string for each row, where cells from original matrix are
# separated by the separator char
final_row <- paste0(final[row, ], collapse = sep)

Check warning on line 677 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=677,col=18,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.
# check if we have an empty row, and if so, fill with an
# "empty line separator", if requested by user
if (!is.null(empty_line) && !any(nzchar(trim_ws(final[row, ])))) {
Expand All @@ -681,7 +685,7 @@
# the empty line, which is just empty cells with separator char,
# will now be replaced by the "empty line char", so we have a
# clean separator line
paste0(rep_len(empty_line, nchar(final_row, type = "width")), collapse = ""),

Check warning on line 688 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=688,col=9,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.
cross, sep, final_row,
is_last_row = row == nrow(final)
)
Expand All @@ -695,7 +699,7 @@
# check whether user wants to have a "cross" char where vertical and
# horizontal lines (from header line) cross.
header_line <- .insert_cross(
paste0(rep_len(header, nchar(final_row, type = "width")), collapse = ""),

Check warning on line 702 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=702,col=9,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.
cross, sep, final_row,
is_last_row = row == nrow(final)
)
Expand Down Expand Up @@ -813,7 +817,7 @@

# markdown formatting -------------------

.format_markdown_table <- function(final,

Check warning on line 820 in R/export_table.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/export_table.R,line=820,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 43 to at most 40.
x,
caption = NULL,
subtitle = NULL,
Expand Down Expand Up @@ -841,12 +845,8 @@
# check if user-defined alignment is requested, and if so, extract
# alignment direction and save to "align_char"
align_char <- ""
if (!is.null(align)) {
if (align %in% c("left", "right", "center", "firstleft")) {
align_char <- ""
} else {
align_char <- substr(align, i, i)
}
if (!is.null(align) && !align %in% c("left", "right", "center", "firstleft")) {
align_char <- substr(align, i, i)
}

# auto-alignment?
Expand Down
Loading