-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathshiny.R
332 lines (313 loc) · 9.46 KB
/
shiny.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#------------------------------------------------------------------------------#
#
# /$$
# | $$
# /$$$$$$ /$$$$$$
# /$$__ $$|_ $$_/
# | $$ \ $$ | $$
# | $$ | $$ | $$ /$$
# | $$$$$$$ | $$$$/
# \____ $$ \___/
# /$$ \ $$
# | $$$$$$/
# \______/
#
# This file is part of the 'rstudio/gt' project.
#
# Copyright (c) 2018-2025 gt authors
#
# For full copyright and license information, please look at
# https://gt.rstudio.com/LICENSE.html
#
#------------------------------------------------------------------------------#
#nocov start
# render_gt() ------------------------------------------------------------------
#' A **gt** display table render function for use in Shiny
#'
#' @description
#'
#' With `render_gt()` we can create a reactive **gt** table that works
#' wonderfully once assigned to an output slot (with [gt_output()]). This
#' function is to be used within [Shiny](https://shiny.posit.co)'s `server()`
#' component. We have some options for controlling the size of the container
#' holding the **gt** table. The `width` and `height` arguments allow for sizing
#' the container, and the `align` argument allows us to align the table within
#' the container (some other fine-grained options for positioning are available
#' in [tab_options()]). If the table is interactive, the selected row indices
#' (relative to the underlying data, regardless of sorting) are available as
#' `input$id`, where `id` is the `outputId` used for this table in [gt_output()].
#' If the user has deselected all rows, the value is `0` (vs `NULL` when the
#' table initializes).
#'
#' @param expr *Expression*
#'
#' `<expression>|obj:<data.frame>|obj:<tbl_df>`
#'
#' An expression that creates a **gt** table object. For sake of convenience,
#' a data frame or tibble can be used here (it will be automatically
#' introduced to [gt()] with its default options).
#'
#' @param width,height *Dimensions of table container*
#'
#' `scalar<numeric|integer|character>` // *default:* `NULL` (`optional`)
#'
#' The width and height of the table's container. Either can be specified as a
#' single-length character vector with units of pixels or as a percentage. If
#' provided as a single-length numeric vector, it is assumed that the value is
#' given in units of pixels. The [px()] and [pct()] helper functions can also
#' be used to pass in numeric values and obtain values as pixel or percent
#' units.
#'
#' @param align *Table alignment*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
#'
#' The alignment of the table in its container. If `NULL`, the table will be
#' center-aligned. Valid options for this are: `"center"`, `"left"`, and
#' `"right"`.
#'
#' @param env *Evaluation environment*
#'
#' `<environment>` // *default:* `parent.frame()`
#'
#' The environment in which to evaluate the `expr`.
#'
#' @param quoted *Option to `quote()` `expr`*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' Is `expr` a quoted expression (with `quote()`)? This is useful if you want
#' to save an expression in a variable.
#'
#' @param outputArgs *Output arguments*
#'
#' `list` // *default:* `list()`
#'
#' A list of arguments to be passed through to the implicit call to
#' [gt_output()] when `render_gt()` is used in an interactive R Markdown
#' document.
#'
#' @return An object of class `shiny.render.function`.
#'
#' @section Examples:
#'
#' Here is a Shiny app (contained within a single file) that (1) prepares a
#' **gt** table, (2) sets up the `ui` with [gt_output()], and (3) sets up the
#' `server` with a `render_gt()` that uses the `gt_tbl` object as the input
#' expression.
#'
#' ```r
#' library(shiny)
#'
#' gt_tbl <-
#' gtcars |>
#' gt() |>
#' fmt_currency(columns = msrp, decimals = 0) |>
#' cols_hide(columns = -c(mfr, model, year, mpg_c, msrp)) |>
#' cols_label_with(columns = everything(), fn = toupper) |>
#' data_color(columns = msrp, method = "numeric", palette = "viridis") |>
#' sub_missing() |>
#' opt_interactive(use_compact_mode = TRUE)
#'
#' ui <- fluidPage(
#' gt_output(outputId = "table")
#' )
#'
#' server <- function(input, output, session) {
#' output$table <- render_gt(expr = gt_tbl)
#' }
#'
#' shinyApp(ui = ui, server = server)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_render_gt_1.png")`
#' }}
#'
#' @family Shiny functions
#' @section Function ID:
#' 12-1
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @export
render_gt <- function(
expr,
width = NULL,
height = NULL,
align = NULL,
env = parent.frame(),
quoted = FALSE,
outputArgs = list()
) {
# Ensure that the shiny package is available
rlang::check_installed("shiny", "to use `render_gt()`.")
# Install the expression as a function
func <-
shiny::installExprFunction(
expr = expr,
name = "func",
eval.env = env,
quoted = quoted
)
shiny::createRenderFunction(
func = func,
function(result, shinysession, name, ...) {
if (is.null(result)) {
return(NULL)
}
# If the `expr` is an object that doesn't inherit from `gt_tbl`,
# simply use `gt()` with no options to create the gt table
if (!inherits(result, "gt_tbl")) {
result <- gt(result)
}
# Modify some gt table options via `tab_options()`
result <-
tab_options(
result,
container.width = width,
container.height = height,
table.align = align
)
html_tbl <- htmltools::tagList(
as.tags(result),
shiny_deps(),
initialize_shiny_gt(name)
)
dependencies <-
lapply(
htmltools::resolveDependencies(htmltools::findDependencies(html_tbl)),
shiny::createWebDependency
)
names(dependencies) <- NULL
list(
html = htmltools::doRenderTags(html_tbl),
deps = dependencies
)
},
gt_output, outputArgs
)
}
shiny_deps <- function() {
htmltools::htmlDependency(
"gtShiny",
"1.0.0",
src = "shiny",
package = "gt",
script = "gtShiny.js"
)
}
initialize_shiny_gt <- function(id) {
htmltools::HTML(
glue::glue("<script>gtShinyBinding.initialize('{id}');</script>")
)
}
# gt_output() ------------------------------------------------------------------
#' Create a **gt** display table output element for Shiny
#'
#' @description
#'
#' Using `gt_output()` we can render a reactive **gt** table, a process
#' initiated by using [render_gt()] in the `server` component of a
#' Shiny app. `gt_output()` is to be used in the [Shiny](https://shiny.posit.co)
#' `ui` component, the position and context wherein this call is made determines
#' the where the **gt** table is rendered on the app page. It's important to
#' note that the ID given during [render_gt()] is needed as the `outputId` in
#' `gt_output()` (e.g., **server**: `output$<id> <- render_gt(...)`; **ui**:
#' `gt_output(outputId = "<id>")`).
#'
#' @param outputId *Shiny output ID*
#'
#' `scalar<character>` // **required**
#'
#' An output variable from which to read the table.
#'
#' @return An object of class `shiny.tag`.
#'
#' @section Examples:
#'
#' Here is a Shiny app (contained within a single file) that (1) prepares a
#' **gt** table, (2) sets up the `ui` with `gt_output()`, and (3) sets up the
#' `server` with a [render_gt()] that uses the `gt_tbl` object as the input
#' expression.
#'
#' ```r
#' library(shiny)
#'
#' gt_tbl <-
#' gtcars |>
#' gt() |>
#' fmt_currency(columns = msrp, decimals = 0) |>
#' cols_hide(columns = -c(mfr, model, year, mpg_c, msrp)) |>
#' cols_label_with(columns = everything(), fn = toupper) |>
#' data_color(columns = msrp, method = "numeric", palette = "viridis") |>
#' sub_missing() |>
#' opt_interactive(use_compact_mode = TRUE)
#'
#' ui <- fluidPage(
#' gt_output(outputId = "table")
#' )
#'
#' server <- function(input, output, session) {
#' output$table <- render_gt(expr = gt_tbl)
#' }
#'
#' shinyApp(ui = ui, server = server)
#' ```
#'
#' \if{html}{\out{
#' `r man_get_image_tag(file = "man_render_gt_1.png")`
#' }}
#'
#' @family Shiny functions
#' @section Function ID:
#' 12-2
#'
#' @section Function Introduced:
#' `v0.2.0.5` (March 31, 2020)
#'
#' @export
gt_output <- function(outputId) {
# Ensure that the shiny package is available
rlang::check_installed("shiny", "to use `gt_output()`.")
shiny::htmlOutput(outputId, class = "gt_shiny")
}
# gt_update_select() -----------------------------------------------------------
#' Update a **gt** selection in Shiny
#'
#' @description
#'
#' Update the selection in an interactive **gt** table rendered using
#' [render_gt()]. The table must be interactive and have selection enabled (see
#' [opt_interactive()]).
#'
#' @param outputId *Shiny output ID*
#'
#' `scalar<character>` // **required**
#'
#' The id of the [gt_output()] element to update.
#' @param rows *Row indices*
#'
#' `<integer>` // **required**
#'
#' The id of the [gt_output()] element to update.
#' @param session *Shiny session*
#'
#' `scalar<ShinySession>` // **required**
#'
#' The session in which the [gt_output()] element can be found. You almost
#' certainly want to leave this as the default value.
#'
#' @return A call to the JavaScript binding of the table.
#' @family Shiny functions
#' @section Function ID:
#' 12-3
#'
#' @export
gt_update_select <- function(outputId,
rows,
session = shiny::getDefaultReactiveDomain()) {
session$sendInputMessage(outputId, rows - 1)
}
#nocov end