common heading in r data.frame - r

I want to add a header to grouped data in my data.frame structured dataset. please, see the input and output. Input;`
data<-data.frame(A=c(1,2,3,4), B=c(7,8,9,10), C=c(11,12,13,14), D=c(15,16,17,18))
Output

If you want this for display purposes there are few packages which can help you.
For example, with knitr and kableExtra.
library(knitr)
library(kableExtra)
kable(data) %>%
kable_styling(
full_width = FALSE,
bootstrap_options = c("striped", "hover", "condensed"),
) %>%
add_header_above(c(Group1 = 2, Group2 = 2))

Related

How to change appearance of specific columns of two tables listed side by side (r markdown)

I've got two tables with identical variables but different observations and I've formatted them to sit next to each other side by side to give the appearance of one table using,
knitr::kable(list(df1, df2))
(Apologies I can't show an image as data is confidential, ask me to elaborate further if I'm not making sense)
And I want to make the first column of the first table bold and I tried using
knitr::kable(list(df1, df2)) %>%
kable_paper("striped", full_width = F) %>%
column_spec(1, bold = T)
to do this but that just made the entire first table bold.
Bare with me I'm very new to rmarkdown, any help would be greatly appreciated
Here is one way if you are trying to knit the file in HTML.
---
title: "temp"
output: html_document
---
```{r, echo = FALSE, warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
df1 <- data.frame(col1 = letters[1:5], col2 = rnorm(5))
df2 <- data.frame(col1 = letters[11:15], col2 = rnorm(5))
kable(df1) %>%
kable_paper("striped", full_width = FALSE) %>%
kable_styling(full_width = FALSE, position = "float_left") %>%
column_spec(1, bold = TRUE)
kable(df2) %>%
kable_paper("striped", full_width = FALSE) %>%
kable_styling(full_width = FALSE, position = "left")
```

Extra white space after knitting in Rmarkdown

I am trying to generate a table using kableExtra on the toy iris data. I am able to successfully generate my output in PDF but I get some white spaces after the 5th row.
set.seed(123)
library(dplyr)
library(kableExtra)
df<- slice_sample(iris, n = 10)
kbl(df, caption = "Iris dataset", booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position"))
I couldn't find any trim flags or so that I can pass them to the slice_sample. Not sure why it persists or is it by design?
The line spacing is a result of selecting booktabs = TRUE in the call to kbl which by definition includes '\addlinespace' in the default linesep argument every fifth row to make it easier to scan tables.
You can override this by setting linesep = "". Or for that matter introduce spacing in any order you like.
kbl(df, caption = "Iris dataset", booktabs = TRUE, linesep = "" ) %>%
kable_styling(latex_options = c("striped", "hold_position"))

Reducing column header names font size in kable tables in Rmarkdown (ioslides)

In all my tables that I'm trying to produces in ioslides I am getting a result that shows a massive column name. How do I shrink this to make the table look okay?
Here is one example, but the same thing happens with every table. What am I missing? Ideally I'd want the content in the table to be the same font size.
Markdown:
## Test
``````{r, Test,echo=FALSE}
options("kableExtra.html.bsTable" = T)
kable(df, format= "html") %>%
kable_styling(full_width = F, bootstrap_options = c("striped", "hover", "condensed"), font_size = 12.5)
Image of my output:
You can change row sizes by using row_spec()
Since the header is interpreted as the row 0 you can change your header independently to fit your needings.
Try
kable(df, format= "html") %>%
kable_styling(full_width = F, bootstrap_options = c("striped", "hover", "condensed"), font_size = 12.5) %>%
row_spec(0, font_size=9)
for more settings check row_spec

How to adjust table to a pdf page with kable?

I am currently working on a Markdown file (with latex) where I use kable() and kableExtra for my tables. The problem is that some of my tables are to big and doesn't fit on a pdf page (even in landscape).
I have tried to use latex_options = "scale_down" from kableExtra but for some reasons it doesn't work, it doesn't change anything. Here is an example of the code I'm running :
kable(dt, "latex", longtable = T, caption = "SampleCaption") %>%
add_header_above(c("","Mens" = 3, "Womens" = 3)) %>%
kable_styling(latex_options = c("striped", "scale_down", "repeat_header"),repeat_header_text = "",
full_width = F) %>%
column_spec(1, width = "10cm")
I already looked on Google and stackoverflow. Anyone have an idea of what I'm doing wrong? Thanks
Edit, here is the working code as requested in comments :
kable(dt, "latex", longtable = T, caption = "SampleCaption") %>%
add_header_above(c("","Mens" = 3, "Womens" = 3, "Total" = 2)) %>%
kable_styling(font_size = 7, latex_options = c("striped", "repeat_header"),repeat_header_text = "",
full_width = F) %>%
column_spec(1, width = "5cm")
This is not an answer but more like a clarification that since scale_down is using the resizebox in package graphicx while longtable is longtable and these two latex packages won't talk with each other, scale_down only works for normal tables.
In fact, you should be seeing a note in your console that "scale_down" doesn't work with longtable
Source in kableExtra
if (table_info$tabular == "longtable") {
warning("Longtable cannot be resized.")
return(x)
}

Strange HTML output from kableExtra package in RStudio

I've having some issues with the kableExtra output in RStudio. I'm trying to create a formatted table in an R markdown file once I hit the 'knit' button.
The code is:
kable(temp_table, "html") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) %>%
kable_styling(bootstrap_options = c("striped", "condensed", "hover", "responsive"), font_size = 11, full_width = FALSE, position = "left") %>%
column_spec(1, bold = T)
It should create a simple table with a header row that I added. But not only does that not show up, but neither do the variable row names. And what precedes the table is a bunch of HTML code that looks like it wasn't rendered.
Some version history:
kableExtra - 0.6.1
knitr - 1.17
rmarkdown - 1.7
R - 3.3.3
RStudio - 1.1.258
Would appreciate any help with this!
---- UPDATE ----
Below is a simple reproducible example that gives me the same kind of output.
---
title: "Iris Example"
output_file: "iris2.html"
---
## Iris Data
``` {r iris, echo = FALSE, warning = FALSE}
library(data.table)
library(knitr)
library(kableExtra)
iris <- data.table(iris)
iris <- iris[, .(
sep_len = mean(Sepal.Length)
, sep_wid = mean(Sepal.Width)
, pet_len = mean(Petal.Length)
, pet_wid = mean(Petal.Width)
), by = .(Species)]
kable(iris, "html") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2)) %>%
kable_styling(bootstrap_options = c("striped", "condensed", "hover", "responsive"), font_size = 10, full_width = FALSE, position = "left") %>%
column_spec(1, bold = T)
```
I've reproduced this output only on an OS X.
Installing the latest version of kableExtra (0.7.0.9000) with
devtools::install_github("haozhu233/kableExtra") solved the problem for me.

Resources