@@ -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
31213014fmt <- 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
0 commit comments