How to refer a figure as a table? - r

I know, this is probably not the wisest idea anyways, but I need to insert a table as figure/screenshot (.png) but refer to it as a table in the caption. Is it possible?
My gaol is basically the same as here with the only difference that I am working in RStudio with rmarkdown, knitr and bookdown. Ideally, the solution should work for both PDF and HTML output (though, PDF is more important for me now).

As a hack, you could create a dummy table that's printed with a miniscule fontsize, just to get the table caption, and then in the same chunk add the actual table image that you want printed. For example:
---
title: Document Title
output:
bookdown::pdf_document2:
toc: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(xtable)
options(xtable.include.rownames=FALSE, xtable.comment=FALSE)
# Dummy table function
dt = function(label, caption=NULL) {
print(xtable(setNames(data.frame(x=numeric()), " "),
caption=caption,
label=paste0("tab:", label)),
hline.after=NULL,
booktabs=FALSE,
size="\\fontsize{0.1pt}{0.1pt}\\selectfont")
}
```
Lorem Ipsum is simply dummy text of the printing and typesetting industry. As you can see, Table \#ref(tab:lab1) shows something. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
```{r, lab1, results="asis", fig.align="center", out.width="6in"}
dt("lab1", "This is table 1")
include_graphics("tab1.png")
```
Now for some more text and then here is Table \#ref(tab:lab2).
```{r, lab2, results="asis", fig.align="center", out.width="4.5in"}
dt("lab2", "This is table 2")
include_graphics("tab2.png")
```
Below, you can see what the output document looks like. As you can see, there some extra space between the caption and the table, due to the small amount of vertical space taken up by the invisible dummy table. Hopefully, some with better knowledge of latex can suggest how to get rid of that space.

I had this same issue and didn't see this question at the time. If you're OK with still having having the horizontal lines at top and bottom, what you can do is make a table that contains nothing but an image of the table you want to display, like this:
```{r echo=F, warning=F}
temp.df <- data.frame(image="![](mytable.png)")
temp.mat <- as.matrix(temp.df)
colnames(temp.mat) <- NULL
knitr::kable(temp.mat, caption="This is my caption")
```
Still a hack but a little less work than the currently accepted answer.

Related

Text wrapping in code sections using Tufte format

Text in the code section of a Tufte formatted, Rmarkdown document does not seem to wrap. I have tried several options and suggestions, but nothing works. I am guessing that all of the other suggestions I could find on StackOverflow did not work, because of the "tufte" format. I am using knitr to construct HTML or PDF documents. Here is the minimal example:
---
title: "Width Minimal Example"
output:
tufte::tufte_handout: default
tufte::tufte_html: default
---
```{r setup, include=FALSE}
library(tufte)
library(formatR)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = TRUE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
```{r, tidy='styler', tidy.opts=list(width.cutoff=50)}
filename <- "/Test/test/test/test/test/test/test/test/test/test/test/test.data"
```
When I use knitr to produce HTML or PDF (handout), the "filename" variable runs out of the textbox allotted for code. I've looked through the "tufte.css" file and nothing there seems to describe the width of text in code boxes.
Ideally, the text in the code box would not only wrap, but indent a bit so that the person reading the code would easily be able to see which lines were new lines and which lines were wrapped lines. Thanks!

How to combine endfloat, markdown formatting and wordwrap in bookdown::pdf_document2 table

I am writing a Rmarkdown document using bookdown::pdf_document2 for which I have a table that needs to do several very specific things, but I simply cannot find the solution to get all of them to work at the same time:
The table needs to float to the end of the document as it would with the latex endfloat package.
The table elements have Markdown bold and italics formatting that should appear correctly in the final PDF document
One of the columns of the table has a lot of text that I would like to wrap within the table cell.
I have tried to get these three options to work with all sorts of combinations of knitr::kable, kableExtra::column_spec and pander, but I cannot find a way to get them all going at once. Below I am pasting an example Rmarkdown document with various tests, none of which fully works.
Is there a simple way to get the table to do what I want? Even a good workaround would be acceptable...
Thanks,
David
---
title: "Test table formatting"
author: "David M. Kaplan"
date: "5/24/2020"
output: bookdown::pdf_document2
header-includes:
- \usepackage[tablesfirst]{endfloat}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
# Data
```{r}
df.long = data.frame(
char = c('*this is some very long text with lots of words that will cause problems with word wrap if it is not properly handled with something like kableExtra*','**b**','~~c~~'),
num = c(1,2,3))
```
# Kable with format=pandoc
```{r}
knitr::kable(df.long,caption="test1",format="pandoc")
```
**Result:** Handles formatting, but table does not float and no wordwrap.
# Kable with booktab
```{r}
knitr::kable(df.long,caption="test2",booktab=TRUE)
```
**Result:** Floats, but does not handle formatting or do wordwrap.
# kableExtra for wordwrap
```{r}
knitr::kable(df.long,caption="test3",booktab=TRUE) %>%
kableExtra::column_spec(1,width="30em")
```
**Result:** Table floats and has wordwrap, but does not handle formatting.
# Pander
```{r}
pander::panderOptions("table.alignment.default","left")
pander::pander(df.long,caption="test4")
```
**Result:** Wordwrap and formatting, but does not float.
I found a solution to this problem. It basically consists of:
In the YAML header, add a header-includes entry declaring longtable to be a float flavor using some LaTeX magic I found here.
Use pander to format the table to get wordwrap
Specifically, I have the following in the YAML header:
header-includes:
- \usepackage[tablesfirst]{endfloat}
- \DeclareDelayedFloatFlavour*{longtable}{table}
and then the table can be generated with pander:
pander::panderOptions("table.alignment.default","left")
pander::pander(df.long,caption="test pander",split.cell=50)

Prevent table being split in Rmarkdown

Is there a way to put my table into one page? I have a table which is broken between two pages. Here is a reproducible example:
---
output: pdf_document
---
`r rep("Text", 400)`
```{r}
# This will create a page break
knitr::kable(mtcars, caption = "A split table")
```
This produces a 2 page pdf with a broken table as follows::
There are two easy ways to do this:
1. Add a page break
If you are only using the PDF output, you can actually integrate LaTeX commands directly into the report. Therefore the \newpage command will force a pagebreak as follows:
---
output: pdf_document
---
`r rep("Text", 400)`
\newpage
```{r}
knitr::kable(mtcars, caption = "A fixed table")
```
2. Use Page Floats:
As RMarkdown uses LaTeX to build the PDF, you can take advantage of the page floats feature. This can take some getting used to, but rather than locking the figure or table in a set position, LaTeX will try and place the figure in a position which it deems "best". With tables, it will try and remove any page breaks.
Here is an example. The table will float to the second page. Make sure to update the YAML to include the header-includes: argument as well:
---
output: pdf_document
header-includes:
- \usepackage{booktabs}
---
`r rep("Text", 400)`
```{r}
knitr::kable(mtcars, format = "latex",
caption = "A caption",
booktabs = TRUE,
longtable = FALSE)
```
`r rep("Text", 200)`
If you look at the output, you will see that the second chunk of text continues on the same page as the first, and that the table is centered on the second page. This is all done automatically by LaTeX and can avoid pain down the road if you add more text before the table which might have previously caused the table to be broken again.
Hope that helps.

Conditionally display block of markdown text using knitr

I would like to edit a single rmarkdown (Rmd) document with a list of "problems", each followed by its solution. Each solution may contain the results of the R console, but also some explaining (markdown and LaTeX formatted) text. Besides, I would like use knitr in 2 versions: with and without the solutions, changing the source as less as possible, and compiling.
I know that I can use a logical variable in order to conditionally evaluate R code and show plots and R output, but I don't know how to show/hide blocks of (markdown and LaTeX) formatted text, unless I put all that text into R character vectors, which seems hard for keeping things clean and readable.
I found this old question,
Conditionally display a block of text in R Markdown
where the solution was given for simple short text, which was included as an argument of the R print() function.
This other old question,
insert portions of a markdown document inside another markdown document using knitr
was for having a father document and child documents which were conditionally compiled, but I don't want to slice my document into so many pieces.
You could use the asis engine to conditionally include/exclude arbitrary text in knitr, e.g.
```{asis, echo=FALSE}
Some arbitrary text.
1. item
2. item
Change echo=TRUE or FALSE to display/hide this chunk.
```
But I just discovered a bug in this engine and fixed it. Unless you use knitr >= 1.11.6, you can create a simple asis engine by yourself, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) paste(options$code, collapse = '\n')
})
```
If you want to include inline R expressions in the text, you will have to knit the text, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) knit_child(text = options$code)
})
```
There is a way to hide parts of the document (including text and chunks): to comment them out with html comment marks.
And comment marks can be generated by R in a block according to a variable that can be set at the beginning of the document.
```{r results='asis', echo=FALSE}
if (hide) {cat("<!---")}
```
```{r results='asis', echo=FALSE}
if (hide) {cat("-->")}
```
And just to show a complete working example, in the example below the middle section of the document can be shown or hidden by setting the hide variable to FALSE or TRUE. That might be useful in case there are several sections to hide or show at once - for example, solutions of course problems.
---
title: "Untitled"
date: "15/10/2020"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
hide <- TRUE #TRUE to comment out part of the document, FALSE to show.
```
## Start
Always shown.
```{r}
hide
```
```{r results='asis', echo=FALSE}
if (hide) {cat("<!---")}
```
## To hide or not to hide
To be hidden or shown according to *hide* variable.
```{r}
"Also to be hidden according to 'hide' variable"
hist(rnorm(10))
```
```{r results='asis', echo=FALSE}
if (hide) {cat("-->")}
```
<!--
Never shown.
-->
## End
Always shown.
Just a caveat: in html output the hidden parts are kept as comments and can be seen just by viewing the source. On the other hand, PDF (LaTex) and Word outputs ignore html comments and the hidden parts aren't included in the knitted documents.
Therefore, when the hidden parts are supposed to be somehow confidential (e.g. exam solutions) PDF or Word output should be used instead of html.
For those looking for a solution when knitting to pdf through LaTex, the answer from #Pere won't work for you (because LaTex doesn't understand the <!--- --> pair as indicating a comment).
Below is one possible workaround:
---
output:
pdf_document
---
\newcommand{\ignore}[1]{}
```{r echo=FALSE}
include <- TRUE
```
```{r results='asis', echo=FALSE}
if(!include){cat("\\ignore{")}
```
Included bla bla
```{r results='asis', echo=FALSE}
if(!include){cat("}")}
```
```{r echo=FALSE}
include <- FALSE
```
```{r results='asis', echo=FALSE}
if(!include){cat("\\ignore{")}
```
NOT Included bla bla
```{r results='asis', echo=FALSE}
if(!include){cat("}")}
```

Rotate a table from R markdown in pdf

I'm writing in R Markdown and have a contingency table that is quite wide. I am converting the R markdown document to a PDF using pandoc.
Is it possible to rotate or shrink the table? Ideally this would be done without having to switch to LaTeX formatting.
My Attempts:
I've been abusing the figure options in knitr to attempt this, but whether I use kable or xtable, I haven't had any luck. Some permutations I have tried include:
```{r out.extra='angle=90', results='asis'}
library(knitr)
kable(iris[1:5,])
```
``{r size='footnotesize', results='asis'}
library(knitr)
kable(iris[1:5,])
```
```{r out.extra='angle=90', results='asis'}
library(xtable)
xtable(iris[1:5,])
```
```{r size='footnotesize', results='asis'}
library(xtable)
xtable(iris[1:5,])
```
All of these show the table nicely, but do not rotate it.
The code I'm using to knit is:
Rscript -e "library(knitr); knit('table.Rmd', 'table.md')"
And to convert to pdf:
pandoc table.md -o table.pdf
The out.extra='angle=90' only works on Figures, and unfortunately not tables. Here are several potential approaches:
KableExtra (Rotate Page)
You can easily rotate tables using the useful addon package kableExtra. Specifically, the landscape() function will put the table on an single landscape page. It’s useful for wide tables that can’t
be printed on a portrait page.
library(kableExtra)
kable(iris[1:5,],
format = "latex", booktabs = TRUE) %>%
kableExtra::landscape()
The limitation of these function is that it does force a new page, so depending on the size of your table it could leave a bit of blank space.
KableExtra (Scale Width)
You can scale the width of the table using the function kable_styling(latex_options = "scale_down"). This will force the table to the width of the page.
kable(iris[1:5,],
format = "latex", booktabs = TRUE) %>%
kable_styling(latex_options = "scale_down")
For more examples of the kableExtra package, check out the package here: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
Stargazer (Rotate Table)
Other options are available, but these largely require the installation of additional LaTeX packages. For example, the stargazer package can print tables in landscape using the float.env argument:
```{r, results="asis"}
stargazer(iris[1:5,],
float.env = "sidewaystable")
```
This requires \usepackage{dcolumn} in LaTeX preamble
Read more about customising your LaTex preamble here: https://tex.stackexchange.com/questions/171711/how-to-include-latex-package-in-r-markdown
You can add some LATEX code in your Rmd file :
\usepackage{lscape}
\usepackage{pdfpages}
Some text on a portrait page.
\newpage
\blandscape
## Title, lorem ipsum
```{r, results = "asis"}
kable(iris[1:5,], caption = "Lorem again")
```
Lorem ipsum...
\elandscape
Some other text on a portrait page.
Can you just use t()?
library(xtable)
xtable(t(iris[1:5,]))
If your table is still to long, split it up into multiple tables. e.g.:
splits = floor(seq(1, ncol(iris), length=5))
for(i in 2:length(splits)){
mini.tab = iris[ , splits[i-1]:splits[i]]
xtable(mini.tab)
}

Resources