tables rmarkdown cellpadding - r

I am trying to make a table using either hwrite or xtable in rmarkdown to produce a html table. I have been unable to use a cellpadding or cellspacing option, this is the code I am using and attached is a photo of the output.
{r, echo = FALSE, results = 'asis'}
cat(hwrite(sd.m.table, row.names = FALSE, cellpadding = 10, row.style = list("font-weight:bold")))
Using xtable() similarly did not work as well. Does anyone have suggestions?
Generated output from code

I'm not familiar with the hwriter package, and I rarely use xtable anymore (because I get frustrated with customizing tables), so I can't give you specific advice on using those.
I'm most comfortable with pixiedust (because I wrote it), but the htmlTable and tableHTML packages produce nice HTML tables and are probably worth looking into as well.
Working example with pixiedust
---
title: "Untitled"
output: html_document
---
```{r}
library(pixiedust)
head(mtcars) %>%
dust() %>%
sprinkle(pad = 10) %>%
medley_all_borders()
```
EDIT:
To get the appearance you are seeking with your data, try using
library(pixiedust)
library(dplyr)
DF <-
data.frame(rep_complete_time = c("2017-01-04 08:58:22",
"2017-01-04 08:58:33",
"2017-01-06 11:35:28"),
result = c(4.99184, 4.07356, 5.01569),
body_fluid = c("Serum", "Serum", "Serum"),
result_type = c("QC", "QC", "QC"),
fluid_lot = c(4426, 4426, 4426),
level = c(1, 1, 1))
DF %>%
# for some reason, sprintf results aren't holding when used in a
# sprinkle. This seems like a bug in pixiedust.
mutate(result = sprintf("%0.1f", result)) %>%
dust() %>%
sprinkle_colnames("Rep Complete Time", "Result", "Body Fluid",
"Result Type", "Fluid Lot", "Level") %>%
# use part = "table" to apply borders to all parts of the table.
# by default, pixiedust only wants to work with the body of the table.
medley_all_borders(part = "table") %>%
# using the sprinkle_table function can apply sprinkles to all of the
# parts of a table.
sprinkle_table(pad = 10,
halign = "left")
I wasn't able to replicate your alignment problem. All of the columns in my results are aligned left.

Related

Scientific formats, subscripts and superscripts in RMarkdown table (docx output)

Let's say I have the following rmd:
---
title: "Table won't work"
author: "Exhausted student"
date: "2022/01/28"
output:
bookdown::word_document2
---
```{r table, echo=F, warning=F, message=F}
library(tidyverse)
a <- tibble(
constants = c("c", "NA", "h", "e", "H2O"),
values = c(2.998e8, 6.022e23, 6.626e-34, -1.602e-19, 18.02)
)
knitr::kable(a, digits = 35)
```
which produces this table in Word.
The Problem
I need the scientific format to use superscripts and multiply sign (i.e. 2.998 × 108), and some cells requires subscript (e.g. NA and H2O).
The final table should look like this. How can I do that?
What I've tried/would never try
huxtable package and its markdown() function: I managed to format some contents as H~2~O, then enable markdown across table by huxtable(a) %>% `markdown<-`(TRUE). Which did not recognize the syntax, and apparently would not work in forseeable future according to the author.
flextable and as_sub(): Produces right format. I pass the lables to flextable::compose(), where the labels were something like as_paragraph(list_values = list("H", as_sub("2"), "O"). The code is obviously too lengthy; plus i have to manipulate cells one-by-one. Technically still doable, but I do have tables with 100+ cells needed formatting.
Output first, format in Word later: Again, needed manipulation one-by-one. Would be an option if it would work everything out automatically.
Convincing the school bureaucrats to accept html/pdf/latex as output: this is forever an impossible option.
Format outside word then export formatted table as image: strictly forbidden in the reports.
Edit: the table works now! Great thanks to Maël's answer, but please check my own findings to see my final result:
You can use tildes (~) to put in subscript and carets (^) for superscripts; and use sprintf to get the expected digit format:
---
title: "Table won't work"
author: "Exhausted student"
date: "2022/01/28"
output:
bookdown::word_document2
---
```{r table, echo=F, warning=F, message=F}
library(tidyverse)
expSup <- function(x, digits=3) {
sprintf(paste0("%05.", digits, "f x 10^%d^"), x/10^floor(log10(abs(x))), floor(log10(abs(x))))
}
a <- tibble(
constants = c("c", "N~A~", "h", "e", "H~2~0"),
values = expSup(c(2.998e8, 6.022e-23, 6.626e-34, -1.602e-19, 18.02))
)
knitr::kable(a)
```
An option that probably falls in the no-go zone for this open issue:
Create a html document,
Insert html tags for subscripts,
Open the html file (not the viewer),
ctrl+c, then ctrl+v in your word file.
---
output: html_document
---
```{r table, echo=F, warning=F, message=F}
library(tidyverse)
library(gt)
a <- tibble(
constants = c("c", "N<sub>A</sub>", "h","e","H<sub>2</sub>O"),
values = c(2.998e8, 6.022e23, 6.626e-34, -1.602e-19, 18.02)
)
a %>%
mutate(constants = map(constants, html)) %>%
gt() %>%
fmt_scientific(values)
The expSup() function in Maël's answer converted the scientific formats into markdown format. For my script, I modified the function a little:
exp_sup <- function(x, digits = 3) {
sprintf(paste0("%05.", digits, "f $\\times$ 10^%d^"), x / 10^floor(log10(abs(x))), floor(log10(abs(x))))
}
I changed "f x 10^%d^" to "f $\\times$ 10^%d^", so that it displays proper multiply symbol (×).
Using the format in flextable
The format works great in Kable. However, a large portion of my workflow requires flextable to make caption/cross reference/publication style/etc. Unfortunately, although the expSup function automatically formats scientific notations into markdowns, it cannot make markdown syntax work in flextable.
However, ftExtra::colformat_md() can. Hence, by combining the modified exp_sup() function with ftExtra, I was finally able to produce an academic-looking table:
Below is the code for my final output; if you are also trying to produce reproducible academic reports with lots of tables in Word format, hope this helps!
---
title: "The tables work!"
author: "Satisfied Student"
date: "2022/01/28"
output:
bookdown::word_document2:
reference_docx: styleRef.docx
---
```{r setup, include = F}
library(easypackages)
packages(
"tidyverse",
"flextable", # This works best for my workflow
"ftExtra", # For markdown formatting work in flextable
"officer" # You can customize appearance/format/etc. of caption *prefixes*
)
knitr::opts_chunk$set(
warning = FALSE,
message = FALSE,
echo = FALSE,
# Make the table caption format definable in reference_docx styles
tab.cap.style = "Table Caption",
# Make "Table 1:" prefixes not bold
tab.cap.fp_text = fp_text_lite(bold = FALSE)
# The tab.cap settings MUST be in a separate chunk from tables
)
# Converts scientific format to markdown
exp_sup <- function(x, digits = 3) {
sprintf(paste0("%05.", digits, "f $\\times$ 10^%d^"), x / 10^floor(log10(abs(x))), floor(log10(abs(x))))
}
# The $\\times$ makes proper multiply symbols
```
```{r table}
a <- tibble(
constants = c("c", "N~A~", "h", "e", "H~2~O"),
values = c(2.998e8, 6.022e23, 6.626e-34, -1.602e-19, 18.02)
)
a %>%
mutate(values = exp_sup(values)) %>%
flextable() %>%
set_caption(
caption = "(ref:foo)", # Produces formatted caption text
style = "Table Caption"
) %>%
colformat_md() %>% # Subscript/superscript works in flextable now!
theme_booktabs() %>% # The 3-part-table used in academics
align(align = "center", part = "all") %>% #Align everything to center
set_table_properties(layout = "autofit") # Comfortable width/height every cell
```
(ref:foo) A scientifically formatted `flextable` with ^superscripts^ and ~subscripts~
Your code should look like this:
```
{r table, echo=F, warning=F, message=F}
library(tidyverse)
a <- tibble(
constants = c("c", "NA", "h", "e", "$H_2O$"),
values = c("$2.998 * {10^{8}}$", "$6.022 * {10^{-23}}$", "$6.626 * {10^{-34}}$", "$-1.602 * {10 ^{-19}}$", "$1.802 * {10^{1}}$")
)
knitr::kable(format = "html", a, digits = 35)
```
Which will give you the output like this:

kableExtra : How can i set to bold the biggest value of the row?

Suppose i have a table that looks like :
x = matrix(runif(10*5),nrow=10,ncol=5)
When i display the matrix using kableextra, i want the highest value, per row, of say the last 2 rows, to be bolded.
I looked at this document https://rdrr.io/cran/kableExtra/f/inst/doc/awesome_table_in_pdf.pdf a lot and i did not found how to use cell_spec correctly to perform this goal.
I thought this would be easier than it turned out to be. As far as I can see, this is how to do it:
---
title: "Untitled"
output: pdf_document
---
```{r}
set.seed(123)
library(knitr)
library(kableExtra)
x <- matrix(round(runif(10*5),2), nrow=10,ncol=5)
j1 <- which.max(x[9,])
j2 <- which.max(x[10,])
col <- seq_len(ncol(x))
x[9,] <- x[9,] %>% cell_spec(bold = col == j1)
x[10,] <- x[10,] %>% cell_spec(bold = col == j2)
x %>% kable(booktabs = TRUE, escape = FALSE)
```
A few notes:
I rounded the values so they aren't so ugly when printed.
I couldn't see a way to do everything in one pipeline, though there probably is one. The trouble is that cell_spec is designed to work on vectors, not matrices.
Finally, the escape = FALSE in kable() is essential: otherwise you'll see the code to make it bold, rather than the bold entry itself.

Wrapping footnote to second line to fit table in kable (LaTeX .Rmd)

I'm wondering if there is a way I can force a footnote to fit the width of a table (and wrap to a second line) using kable in R (I'm knitting .Rmd to PDF so the format is latex). I've used both add_footnote() and footnote(). The add_footnote function will actually wrap a footnote to a second line if it extends beyond the width of the table, but it also forces the use of superscripts (which for this sake is something I can't have in my table). footnote gives me the option to remove the superscript but I'm not sure how to get it to match the formatting of add_footnote and wrap footnotes that are wider than the table to a second line. Another solution would be to remove the superscripts from add_footnote
\captionsetup[table]{labelformat=empty}
```{r packs}
library(pacman)
p_load(tidyverse,knitr,kableExtra,janitor)
mydf <-data_frame(x=1:4,y=2:5,z=3:6)
fn1='This the footnote 1'
fn2='This is footnote 2 and is much longer'
mydf %>%
kable(format='latex',booktabs=T,
col.names=c('X','Y','Z'),
caption=c('This method stretches', 'my table out in an ugly way')) %>%
kable_styling(latex_options = c('hold_position')) %>%
footnote(general=c(fn1, fn2),general_title="")
mydf %>%
kable(format='latex',booktabs=T,
col.names=c('X','Y','Z'),
caption=c('This method ruins my title', 'and left justifies my table')) %>%
kable_styling(latex_options = c('hold_position')) %>%
footnote(general=c(fn1, fn2),general_title="",threeparttable = T)
mydf %>%
kable(format='latex',booktabs=T,
col.names=c('X','Y','Z'),
caption=c('This is pretty close to perfect', 'if I could remove the superscripts')) %>%
kable_styling(latex_options = c('hold_position')) %>%
add_footnote(c(fn1, fn2))
```
A screenshot of the knitted PDF:
setting threeparttable = TRUE as #hao suggested worked for me:
add_footnote(c(fn1, fn2), threeparttable = TRUE)
Below is the notation="none" option #hao suggested. It is center-justified.
mydf %>%
kable(format='latex',booktabs=T,
col.names=c('X','Y','Z'),
caption=c('Notation="none"') %>%
kable_styling(latex_options = c('hold_position')) %>%
add_footnote(c(fn1, fn2), notation="none")

Format Numbers in FlexTable

I am using ReporteRs to generate a flexTable using the following piece of code:
library( ReporteRs )
baseCellProp <- cellProperties( padding = 2 )
baseParProp <- parProperties(text.align = "center")
ft.list <- list(mua= 1, mub = 2, mug = 1e-7)
ft.data <- t(data.frame(ft.list))
ft.FlexTable <- FlexTable( data = ft.data,
add.rownames = TRUE,
header.columns = FALSE,
body.cell.props = baseCellProp,
body.par.props = baseParProp)
ft.FlexTable <- setFlexTableWidths(ft.FlexTable, widths =c(1,2))
ft.FlexTable
The problem I am having is all the numbers are coming out in scientific notation like this:
mua 1e+00
mub 2e+00
mug 1e-07
Is there a way to tell flexTable how to format numbers? Specifically do not apply the same format to all the rows, but apply a format that makes sense on a row-by-row basis?
I solved this problem using flextable in R Markdown document by adding set_formatter_type and controlling how all my double data type are presented. My output was padding everything with lots of zeros that I did not want.
```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(flextable)
library(tidyverse)
results <- as.tibble(read.csv("D:/ALLRESULTS.csv",header=TRUE))
results %>% na.omit() %>% filter(grepl("KEY-K",Alignment)) %>%
arrange(Year,Source) %>%
select(Source,Question,Year,Target,Actual,Highest,n) %>%
regulartable() %>%
set_formatter_type(fmt_double="%.01f") %>%
align(part="header",align="center") %>%
align(align="left") %>% autofit()
```
I solved my problem using a hint from r transposing a data frame. The problem was transposing a data frame frame coerces all the elements to characters. Using matrices solves that problem:
ft.list <- list(mua= 1, mub = 2, mug = 1e-7)
ft.data <- as.matrix(ft.list, rownames.force=TRUE )

How to change fontface (bold/italics) for a cell in a kable table in rmarkdown?

Is there a way to format a single cell in a table in rmarkdown? I am using kable to generate a table as follows:
library(knitr)
kable(data.frame(c('a','b','c'),c(1,2,3)))
I wish to bold "c" in the last row and also add a horizontal line at the end of the table. Any pointers?
This also works: the first argument is row number, so you can bold rows programmatically
or columns using column_spec.
library(kableExtra)
library(tidyverse)
kable(data.frame(letter =c('a','b','c'),number =c(1,2,3)))%>%
kable_styling()%>%
row_spec(3,bold=T,hline_after = T)
Just generalising the question to include other font faces. Pandoc offers other ways to easily reformat text, and as explained in the RMarkdown Cheatsheet:
italics can be achieved using *italics*
bold can be achieved using **bold**
strikethrough can be achieved using ~~strikethrough~~
This output will support the various output methods, including PDF, html and word:
Here is a basic example:
---
output: pdf_document: default
---
```{r}
knitr::kable(data.frame(char = c('*a*','**b**','~~c~~'),
num = c(1,2,3)))
```
Applying formatting with a function
To make it easier to select cells to reformat, I have put together the following function format_cells.
format_cells <- function(df, rows ,cols, value = c("italics", "bold", "strikethrough")){
# select the correct markup
# one * for italics, two ** for bold
map <- setNames(c("*", "**", "~~"), c("italics", "bold", "strikethrough"))
markup <- map[value]
for (r in rows){
for(c in cols){
# Make sure values are not factors
df[[c]] <- as.character( df[[c]])
# Update formatting
df[r, c] <- paste0(markup, df[r, c], markup)
}
}
return(df)
}
It allows the user to select the cell row and columns which need to be reformatted and select the styling to apply. Multiple cells can be selected at the same time if several values in a row/column need to be reformatted. Here is an example:
library(tidyverse)
df <- data.frame(char = c('a','b','c'),
num = c(1,2,3))
df %>%
format_cells(1, 1, "italics") %>%
format_cells(2, 2, "bold") %>%
format_cells(3, 1:2, "strikethrough") %>%
knitr::kable()
Further Reading: the kableExtra package has been written to offer a lot of extra controls of styling of tables. However, the advice is different for the different types of output, and therefore there are different approaches for HTML and LaTeX
Highlighting cells, rows or columns with pander is pretty straightforward:
> df <- data.frame(c('a','b','c'),c(1,2,3))
> emphasize.strong.cells(which(df == 3, arr.ind = TRUE))
> pander(df)
-------------------------------
c..a....b....c.. c.1..2..3.
------------------ ------------
a 1
b 2
c **3**
-------------------------------
But adding horizontal line to the table is out of the scope of markdown table specifications.

Resources