Skip to content

Commit

Permalink
Merge pull request #171 from atorus-research/gh_issue_170
Browse files Browse the repository at this point in the history
Introduce `replace_leading_whitespace()` for #170
  • Loading branch information
asbates committed Dec 20, 2023
2 parents f23701d + 8314175 commit e22b671
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions R/replace_leading_whitespace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Reformat strings with leading whitespace for HTML
#'
#' @param x Target string
#' @param tab_width Number of spaces to compensate for tabs
#'
#' @return String with   replaced for leading whitespace
#' @export
#'
#' @examples
#' 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 <- str_match(x, "^([ \\t])+")[,1]
# Count spaces and tabs, factor in 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 &nbsp; string and combine with the trimmed string
nbsp_string <- map_chr(leading_length, \(.x) {
if (!is.na(.x)) {
paste(rep("&nbsp;", .x), collapse="")
} else {
""
}})
minus_whitespace <- str_trim(x, side='left')
paste(nbsp_string, minus_whitespace, sep="")
}

4 changes: 2 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions man/replace_leading_whitespace.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-replace_leading_whitespace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("Test replacement of leading whitespace", {
x <- c("Hello there", " Goodbye Friend ", "\tNice to meet you", " \t What are you up to? \t \t ")

expect_equal(
replace_leading_whitespace(x),
c("Hello there", "&nbsp;&nbsp;Goodbye Friend ",
"&nbsp;&nbsp;&nbsp;&nbsp;Nice to meet you", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;What are you up to? \t \t ")
)

expect_equal(
replace_leading_whitespace(x, tab=2),
c("Hello there", "&nbsp;&nbsp;Goodbye Friend ",
"&nbsp;&nbsp;Nice to meet you", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;What are you up to? \t \t ")
)
})
44 changes: 44 additions & 0 deletions vignettes/post_processing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "&nbsp;&nbsp;") %>%
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 `&nbsp;`, 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 `&nbsp;` 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 `&nbsp;` 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)
```

Expand Down

0 comments on commit e22b671

Please sign in to comment.