reactable icon indicating copy to clipboard operation
reactable copied to clipboard

how to save as a pdf

Open shangguandong1996 opened this issue 5 years ago • 8 comments

Hi, thanks for this awesome package. I have a problem about the output html. I output the table using this function in R studio 图片

But I want to edit some thing using AI, wich means I have to output as a pdf. So I using the function of chrome 图片 But I found the color disapper

Before 图片

After 图片

So I was wondering whether some solution about this.

Thanks.

shangguandong1996 avatar Dec 08 '19 10:12 shangguandong1996

Here is the code

make_color_pal <- function(colors, bias = 1) {
  get_color <- colorRamp(colors, bias = bias)
  function(x) rgb(get_color(x), maxColorValue = 255)
}

fold_rating_color <- make_color_pal(c("#d64047", "#f8fafc","#3a779e"), bias = 2)
padj_rating_color <- make_color_pal(c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab"), bias = 2)


reactable(reactable_plot,
          columns = list(
  symbol = colDef(name = "symbol",width = 80),
  
  
  TPM_ATAC = colDef(
    cell = function(values){
    sparkline(values, type = "bar",
              chartRangeMin = min(total_plot_data_interest$TPM_ATAC), chartRangeMax = max(total_plot_data_interest$TPM_ATAC))
  },width = 60),
  
  
  TPM_RNA = colDef(
    cell = function(values){
    sparkline(values, type = "bar",chartRangeMin = min(total_plot_data_interest$TPM_RNA), chartRangeMax = max(total_plot_data_interest$TPM_RNA))
  },width = 60),
  
  
  log2FoldChange = colDef(
    name = "log2FoldChange",class = "number",align = "center",width = 80,
    cell = function(value){
      scaled <- (value - min(reactable_plot$log2FoldChange)) / (max(reactable_plot$log2FoldChange) - min(reactable_plot$log2FoldChange))
      color <- fold_rating_color(scaled)
      value <- value <- format(round(value, 2), nsmall = 1)
      div(style = list(background = color), value)
    }),
  
  
  padj = colDef(
    name = "padj",class = "number",align = "center",width = 90,
    cell = function(value) {
      scaled <- (value - min(reactable_plot$padj)) / (max(reactable_plot$padj) - min(reactable_plot$padj))
      color <- padj_rating_color(scaled)
      value <- formatC(paste0("1E-",round(value, 1)))
      div(style = list(background = color),value)
    })
),
defaultPageSize = 100) -> p4

shangguandong1996 avatar Dec 08 '19 10:12 shangguandong1996

Actually, the sprakline plot color still remain, while the div type color disappear 图片 图片

shangguandong1996 avatar Dec 08 '19 11:12 shangguandong1996

I am so sorry to bother you. I finally find a solution 图片 图片

shangguandong1996 avatar Dec 08 '19 11:12 shangguandong1996

Not working for me 😞, even after turning on Background graphics.

Also, I'd like to render it straight off Rmarkdown via knitr if possible; controlling the width is a challenge.

leungi avatar Dec 10 '19 22:12 leungi

I think this may be related to #11

pablobernabeu avatar Dec 14 '19 07:12 pablobernabeu

Indeed it is; I came across that post too.

Thanks for referencing.

leungi avatar Dec 14 '19 18:12 leungi

Yeah, browsers don't print background colors without enabling a browser-specific setting first. In Chrome, it's Print->Background graphics, and in Firefox, it's File->Page Setup->Print Background. And there seem to be other ways to do it, described here: https://stackoverflow.com/questions/14987496/background-color-not-showing-in-print-preview

But even with background graphics enabled, I still couldn't get background colors to print in R Markdown documents. @leungi were you trying to print an R Markdown doc as well? Apparently Bootstrap 3 includes some CSS that removes colors for all elements when printing:

@media print {
  *,
  *:before,
  *:after {
    color: #000 !important;
    text-shadow: none !important;
    background: transparent !important;
    -webkit-box-shadow: none !important;
            box-shadow: none !important;
  }

https://github.com/rstudio/rmarkdown/blob/7f51e232c98b2f7db40b40cd593385fe76b3189b/inst/rmd/h/bootstrap/css/bootstrap.css#L191-L200

There are ways to override and remove the CSS here, but they aren't very easy to do: https://stackoverflow.com/questions/41604798/saving-html-to-pdf-in-chrome/41611312#41611312

So far, the simplest workaround I've found is to force the browser to render the page as screen media. In Chrome, you can do this from the Rendering settings in DevTools: https://developers.google.com/web/tools/chrome-devtools/css/print-preview

Set the CSS media type to "screen": rendering settings in Chrome DevTools

Then colors should show up when printing with Background graphics enabled. Note that this will remove all other print media styles though, so it's still not a perfect workaround.

glin avatar Dec 21 '19 22:12 glin

@glin: thanks for detailed info!

Setting CSS media type to "screen" works 👍; this provides a workaround for current use case of "printing" html output as pseudo-pdf, with similar formatting.

Indeed, I wanted to allow export/download of multiple formats (e.g., docx, pdf) via rmarkdown, but as noted, controlling contents is difficult. My current workaround is recreating table in flextable and export via its sister package officer.

leungi avatar Jan 03 '20 22:01 leungi