Skip to content

Commit 7220bd2

Browse files
authored
Merge pull request #912 from rstudio/sub-fns
Add small collection of `sub_*()` functions
2 parents e6a762f + 0dd80f5 commit 7220bd2

File tree

147 files changed

+2714
-617
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+2714
-617
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ BugReports: https://github.com/rstudio/gt/issues
2323
Encoding: UTF-8
2424
LazyData: true
2525
ByteCompile: true
26-
RoxygenNote: 7.1.2
26+
RoxygenNote: 7.2.0
2727
Depends:
2828
R (>= 3.2.0)
2929
Imports:
@@ -102,6 +102,7 @@ Collate:
102102
'render_as_html.R'
103103
'resolver.R'
104104
'shiny.R'
105+
'substitution.R'
105106
'summary_rows.R'
106107
'text_transform.R'
107108
'utils.R'

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ export(random_id)
9898
export(render_gt)
9999
export(row_group_order)
100100
export(starts_with)
101+
export(sub_large_vals)
102+
export(sub_missing)
103+
export(sub_small_vals)
104+
export(sub_zero)
101105
export(summary_rows)
102106
export(tab_footnote)
103107
export(tab_header)

R/data_color.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
#'
129129
#' @family Format Data
130130
#' @section Function ID:
131-
#' 3-18
131+
#' 3-21
132132
#'
133133
#' @import rlang
134134
#' @export

R/dt_formats.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ dt_formats_set <- function(data, formats) {
1212

1313
dt_formats_init <- function(data) {
1414

15-
list() %>%
16-
dt_formats_set(formats = ., data = data)
15+
dt_formats_set(data = data, formats = list())
1716
}
1817

19-
dt_formats_add <- function(data, formats) {
18+
dt_formats_add <- function(data, formats, prepend) {
2019

21-
data %>%
22-
dt_formats_get() %>%
23-
append(
24-
list(formats)
25-
) %>%
26-
dt_formats_set(formats = ., data = data)
20+
formats_list <- dt_formats_get(data = data)
21+
22+
if (prepend) {
23+
formats <- prepend_vec(list(formats), values = formats_list)
24+
} else {
25+
formats <- append(list(formats), values = formats_list)
26+
}
27+
28+
dt_formats_set(data = data, formats = formats)
2729
}
2830

2931
# This function is used in `dt_summary_build()` to get a

R/format_data.R

Lines changed: 26 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,10 +1345,7 @@ fmt_partsper <- function(
13451345
#' accuracy = 10,
13461346
#' simplify = FALSE
13471347
#' ) %>%
1348-
#' fmt_missing(
1349-
#' columns = everything(),
1350-
#' missing_text = ""
1351-
#' ) %>%
1348+
#' sub_missing(missing_text = "") %>%
13521349
#' tab_spanner(
13531350
#' label = "Sold",
13541351
#' columns = contains("sold")
@@ -2913,7 +2910,7 @@ fmt_passthrough <- function(
29132910

29142911
x_str
29152912
},
2916-
latex = function(x) {
2913+
rtf = function(x) {
29172914

29182915
# Create `x_str` with same length as `x`
29192916
x_str <- rep(NA_character_, length(x))
@@ -2949,135 +2946,28 @@ fmt_passthrough <- function(
29492946
)
29502947
}
29512948

2952-
#' Format missing values
2953-
#'
2954-
#' @description
2955-
#' Wherever there is missing data (i.e., `NA` values) a customizable mark may
2956-
#' present better than the standard `NA` text that would otherwise appear. The
2957-
#' `fmt_missing()` function allows for this replacement through its
2958-
#' `missing_text` argument (where an em dash serves as the default).
2959-
#'
2960-
#' @details
2961-
#' Targeting of values is done through `columns` and additionally by `rows` (if
2962-
#' nothing is provided for `rows` then entire columns are selected). Conditional
2963-
#' formatting is possible by providing a conditional expression to the `rows`
2964-
#' argument. See the *Arguments* section for more information on this.
2965-
#'
2966-
#' @inheritParams fmt_number
2967-
#' @param missing_text The text to be used in place of `NA` values in the
2968-
#' rendered table.
2969-
#'
2970-
#' @return An object of class `gt_tbl`.
2971-
#'
2972-
#' @section Examples:
2973-
#'
2974-
#' Use [`exibble`] to create a **gt** table. The `NA` values in different
2975-
#' columns will be given replacement text.
2976-
#'
2977-
#' ```r
2978-
#' exibble %>%
2979-
#' dplyr::select(-row, -group) %>%
2980-
#' gt() %>%
2981-
#' fmt_missing(
2982-
#' columns = 1:2,
2983-
#' missing_text = "missing"
2984-
#' ) %>%
2985-
#' fmt_missing(
2986-
#' columns = 4:7,
2987-
#' missing_text = "nothing"
2988-
#' )
2989-
#' ```
2990-
#'
2991-
#' \if{html}{\out{
2992-
#' `r man_get_image_tag(file = "man_fmt_missing_1.png")`
2993-
#' }}
2994-
#'
2995-
#' @family Format Data
2996-
#' @section Function ID:
2997-
#' 3-15
2998-
#'
2999-
#' @import rlang
3000-
#' @export
3001-
fmt_missing <- function(
3002-
data,
3003-
columns,
3004-
rows = everything(),
3005-
missing_text = "---"
3006-
) {
3007-
3008-
# Perform input object validation
3009-
stop_if_not_gt(data = data)
3010-
3011-
# Pass `data`, `columns`, `rows`, and the formatting
3012-
# functions (as a function list) to `fmt()`
3013-
fmt(
3014-
data = data,
3015-
columns = {{ columns }},
3016-
rows = {{ rows }},
3017-
fns = list(
3018-
html = function(x) {
3019-
3020-
missing_text <-
3021-
context_missing_text(
3022-
missing_text = missing_text,
3023-
context = "html"
3024-
)
3025-
3026-
# Any values of `x` that are `NA` get
3027-
# `missing_text` as output; any values that
3028-
# are not missing get `NA` as their output
3029-
# (meaning, the existing output for that
3030-
# value, if it exists, should be inherited)
3031-
ifelse(is.na(x), missing_text, NA_character_)
3032-
},
3033-
rtf = function(x) {
3034-
3035-
missing_text <-
3036-
context_missing_text(
3037-
missing_text = missing_text,
3038-
context = "rtf"
3039-
)
3040-
3041-
# Any values of `x` that are `NA` get
3042-
# `missing_text` as output; any values that
3043-
# are not missing get `NA` as their output
3044-
# (meaning, the existing output for that
3045-
# value, if it exists, should be inherited)
3046-
ifelse(is.na(x), missing_text, NA_character_)
3047-
},
3048-
default = function(x) {
3049-
3050-
# Any values of `x` that are `NA` get
3051-
# `missing_text` as output; any values that
3052-
# are not missing get `NA` as their output
3053-
# (meaning, the existing output for that
3054-
# value, if it exists, should be inherited)
3055-
ifelse(is.na(x), missing_text, NA_character_)
3056-
}
3057-
)
3058-
)
3059-
}
3060-
30612949
#' Set a column format with a formatter function
30622950
#'
30632951
#' @description
3064-
#' The `fmt()` function provides greater control in formatting raw data values
3065-
#' than any of the specialized `fmt_*()` functions that are available in
3066-
#' **gt**. Along with the `columns` and `rows` arguments that provide some
3067-
#' precision in targeting data cells, the `fns` argument allows you to define
3068-
#' one or more functions for manipulating the raw data.
2952+
#' The `fmt()` function provides a way to execute custom formatting
2953+
#' functionality with raw data values in a way that can consider all output
2954+
#' contexts.
2955+
#'
2956+
#' Along with the `columns` and `rows` arguments that provide some precision in
2957+
#' targeting data cells, the `fns` argument allows you to define one or more
2958+
#' functions for manipulating the raw data.
30692959
#'
30702960
#' If providing a single function to `fns`, the recommended format is in the
30712961
#' form: `fns = function(x) ...`. This single function will format the targeted
30722962
#' data cells the same way regardless of the output format (e.g., HTML, LaTeX,
30732963
#' RTF).
30742964
#'
30752965
#' If you require formatting of `x` that depends on the output format, a list of
3076-
#' functions can be provided for the `html`, `latex`, and `default` contexts.
3077-
#' This can be in the form of `fns = list(html = function(x) ..., latex =
3078-
#' function(x) ..., default = function(x) ...)`. In this multiple-function case,
3079-
#' we recommended including the `default` function as a fallback if all contexts
3080-
#' aren't provided.
2966+
#' functions can be provided for the `html`, `latex`, `rtf`, and `default`
2967+
#' contexts. This can be in the form of `fns = list(html = function(x) ...,
2968+
#' latex = function(x) ..., default = function(x) ...)`. In this
2969+
#' multiple-function case, we recommended including the `default` function as a
2970+
#' fallback if all contexts aren't provided.
30812971
#'
30822972
#' @details
30832973
#' As with all of the `fmt_*()` functions, targeting of values is done through
@@ -3088,6 +2978,9 @@ fmt_missing <- function(
30882978
#'
30892979
#' @inheritParams fmt_number
30902980
#' @param fns Either a single formatting function or a named list of functions.
2981+
#' @param prepend Should the formatting function(s) be brought to the beginning
2982+
#' of the formatting queue (`TRUE`) or placed at the end (`FALSE`). By default,
2983+
#' this is `FALSE` and this leads to 'last-one-wins' semantics.
30912984
#'
30922985
#' @return An object of class `gt_tbl`.
30932986
#'
@@ -3114,21 +3007,22 @@ fmt_missing <- function(
31143007
#'
31153008
#' @family Format Data
31163009
#' @section Function ID:
3117-
#' 3-16
3010+
#' 3-15
31183011
#'
31193012
#' @import rlang
31203013
#' @export
31213014
fmt <- function(
31223015
data,
31233016
columns = everything(),
31243017
rows = everything(),
3125-
fns
3018+
fns,
3019+
prepend = FALSE
31263020
) {
31273021

31283022
# Perform input object validation
31293023
stop_if_not_gt(data = data)
31303024

3131-
# Get the `stub_df` data frame from `data`
3025+
# Get the `stub_df` and `data_tbl` tables from `data`
31323026
stub_df <- dt_stub_df_get(data = data)
31333027
data_tbl <- dt_data_get(data = data)
31343028

@@ -3163,7 +3057,11 @@ fmt <- function(
31633057
rows = resolved_rows_idx
31643058
)
31653059

3166-
dt_formats_add(data = data, formats = formatter_list)
3060+
dt_formats_add(
3061+
data = data,
3062+
formats = formatter_list,
3063+
prepend = prepend
3064+
)
31673065
}
31683066

31693067
#' Insert separator marks to an integer to conform to Indian numbering system

R/helpers.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,7 @@ cells_group <- function(groups = TRUE) {
787787
#' dplyr::filter(!is.na(sza)) %>%
788788
#' tidyr::spread(key = "tst", value = sza) %>%
789789
#' gt(rowname_col = "month") %>%
790-
#' fmt_missing(
791-
#' columns = everything(),
792-
#' missing_text = ""
793-
#' ) %>%
790+
#' sub_missing(missing_text = "") %>%
794791
#' tab_style(
795792
#' style = list(
796793
#' cell_fill(color = "darkblue"),
@@ -2234,7 +2231,7 @@ cell_style_structure <- function(name, obj, subclass = name) {
22342231
#' exibble %>%
22352232
#' dplyr::select(char, time) %>%
22362233
#' gt() %>%
2237-
#' fmt_missing(columns = everything()) %>%
2234+
#' sub_missing() %>%
22382235
#' tab_style(
22392236
#' style = cell_text(
22402237
#' font = c(

R/modify_columns.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ cols_unhide <- function(
10241024
#' merged column (e.g., `NA` + `NA` = `NA`)
10251025
#'
10261026
#' Any resulting `NA` values in the `col_val` column following the merge
1027-
#' operation can be easily formatted using the [fmt_missing()] function.
1027+
#' operation can be easily formatted using the [sub_missing()] function.
10281028
#'
10291029
#' This function is part of a set of four column-merging functions. The other
10301030
#' two are the general [cols_merge()] function and the specialized
@@ -1161,8 +1161,8 @@ cols_merge_uncert <- function(
11611161
#' the merged column
11621162
#'
11631163
#' Any resulting `NA` values in the `col_begin` column following the merge
1164-
#' operation can be easily formatted using the [fmt_missing()] function.
1165-
#' Separate calls of [fmt_missing()] can be used for the `col_begin` and
1164+
#' operation can be easily formatted using the [sub_missing()] function.
1165+
#' Separate calls of [sub_missing()] can be used for the `col_begin` and
11661166
#' `col_end` columns for finer control of the replacement values.
11671167
#'
11681168
#' This function is part of a set of four column-merging functions. The other
@@ -1312,8 +1312,8 @@ cols_merge_resolver <- function(data, col_begin, col_end, sep) {
13121312
#' `"0"` (i.e., no percentage will be shown)
13131313
#'
13141314
#' Any resulting `NA` values in the `col_n` column following the merge
1315-
#' operation can be easily formatted using the [fmt_missing()] function.
1316-
#' Separate calls of [fmt_missing()] can be used for the `col_n` and
1315+
#' operation can be easily formatted using the [sub_missing()] function.
1316+
#' Separate calls of [sub_missing()] can be used for the `col_n` and
13171317
#' `col_pct` columns for finer control of the replacement values. It is the
13181318
#' responsibility of the user to ensure that values are correct in both the
13191319
#' `col_n` and `col_pct` columns (this function neither generates nor

R/opts.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#' ) %>%
6060
#' gt(rowname_col = "tst") %>%
6161
#' tab_spanner_delim(delim = ".") %>%
62-
#' fmt_missing(
62+
#' sub_missing(
6363
#' columns = everything(),
6464
#' missing_text = "90+"
6565
#' ) %>%

0 commit comments

Comments
 (0)