Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R- knitr:kable - How to display table without column names?

Currently, I have this data frame (PS):

Table with column header

My code to display this table is:

kable(PS) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))

I want to display the table without column names like this: enter image description here

Problem is

1) The column names should be non-empty, and attempts to use empty names will have unsupported results

2) If I convert the data frame and remove the column names and then use kable like this:

PS.mat <- as.matrix(PS)
colnames(PS.mat) <- NULL
kable(PS) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))

I get the following error

Error in kable_info$colnames[[length(kable_info$colnames)]] : attempt to select less than one element in integerOneIndex

I also tried the following parameter but with no results

kable(PS, col.names = NA) 

EDIT 1:

A reproducible example:

if (!require(pacman)) install.packages("pacman")
p_load("lubridate","knitr","kableExtra","scales")

Statistics <- c("AUM",
            "Minimum Managed Account Size",
            "Liquidity",
            "Average Margin / Equity",
            "Roundturns / $ Million / Year",
            "Incentive Fees",
            "Instruments Traded")
Value <- c("$30K","$30K","Daily","50%","6,933","25%","ES")
AI <- data.frame(Statistics,Value);
kable(AI) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
like image 263
Urvah Shabbir Avatar asked Jun 23 '17 05:06

Urvah Shabbir


People also ask

What is Kable package in R?

Function 'kable()' is a light weight table generator coming from 'knitr'. This package simplifies the way to manipulate the HTML or 'LaTeX' codes generated by 'kable()' and allows users to construct complex tables and customize styles using a readable syntax.

How do I remove a column name in R?

In R, the easiest way to remove columns from a data frame based on their name is by using the %in% operator. This operator lets you specify the redundant column names and, in combination with the names() function, removes them from the data frame. Alternatively, you can use the subset() function or the dplyr package.

How do you make a Kable table smaller?

By defining the table format and adding some CSS styling you can change the size of the like so: knitr::kable(x, format = "html", table. attr = "style='width:30%;'") .

What does the Kable function do in R?

The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.


1 Answers

Depending on your desired output format you could make use of such functions. For pandoc:

x = kable(AI, format="pandoc") %>%
    kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
cat(x[3:9], sep="\n")

For html:

x = kable(AI, format="html") %>%
    kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
gsub("<thead>.*</thead>", "", x)
like image 150
Jan Avatar answered Oct 19 '22 10:10

Jan