Skip to content

Commit

Permalink
Update pagination vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Mar 7, 2024
1 parent 6a5772a commit 9970d40
Showing 1 changed file with 5 additions and 36 deletions.
41 changes: 5 additions & 36 deletions vignettes/pagination.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,11 @@ The `export_as_rtf` function can be used similarly to `export_as_txt` to paginat

## Pagination by Parameter

In addition to paginating by page size as described in the previous sections of this vignette, a user may also want to paginate their listing such that each page corresponds to a different value of a given parameter. For example, you may require that each treatment arm is printed on a separate page. This can currently be done with `rlistings` using the following workaround:

```{r}
split_listing_by_param <- function(lsting, param, page_prefix = param) {
checkmate::assert_class(lsting, "listing_df")
checkmate::assert_choice(param, names(lsting))
lsting_by_param <- list()
for (lvl in unique(lsting[[param]])) {
param_desc <- paste0(page_prefix, ": ", lvl)
lsting_by_param[[lvl]] <- lsting[lsting[[param]] == lvl, ]
subtitles(lsting_by_param[[lvl]]) <- c(subtitles(lsting), param_desc)
}
unname(lsting_by_param)
}
```

After loading the function above, you can apply it to your pre-existing listing as follows:
In addition to paginating by page size as described in the previous sections of this vignette, a user may also want to paginate their listing such that each page corresponds to a different value of a given parameter. For example, you may require that each treatment arm is printed on a separate page. This can currently be done with `rlistings` using the `split_listing_by_var` function, which can be applied to your pre-existing listing as follows:

```{r}
lsting_by_arm <- lsting %>%
split_listing_by_param("ARM", page_prefix = "Treatment Arm")
split_listing_by_var("ARM", page_prefix = "Treatment Arm")
lsting_by_arm
```
Expand All @@ -192,30 +175,16 @@ As with `paginate_listing`, this creates a list of listings where each list elem

#### Combining Pagination by Parameter with Regular Pagination

To then apply regular pagination to the listing, you can apply the following function to your list of listings by parameter. Any arguments to `paginate_listing` supplied to this function will be applied to each list element.

```{r}
paginate_lsting_by_param <- function(lsting, ...) {
unlist(lapply(lsting, paginate_listing, ...), recursive = FALSE)
}
```
To then apply regular pagination to the listing, you can apply `paginate_listing` to your list of listings by parameter. Any arguments supplied to this function will be applied to each list element.

#### Combining Pagination by Parameter with `export_as_txt`

Similarly, for pagination via `export_as_txt` after paginating by parameter, you can apply the following function to your list of listings by parameter. Any arguments to `export_as_txt` supplied to this function will be applied to each list element and the list will then be concatenated into the correct text format.

```{r}
export_to_txt_lsting_by_param <- function(lsting, file = NULL, page_break = "\\s\\n", ...) {
lst <- unlist(lapply(lsting, export_as_txt, page_break = page_break, ...), recursive = FALSE)
res <- paste(lst, collapse = page_break)
if (is.null(file)) res else cat(res, file = file)
}
```
Similarly, for pagination via `export_as_txt` after paginating by parameter, you can apply `export_as_txt` to your list of listings by parameter. Any arguments supplied to `export_as_txt` will be applied to each list element and the list will then be concatenated into the correct text format.

For example:

```{r}
cat(export_to_txt_lsting_by_param(lsting_by_arm))
cat(export_as_txt(lsting_by_arm))
```

Again, we use the `cat` function to make the text output more easily readable in the console.
Expand Down

0 comments on commit 9970d40

Please sign in to comment.