R - combine two gt objects in a single page output - r

In our production environment, we use gtsummary package to summarize large data, then convert to gt object to add title and subtitle. The visual quality of the resulting table is unparalleled.
However, in many instances we need this summary table accompanied by a companion data table that carries detail of a handful of outliers or similar specifics that clarify the overall. In a sense, this companion data is a table-based footnote, or clarifying support information.
Because we emit large numbers of these summaries, I am looking for a way to keep them together in the same emitted single-page pdf.
I've tried to solve this with the following:
tbl_merge, tbl_stack in gtsummary: gtsummary tables, not gt objects, requires shared rows or columns.
gridExtra: gt objects cannot be converted to grobs
pdf() or print to device: gtsave or print(gt) does not output to device.
Does anyone know if there is there a way to embed one table as a png object in the footnote of the other? Or is there another alternative?

Found a really clean solution to this problem using the gt package. We needed to avoid converting tables into images, which strips the content of much functionality including machine search.
The solution: Tranform the supplemental table into html using as_raw_html(), then insert into the primary table as source_note using tab_source_note(source_note = html(suppTable).
This retains all formatting, including titles and footnotes for each of the tables.

Related

Easily view/browse free-text datasets in R/RStudio - alternatives to View() in R/RStudio?

When working with survey data (10-30 columns, 100 - 10k rows, mix of demographic columns like name, age etc, and free-text responses up to nchar == 3000), View() isn't so useful, because it only displays the first 50 or so characters of lengthy strings (we can always widen the column, but this has practical limitations). AFAIK, increasing row height is not possible. So it is not easy to view free text inside RStudio unless it's in the console, which is not necessarily designed for easily browsing through columns of long strings.
Is there any function like View() that displays data similarly but allows for resizing of row heights (to display >1 line of long strings), and perhaps some smarts to allow us to explore list columns in data.frames?
One idea is a function that takes a data.frame argument, writes it as a temp file, and starts a shiny app that displays the data. But something in native R (or built into RStudio) would probably be better than an ad hoc shiny app.
Note: I do know how to achieve this in markdown using kableextra and similar packages that make nice bootstrap tables. However, the goal is to reduce friction between coding in the script pane in RStudio and exploring the data, and I feel like moving code into an Rmd has potential but creates extra friction
DT::datatable() provides the ability to view raw data in tables using all the features of the Javascript DataTables library either in RStudio's viewer tab or separately on the browser of your choice. You can further finetune the display of your data to fit your needs using any of the features provided here: https://rstudio.github.io/DT/

A way in Rmarkdown to copy specific ranges in excel and paste them as images?

I have several formatted tables (and some graphs) in a single excel book, and I want to use Rmarkdown to grab those tables and graphs and add them to the knitted word document as images. I cannot simply copy the cell range with Readxl or other packages because all cells lose their formatting, and I need the tables in the document to look exactly the same as in the spreadsheet.
Alternatively, I am considering recreating the tables in R, but again the new tables will not have the formatting (though I imagine kable could allow me to recreate all the formatting). This is certainly not ideal, however, since I'm creating the tables twice, so hopefully the copy ability is doable somehow. I know Rmarkdown can use Python, Javascript, and many other languages, so maybe one of those languages is capable of this.
Is there a way to do this?

How to make a nice looking table in base r (not markdown)

I’ve been looking for an hour, but everything I can find about how to make a nice looking table out of a data frame mentions that it’s for rmarkdown, html, or latex.
Is it not possible to make a nice looking table in base r?
plot(x, y) makes a graph.
Is there no function like: printTable(df)?
Broadly speaking over what you can get from a normal print in base::print there is not much else you can do. You could try to twist plot function to plot values from selected cells in a data frame but that would be very onerous to develop and impractical in the light of currently available and maintained solutions. There is a number of packages that let you achieve what you need. For instance you can try formattable by renkun-ken.
Example
For a simple example you can try formattable::formattable(mtcars[1:10,])
Creating Images
For a solution creating images from tables, have a look at this discussion. As discussed, in the linked answer if you insist on generating a static image you can use grid.table function offered via gridExtra: tbl <- grid.table(mtcars[1:5,]).
You may be interested in the flextable package that is very easy to use with multiple options to create nice tables.
You can also have multiple word, pdf, or html output types.
I invite you to check the manual : https://cran.r-project.org/web/packages/flextable/vignettes/overview.html

Get the count of tables embedded in the PDF

Following code extracts the tables from PDF.
install.packages("tabulizer"); install.packages("tidyverse")
library(tabulizer); library(tidyverse)
n_tables <- extract_tables("filename.pdf") %>% length()
However, it takes forever to do this. Can we bypass the actual table extraction step, presumably a very time consuming process, and get the count of tables from pdfs directly using tabulizer or any other R package?
Original tabulizer developer here: Nope. The algorithm works page-by-page, identifying the tables and extracting them. The extraction per se is not expensive - it's the identification that's time-consuming.
The reason the package - and the underlying Tabula Java library - exists at all is because there is no internal representation of a "table" in the PDF specification, unlike in say HTML or docx. Tables in a PDF are just arrangements of glyphs in something that looks to the human eye like a table. Thus there's no way to quickly query for the presence of a table or a list of all tables as no such list exists in the file.
So short, disappointing answer: nope.

Suggestion for R/LaTeX table creation package

I've been using xtable package for a long time, and looking forward to writting my first package in R... so I reckon that if I have some "cool" idea that's worth carying out, there's a great chance that somebody got there before me... =)
I'm interested in functions/packages specialized for LaTeX table creation (through R, of course). I bumped on quantreg package which has latex.table function. Any suggestion for similar function(s)/package(s)?
P.S.
I'm thinking about building a webapp in which users can define their own presets/templates of tables, choose style, statistics, etc. It's an early thought, though... =)
I sometimes divide the task of creating LaTeX tables into two parts:
I'll write the table environment, caption, and tabular environment commands directly in my LaTeX document.
I'll export just the body of the table from R using a custom function.
The R export part involves several steps:
Starting with a matrix of the whole table including any headings:
Add any LaTeX specific formatting to the table. E.g., enclose digits in dollar symbols to ensure that negative numbers display correctly.
Collapse rows into a single character value by replacing separate columns with the ampersand (&) and adding ends-of-row symbols "\\"
Add any horizontal lines to be displayed in the table. I use the booktabs LaTeX package.
Export the resulting character vector using the write function
The exported text file is then imported using the input command in LaTeX. I ensure that the file name corresponds to the table label.
I have used this approach in the context of writing journal articles.
In these cases, there are a lot of different types of tables (e.g., multi-page tables, landscape tables, tables requiring extended margins, tables requiring particular alignment, tables where I want to change the wording of the table title). In this setting, I've mostly found it easier to just export the data from R. In this way, the result is reproducible research, but it is easier to tweak aspects of table design in the LaTeX document. And in the context of journal articles, there are usually not too many tables and rather specific formatting requirements.
However, I imagine if I were producing large numbers of batch reports, I'd consider exporting more aspects directly from R.
Beyond xtable and Hmisc as listed by Rob, there are also at least
apsrtable which formats latex tables from one or more model objects
p2lh which exports R to LaTeX and HTML
RcmdrPlugin.Export which graphically exports output to LaTeX or HTML
reporttools which generates LaTeX tables of descriptive statistics
This was just based on a quick search. So there may be more for you to look at before you try to hook it into a webapp. Good luck.
In addition to the packages mentioned above, there is the stargazer package. It works well with objects from many commonly used functions and packages (lm, glm, svyglm, plm, survival, AER, pscl, and others), as well as with zelig objects.
Apart from xtable, there's the latex function in the Hmisc package.

Resources