1

I have multitple DataTables which I need to print in a PDF Document. HTML Document works as it retains formatting.

    artistTable1.data <- reactive({
         df <- artistData.filtered()
         df <- df[c("old_level", "level", "reason", "rank", "apacDominant")]
         # names(df) <- c("ARTIST (click for wikipedia)","LEVEL", "REASON", paste0("RANK (of ", my.sum(artistData$hasLevel), " ranked artists)"), "RISK FLAG(S)")

         datatable(df, filter = "none", escape = FALSE,
                   rownames = FALSE, caption = "HIGH LEVEL DATA SUMMARY",
                   options = list(columnDefs=list(list(targets = 0:3, class="dt-center")),
                                  paging = FALSE, searching = FALSE, autoWidth = FALSE, lengthChange = FALSE, info = FALSE, ordering = FALSE),
                   class = 'cell-border hover compact')
    })

    output$artistTable1 <- renderDataTable({
         artistTable1.data()
    })

I'm aware that Datatables (reactive) can't be used in Static PDF Documents. Webshot was a fix but it ruins the formatting.

I have an RMarkdown file which is generates the reports in PDF and HTML for me. The knitr package uses "webshot" but doesn't retain the formating of the HTML Document. Can i build this into the RMarkdown?

Are there any alternatives?

1 Answers1

0

Your code doesn't run out of the box for me since you haven't included the needed libraries. Since this is a general DataTable question I'll use a an DateTable generated in the help page (see ?data.table).

One way to do this is using knitr. Below is a minimal workable example which you can save in a text file e.g. dt.Rnw:

\documentclass{article}
\begin{document}
<<dt,results="asis">>=
library(data.table)
library(xtable)
DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
xtable(DT,caption="cap")
@
\end{document}

This embeds R code in a latex environment. xtable converts the data.dable object DT into a latex table and prints it which is then included in the document as it is (result="asis").

Having saved this file one has to run knitr to create dt.tex, for example:

Rscript -e 'library(knitr);knit("dt.Rnw")'

Which then can then be compiled into a pdf (dt.pdf) with a nicely formatted table in it (obviously this can be extended to host multiple tables):

pdflatex dt.tex
  • Thanks Sebastian! I have an RMarkdown file which is generates the reports in PDF and HTML for me. The knitr package uses webshot[link](https://github.com/wch/webshot) but doesn't retain the formating of the HTML Document. Can i build this into the RMarkdown? Thanks for the patience, i'm an amateur Shiny developer – Saatvik Ramisetty Sep 12 '17 at 15:02
  • I suppose that is a different question, which seems to be addresed in this [thread](https://stackoverflow.com/questions/11025123/how-to-convert-r-markdown-to-pdf). Seems like [pandoc](http://pandoc.org/) is your friend. – Sebastian Müller Sep 12 '17 at 15:26