Gt: Have underlying data for an HTML table be downloadable

Created on 4 Oct 2018  Â·  4Comments  Â·  Source: rstudio/gt

For reproducibility, we'd want to have source data available with an HTML table by default. This includes the input data and also the code required to generate the table. The user could opt out of including any one of these (or both).

For HTML, the following files should made available through links somewhere in the displayed table:

  • raw CSV of the table (data_df)
  • gt code required to produce the table

To do this, have an extra internal attribute that collects all gt statements. At render time, the statements will

  • be formatted as a pipeline
  • always referring to input data as data

Difficulties will come in when users supply their own custom functions or values that can not be immediately captured. The choice will have to be made as to how far the inspection code will traverse to have full reproducibility.

Proof of concept:

z <- 1
f <- function(x) x + z
key <- function(x, b) {
  x + f(b)
}

input_to_string <- function(x) {
  conn <- textConnection("list_to_string", "w")
  on.exit({close(conn)})
  dput(x, file = conn)
  paste0(textConnectionValue(conn), collapse = "\n")
}

input_to_string(a)
#> [1] "function (x, b) \n{\n    x + f(b)\n}"

datadr::drGetGlobals(key)
#> $vars
#> $vars$f
#> function (x) 
#> x + z
#> 
#> $vars$z
#> [1] 1
#> 
#> 
#> $packages
#> [1] "base"

Created on 2018-10-04 by the reprex package (v0.2.1)

Result could be something like...

some_gt_function(
  key = local({
    z <- 1
    f <- function(x) x + 1
    function(x, b) {
      x + f(b)
    }
  })
)

Reproducibility could be tested by calling the captured code and comparing the initial table with the reproduced table.

[3] Advanced [3] High [2] Medium ★ Enhancement

Most helpful comment

From issue https://github.com/rstudio/gt/issues/148 by @fmmattioni:

Any chance of having HTML export buttons as in the following link?

https://datatables.net/extensions/buttons/examples/html5/simple.html

I currently use the DT package for a shiny app, and I would like to move to gt as it is much easier to use. However, I would need those export buttons.

Thanks in advance!

All 4 comments

@lionel- is there a way to extract all variable values of an object (similar to what's done above, which is taken from https://github.com/delta-rho/datadr/blob/master/R/globals.R)?

@schloerke

r-lib has the carrier package but it currently requires the user to be explicit about the variables pulled in crated functions. Would that be an option? Otherwise you could use the globals or the defer packages.

I wouldn't use text deparsing to serialise objects though, deparsed code cannot contain dynamic objects like environments. You should use serialize() instead (whose biggest limitation is that it won't serialise anything defined on the search path / global env).

From issue https://github.com/rstudio/gt/issues/148 by @fmmattioni:

Any chance of having HTML export buttons as in the following link?

https://datatables.net/extensions/buttons/examples/html5/simple.html

I currently use the DT package for a shiny app, and I would like to move to gt as it is much easier to use. However, I would need those export buttons.

Thanks in advance!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vincentarelbundock picture vincentarelbundock  Â·  4Comments

jenniferp96 picture jenniferp96  Â·  4Comments

jthomasmock picture jthomasmock  Â·  3Comments

DickStartz picture DickStartz  Â·  4Comments

higgi13425 picture higgi13425  Â·  3Comments