1-
21# ' Add HTML-based bar plots into rows of a `gt` table
32# ' @description
43# ' The `gt_plt_bar_pct` function takes an existing `gt_tbl` object and
4544# ' )
4645# ' ```
4746# ' @section Figures:
48- # ' \if{html}{\figure{gt_bar_plot.png}{options: width=100\% }}
47+ # ' \if{html}{\figure{gt_bar_plot.png}{options: width:500px }}
4948# '
5049# ' @family Plotting
5150# ' @section Function ID:
5251# ' 3-5
5352
5453gt_plt_bar_pct <- function (
55- gt_object ,
56- column ,
57- height = 16 ,
58- width = 100 ,
59- fill = " purple" ,
60- background = " #e1e1e1" ,
61- scaled = FALSE ,
62- labels = FALSE ,
63- label_cutoff = 0.40 ,
64- decimals = 1 ,
65- font_style = " bold" ,
66- font_size = " 10px" ) {
67-
68-
69- stopifnot(`'gt_object' must be a 'gt_tbl', have you accidentally passed raw data?` = " gt_tbl" %in%
70- class(gt_object ))
54+ gt_object ,
55+ column ,
56+ height = 16 ,
57+ width = 100 ,
58+ fill = " purple" ,
59+ background = " #e1e1e1" ,
60+ scaled = FALSE ,
61+ labels = FALSE ,
62+ label_cutoff = 0.40 ,
63+ decimals = 1 ,
64+ font_style = " bold" ,
65+ font_size = " 10px"
66+ ) {
67+ stopifnot(
68+ `'gt_object' must be a 'gt_tbl', have you accidentally passed raw data?` = " gt_tbl" %in%
69+ class(gt_object )
70+ )
7171
72- stopifnot(' label_cutoff must be a number between 0 and 1' = dplyr :: between(label_cutoff , 0 , 1 ))
72+ stopifnot(
73+ ' label_cutoff must be a number between 0 and 1' = dplyr :: between(
74+ label_cutoff ,
75+ 0 ,
76+ 1
77+ )
78+ )
7379
7480 # ensure font_style is one of the accepted values
7581 stopifnot(
76- ' `font_style` argument must be "bold", "normal", or "italic"' =
77- font_style %in% c(" bold" , " normal" , " italic" )
78- )
82+ ' `font_style` argument must be "bold", "normal", or "italic"' = font_style %in%
83+ c(" bold" , " normal" , " italic" )
84+ )
7985
8086 all_cols <- gt_index(gt_object , column = {{ column }}, as_vector = FALSE )
8187
@@ -89,24 +95,22 @@ gt_plt_bar_pct <- function(
8995 col_to_widen <- rlang :: new_formula(col_name , px(width ))
9096
9197 bar_plt_html <- function (xy ) {
92-
9398 if (length(na.omit(xy )) == 0 ) {
9499 max_x <- 0
95100 } else {
96101 max_x <- max(as.double(xy ), na.rm = TRUE )
97102 }
98103
99104 bar <- lapply(data_in , function (x ) {
100-
101- scaled_value <- if (isFALSE(scaled )) {
105+ scaled_value <- if (isFALSE(scaled )) {
102106 x / max_x * 100
103107 } else {
104108 x
105109 }
106110
107111 if (labels ) {
108112 # adjust values for labeling // scale_label
109- label_values <- if (scaled ) {
113+ label_values <- if (scaled ) {
110114 x
111115 } else {
112116 x / max_x * 100
@@ -116,70 +120,102 @@ gt_plt_bar_pct <- function(
116120 label <- glue :: glue(" {round(label_values, decimals)}%" )
117121
118122 if (x < (label_cutoff * max_x )) {
119-
120123 css_styles <- paste0(
121- " background:" , fill ," ;" ,
122- " width:" , scaled_value , " %;" ,
123- " height:" , height , " px;" ,
124+ " background:" ,
125+ fill ,
126+ " ;" ,
127+ " width:" ,
128+ scaled_value ,
129+ " %;" ,
130+ " height:" ,
131+ height ,
132+ " px;" ,
124133 " display:flex;" ,
125134 " align-items:center;" ,
126135 " justify-content:center;" ,
127- " color:" , ideal_fgnd_color(background )," ;" ,
128- " font-weight:" , font_style ," ;" ,
129- " font-size:" , font_size , " ;" ,
136+ " color:" ,
137+ ideal_fgnd_color(background ),
138+ " ;" ,
139+ " font-weight:" ,
140+ font_style ,
141+ " ;" ,
142+ " font-size:" ,
143+ font_size ,
144+ " ;" ,
130145 " position:relative;"
131146 )
132147
133148 span_styles <- paste0(
134- " color:" , ideal_fgnd_color(background )," ;" ,
149+ " color:" ,
150+ ideal_fgnd_color(background ),
151+ " ;" ,
135152 " position:absolute;" ,
136153 " left:0%;" ,
137- " margin-left:" , scaled_value * width / 100 , " px;" ,
138- " font-weight:" , font_style ," ;" ,
139- " font-size:" , font_size ," ;"
154+ " margin-left:" ,
155+ scaled_value * width / 100 ,
156+ " px;" ,
157+ " font-weight:" ,
158+ font_style ,
159+ " ;" ,
160+ " font-size:" ,
161+ font_size ,
162+ " ;"
140163 )
141164
142165 glue :: glue(
143166 " <div style='{css_styles}'>" ,
144167 " <span style='{span_styles}'>{label}</span></div>"
145168 )
146169 } else {
147-
148170 css_styles <- paste0(
149- " background:" , fill ," ;" ,
150- " width:" , scaled_value , " %;" ,
151- " height:" , height , " px;" ,
171+ " background:" ,
172+ fill ,
173+ " ;" ,
174+ " width:" ,
175+ scaled_value ,
176+ " %;" ,
177+ " height:" ,
178+ height ,
179+ " px;" ,
152180 " display:flex;" ,
153181 " align-items:center;" ,
154182 " justify-content:flex-start;" ,
155183 " position:relative;"
156184 )
157185
158186 span_styles <- paste0(
159- " color:" , ideal_fgnd_color(fill )," ;" ,
187+ " color:" ,
188+ ideal_fgnd_color(fill ),
189+ " ;" ,
160190 " position:absolute;" ,
161191 " left:0px;" ,
162192 " margin-left:5px;" ,
163- " font-weight:" , font_style ," ;" ,
164- " font-size:" , font_size ," ;"
193+ " font-weight:" ,
194+ font_style ,
195+ " ;" ,
196+ " font-size:" ,
197+ font_size ,
198+ " ;"
165199 )
166200
167201 glue :: glue(
168202 " <div style='{css_styles}'>" ,
169203 " <span style='{span_styles}'>{label}</span></div>"
170204 )
171205 }
172- } else if (! is.na(x )) {
206+ } else if (! is.na(x )) {
173207 glue :: glue(
174208 " <div style='background:{fill};width:{scaled_value}%;height:{height}px;'></div>" # no labels added
175209 )
176- } else if (is.na(x )){
210+ } else if (is.na(x )) {
177211 " <div style='background:transparent;width:0%;height:{height}px;'></div>" # no labels added
178212 }
179213 })
180214
181215 chart <- lapply(bar , function (bar ) {
182- glue :: glue(" <div style='flex-grow:1;margin-left:8px;background:{background};'>{bar}</div>" )
216+ glue :: glue(
217+ " <div style='flex-grow:1;margin-left:8px;background:{background};'>{bar}</div>"
218+ )
183219 })
184220
185221 chart
@@ -192,11 +228,13 @@ gt_plt_bar_pct <- function(
192228 invisible (force(x ))
193229 }
194230
195- quiet(gt_object %> %
196- cols_width(col_to_widen ) %> %
197- text_transform(
198- locations = cells_body(columns = {{ column }}),
199- fn = quiet(bar_plt_html )
200- ) %> %
201- cols_align(align = " left" , columns = {{ column }}))
231+ quiet(
232+ gt_object %> %
233+ cols_width(col_to_widen ) %> %
234+ text_transform(
235+ locations = cells_body(columns = {{ column }}),
236+ fn = quiet(bar_plt_html )
237+ ) %> %
238+ cols_align(align = " left" , columns = {{ column }})
239+ )
202240}
0 commit comments