From 4a94429f18436238494b1d6a8c66002d2bfff191 Mon Sep 17 00:00:00 2001 From: "mike.stackhouse" Date: Tue, 19 Dec 2023 20:56:13 +0000 Subject: [PATCH] Updated for #170 --- NAMESPACE | 2 ++ R/replace_leading_whitespace.R | 18 +++++++++----- R/zzz.R | 4 ++-- vignettes/post_processing.Rmd | 44 ++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 85bb70fb..d2b779eb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -89,6 +89,7 @@ export(process_statistic_data) export(process_statistic_formatting) export(process_summaries) export(remove_layer_template) +export(replace_leading_whitespace) export(set_by) export(set_count_layer_formats) export(set_custom_summaries) @@ -252,6 +253,7 @@ importFrom(stringr,str_detect) importFrom(stringr,str_extract) importFrom(stringr,str_extract_all) importFrom(stringr,str_locate_all) +importFrom(stringr,str_match) importFrom(stringr,str_match_all) importFrom(stringr,str_pad) importFrom(stringr,str_remove_all) diff --git a/R/replace_leading_whitespace.R b/R/replace_leading_whitespace.R index a75ecc09..94907755 100644 --- a/R/replace_leading_whitespace.R +++ b/R/replace_leading_whitespace.R @@ -7,22 +7,28 @@ #' @export #' #' @examples -#' x <- c(" Hello there", " Goodbye Friend ", "\tNice to meet you", " \t What are you up to? \t \t ") +#' x <- c(" Hello there", " Goodbye Friend ", "\tNice to meet you", +#' " \t What are you up to? \t \t ") #' replace_leading_whitespace(x) #' #' replace_leading_whitespace(x, tab=2) #' replace_leading_whitespace <- function(x, tab_width=4) { # Pull out the leading whitespace chunk - leading_spaces <- stringr::str_match(x, "^([ \\t])+")[,1] + leading_spaces <- str_match(x, "^([ \\t])+")[,1] # Count spaces and tabs, factor in tab width - spaces <- stringr::str_count(leading_spaces, pattern = " ") - tabs <- stringr::str_count(leading_spaces, pattern = "\\t") * tab_width + spaces <- str_count(leading_spaces, pattern = " ") + tabs <- str_count(leading_spaces, pattern = "\\t") * tab_width leading_length <- as.integer(spaces + tabs) # Build the   string and combine with the trimmed string - nbsp_string <- map_chr(leading_length, \(.x) paste(rep(" ", .x), collapse="")) - minus_whitespace <- stringr::str_trim(x, side=left) + nbsp_string <- map_chr(leading_length, \(.x) { + if (!is.na(.x)) { + paste(rep(" ", .x), collapse="") + } else { + "" + }}) + minus_whitespace <- str_trim(x, side='left') paste(nbsp_string, minus_whitespace, sep="") } diff --git a/R/zzz.R b/R/zzz.R index c111ad2a..70df2747 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -2,9 +2,9 @@ #' @importFrom rlang call_modify call_name call_args is_call current_env quo_name trace_back is_function list2 #' @importFrom rlang expr exprs enexprs enexpr is_named env_parent env_label is_logical is_empty is_quosures quo_is_symbol sym syms := as_name #' @importFrom rlang quos quo env_names env_bind_active as_label eval_tidy warn quo_is_call -#' @importFrom stringr str_split str_extract_all regex str_detect str_replace_all str_replace str_locate_all fixed str_count str_trim str_wrap +#' @importFrom stringr str_split str_extract_all regex str_detect str_replace_all str_replace str_locate_all fixed str_count str_trim str_wrap str_count #' @importFrom purrr flatten map map_lgl pmap_chr imap reduce map_chr map_int map_dbl map_dfr pmap_dfr walk2 map2 map2_dfr map2_chr walk -#' @importFrom stringr str_sub str_sub<- str_extract str_pad str_starts str_remove_all str_match_all +#' @importFrom stringr str_sub str_sub<- str_extract str_pad str_starts str_remove_all str_match_all str_match #' @importFrom tidyr pivot_longer pivot_wider replace_na #' @importFrom magrittr %>% extract extract2 #' @importFrom assertthat assert_that diff --git a/vignettes/post_processing.Rmd b/vignettes/post_processing.Rmd index 1c6ba1f9..ca676397 100644 --- a/vignettes/post_processing.Rmd +++ b/vignettes/post_processing.Rmd @@ -127,6 +127,50 @@ You also have control over which columns you collapse, allowing you to keep sepa ```{r collapse_row_labels3} collapse_row_labels(dat, row_label1, row_label2, indent = "  ") %>% select(row_label, row_label3, var1_Placebo) %>% + head() %>% + kable() +``` + +## Leading Spaces in HTML Files + +Another helper function we've made available is `replace_leading_whitespace()`. In the table created above, note that the `indent` parameter was set using ` `, which is a non-breaking space. This can be used in HTML files to preserve leading white spaces instead of automatically stripping them in the display, as viewing utilities usually do. Ever noticed that in your data viewers you typically don't see leading spaces? Yeah - that's why! + +Let's take the example from above and not change the `indent` parameter. + +```{r replace_leading_whitespace1} +collapse_row_labels(dat, row_label1, row_label2) %>% + select(row_label, row_label3, var1_Placebo) %>% + kable() +``` + +In indented rows, the spaces still exist, and we can see that in the dataframe output itself. + +```{r replace_leading_whitespace2} +collapse_row_labels(dat, row_label1, row_label2) %>% + select(row_label, row_label3, var1_Placebo) %>% + head() +``` + +But the HTML view strips them off when we pass it into the `kable()` function. `replace_leading_whitespace()` will take care of this for us by converting the spaces. Note that you'll see the ` ` in the raw data itself. + +```{r replace_leading_whitespace3} +collapse_row_labels(dat, row_label1, row_label2) %>% + select(row_label, row_label3, var1_Placebo) %>% + mutate( + across(where(is.character), ~ replace_leading_whitespace(.)) + ) %>% + head() +``` + +But now when we want to use this in a display, the ` ` characters will show as leading whitespace within our HTML table. Note that you'll need to prevent escaping special characters for this to work, or the raw text will display. In `kable()` you can use `escape=FALSE` do this. + +```{r replace_leading_whitespace4} +collapse_row_labels(dat, row_label1, row_label2) %>% + select(row_label, row_label3, var1_Placebo) %>% + mutate( + across(where(is.character), ~ replace_leading_whitespace(.)) + ) %>% + head() %>% kable(escape=FALSE) ```