Skip to content

Commit

Permalink
bug fix #174
Browse files Browse the repository at this point in the history
divided from 0 instead of 1 to allow first page to have same number of rows.
  • Loading branch information
fb-elong committed Jun 16, 2023
1 parent 08520b9 commit 2ec5ad9
Show file tree
Hide file tree
Showing 7 changed files with 731 additions and 695 deletions.
30 changes: 15 additions & 15 deletions R/as_rtf_pageby.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,17 @@ as_rtf_pageby <- function(tbl) {
page_dict$id <- factor(page_dict$id, levels = unique(page_dict$id))
}

# Define page number for each row
page_dict_page <- function(page_dict) {
# Page Number
page <- cumsum(page_dict$nrow) %/% page_dict$total

# If the last row is a page by row, move it to next row
retain <- unlist(lapply(split(page_dict, page), function(x) {
rev(cumsum(rev(x$pageby)) == seq_len(nrow(x)))
}))

page + retain + 1
}

## Adjust page number if pageby$new_page is true
new_page <- pageby$new_page | attr(tbl, "rtf_by_subline")$new_page
if (new_page) {
if (pageby$new_page) page_id <- page_dict$page_id
if (attr(tbl, "rtf_by_subline")$new_page) page_id <- page_dict$subline
if (pageby$new_page & attr(tbl, "rtf_by_subline")$new_page) page_id <- page_dict$id

page_dict$page <- unlist(lapply(split(page_dict, page_id), page_dict_page))
page_dict$page <- unlist(lapply(split(page_dict, page_id), pageby_dict_page))
page_dict$page <- as.numeric(page_id) * 1e6 + page_dict$page
} else {
page_dict$page <- page_dict_page(page_dict)
page_dict$page <- pageby_dict_page(page_dict)
}

page_dict$index <- cumsum(!page_dict$pageby)
Expand Down Expand Up @@ -295,3 +282,16 @@ as_rtf_pageby <- function(tbl) {
attr(rtf, "info") <- page_dict
rtf
}

# Define page number for each row
pageby_dict_page <- function(page_dict) {
# Page Number
page <- (cumsum(page_dict$nrow) - 1) %/% page_dict$total

# If the last row is a page by row, move it to next row
retain <- unlist(lapply(split(page_dict, page), function(x) {
rev(cumsum(rev(x$pageby)) == seq_len(nrow(x)))
}))

page + retain + 1
}
16 changes: 7 additions & 9 deletions R/as_rtf_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,13 @@ as_rtf_table <- function(tbl) {
stringsAsFactors = FALSE
)

# Define page number for each row
page_dict_page <- function(page_dict) {
# Page Number
page <- cumsum(page_dict$nrow) %/% page_dict$total

page + 1
}

if (!is.null(attr(tbl, "rtf_by_subline")$id)) {
page_dict$id <- attr(tbl, "rtf_by_subline")$id
page_dict$subline <- attr(tbl, "rtf_by_subline")$id
page_dict$page <- unlist(lapply(split(page_dict, page_dict$id), page_dict_page))
page_dict$page <- as.numeric(page_dict$id) * 1e6 + page_dict$page
} else {
page_dict$page <- with(page_dict, cumsum(nrow) %/% total) + 1
page_dict$page <- page_dict_page(page_dict)
}

# Move to next page for footnote and data source
Expand Down Expand Up @@ -138,3 +130,9 @@ as_rtf_table <- function(tbl) {
attr(rtf, "info") <- page_dict
rtf
}

# Define page number for each row
page_dict_page <- function(page_dict) {
# Page Number
with(page_dict, (cumsum(nrow) - 1) %/% total) + 1
}
2 changes: 1 addition & 1 deletion R/rtf_encode_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ rtf_encode_list <- function(tbl) {

info$total <- min(info$total)

info$page1 <- cumsum(info$nrow) %/% info$total + 1
info$page1 <- page_dict_page(info)

page1 <- info[info$page == 1, ]
page1 <- split(page1, page1$item)
Expand Down
1,334 changes: 694 additions & 640 deletions tests/testthat/_snaps/independent-testing-as_rtf_pageby.md

Large diffs are not rendered by default.

Large diffs are not rendered by default.

30 changes: 3 additions & 27 deletions tests/testthat/_snaps/independent-testing-rtf_encode_table.md

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions tests/testthat/test-independent-testing-rtf_encode_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,17 @@ test_that("Test case when using subline_by, page_by, group_by simultaneously wit

attr(ae_t3, "page")$page_title <- "first"
ae_t3a <- rtf_encode_table(ae_t3)
expect_snapshot_output(ae_t3a)

tmp <- lapply(strsplit(ae_t3a$body, "\\page", fixed = TRUE)[[1]],
function(x) grepl("subline", x)
)
expect_true(tmp[[1]])

attr(ae_t3, "page")$page_title <- "last"
ae_t3b <- rtf_encode_table(ae_t3)
expect_snapshot_output(ae_t3b)

tmp <- lapply(strsplit(ae_t3b$body, "\\page", fixed = TRUE)[[1]],
function(x) grepl("subline", x)
)
expect_true(tmp[[length(tmp)]])
})

0 comments on commit 2ec5ad9

Please sign in to comment.