Currently, I have this data frame (PS):
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:
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"))
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)
Related
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))
I'm trying to spit out two tables side by side, using knitr::kables.
I can't figure out how to make my styles stick, though. If I run one kable, the styles work fine:
kable(
caption = "Oh look! A Caption",
starwars %>%
count(gender, sex) %>%
arrange(desc(gender))
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
gives me a tidy looking table:
But if I try to set two up side by side, the formatting (or kable_styling) gets lost:
knitr::kables(list(
kable(caption = "Oh look! A Caption",
starwars %>%
count(gender, sex) %>%
arrange(desc(gender))) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed")),
kable(caption = "Oh look! A Caption",
starwars %>%
count(gender, sex) %>%
arrange(desc(gender))) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
))
The formatting all just evaporates:
How do I get kable_styling to apply to two kables?
The extremely unsatisfying solution turned out to be adding a third kable_styling() call:
knitr::kables(list(
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed")),
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
)
) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
That seems verbose, but it works.
There's a block of code that I'm using over and over in a project. It works fine when I run it straight:
kable(
starwars %>%
count(gender),
col.names = c("Response", "Count")
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
But I want to create a function that I can just call once to run it. I expected that this would work:
fancy_table <- function(x,y) {
kable(
y %>%
count(x),
col.names = c("Response", "Count")
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
}
But when I try to run it with fancy_table(gender, starwars), I get an error:
Error: Column `x` is unknown
How do I pass a function variable to count()?
I am generating different tables of different row length, so I want to have all the text of certain color, but I have a question about the last row of my code in row_spec
library(kable)
library(kableExtra)
mtcars %>% filter(cyl=4) %>%
kable(align=c("l", rep("c", ncol(.)-1)),bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_styling(c("striped", "hover", "condensed", "responsive"), full_width = TRUE) %>%
row_spec(0: nrow(.), color = "black")
0: nrow(.) is not valid and I am not sure why, while rep("c", ncol(.)-1)) works.
I think it doesn't work because nrow(.) returns NULL:
library(kable)
library(kableExtra)
mtcars %>%
kable(align=c("l", rep("c", ncol(.)-1)),bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_styling(c("striped", "hover", "condensed", "responsive"), full_width = TRUE) %>% nrow(.)
#NULL
You could do this to color all rows:
mtcars %>%
kable(align=c("l", rep("c", ncol(.)-1)),bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_styling(c("striped", "hover", "condensed", "responsive"), full_width = TRUE) %>%
row_spec(1:nrow(mtcars),color = "black")
Even though not elegant, I would do it in two steps:
library(knitr)
library(kableExtra)
library(dplyr)
# Step 1: Prepare data
temp <- mtcars %>%
filter(cyl == 4) %>%
sample_n(sample(2:nrow(.), 1)) %>%
select(1:3)
# Step 2: Produce table
temp %>%
kable(align=c("l", rep("c", ncol(temp)-1))) %>%
kable_styling(c("striped", "hover", "condensed"), full_width = FALSE) %>%
# Format last row:
row_spec(nrow(temp), color = "red", italic = TRUE, bold = TRUE)
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