I want to use kable inside a for loop to generate a lot of tables in a HTML rmarkdown. I was looking for solutions and most of them are solved using the wrapper print around kable code. But when I want to generate html table outputs with kable_styling, this solution didn't work! For example:
table <- tibble(a = c(1:10),
b = letters[1:10])
for(each in 1:2) {
print(table %>%
kable())
cat("<br>")
}
This generate two simple tables.
But when I try:
table <- tibble(a = c(1:10),
b = letters[1:10])
for(each in 1:2) {
print(table %>%
kable() %>%
kable_styling("striped"))
cat("<br>")
}
Nothing happens! And that's only for html outputs. With latex it's ok. What should I do?
Add htmltools::HTML() to your pipe:
table %>%
kable() %>%
kable_styling("striped") %>%
htmltools::HTML() %>%
print
Related
I'm trying to write a gt table to pdf using the pdf() function with the following code.
This works very well with ggplots, but for some reason not with gt.
The reason for using the pdf() function is that I'm looking to create a single pdf document with multiple plots and tables.
library(gt)
library(dplyr)
islands_tbl <-
tibble(
name = names(islands),
size = islands
) %>%
arrange(desc(size)) %>%
slice(1:10)
pdf()
plot(x = 1:10, y = 1:10)
plot(x = 1:10, y = 11:20)
gt(islands_tbl) %>% print
dev.off()
Not sure, but I think gt::gtsave function is what you are looking for.
library(gt)
library(dplyr)
islands_tbl <-
tibble(
name = names(islands),
size = islands
) %>%
arrange(desc(size)) %>%
slice(1:10)
gt(islands_tbl) %>%
gtsave('islands.pdf')
If you need a complete pdf document including table and previous plots as well, I should use an Rmarkdown file with output:pdf_document
I am very new to Quarto and I am trying to create an markdown document using it. Everything works well except I am not able to render tables on the output HMTL file.
The following is my code. The HTML document shows ':: {.cell-output-display}' where the table is supposed to be rendered. I would really appreciate it if you could help me out with this.
process_results <- function(value){
results <- topTable(fit2, coef=value,n=Inf,sort.by = 'p')
top_results <- head(results, n = 10) %>%
kable(caption = value) %>% ### This works on traditional mardown.
kable_styling()
...........
..............
}
process_results('GroupB_vs_GroupA')
The function doesn't return a value is the problem.
library(kableExtra)
process_results <- function(value){
#results <- topTable(fit2, coef=value,n=Inf,sort.by = 'p')
top_results <- head(mtcars, n = 10) %>%
kable(caption = value) %>% ### This works on traditional mardown.
kable_styling()
return(top_results)
}
process_results('GroupB_vs_GroupA')
I need to create a lot of kable tables so i use a for loop and store this tables in a list, but when I try to deploy the kable table from the list in markdown, the result is the latex code, not a pdf, what can I do to solve it?
table <- data.frame(col1 = c(1:30), col2 = c(rep(a, 10), rep(b, 10), rep(c, 10)))
t_list <- list()
for (letter in unique(table$col2)){
a <- table %>% filter(col2 == letter)
aa <- a %>% kbl()
t_list <- append(t_list, aa)
}
# R Markdown ------------------
\```{r}
t_list[[1]]
\```
# output in latex not in pdf
Thanks
The solution should be relatively simple, by asking the output inline (r t_list[[1]]), and not within an R code chunk. Like this, I was able to reproduce your code and produce a PDF printing the first table, as in your code above:
```{r}
table <- data.frame(col1 = c(1:30), col2 = c(rep("a", 10), rep("b", 10), rep("c", 10)))
t_list <- list()
for (letter in unique(table$col2)){
a <- table %>% filter(col2 == letter)
aa <- a %>% kbl() %>%
kable_styling(latex_options = "striped")
t_list <- append(t_list, aa)
aa
}
```
# R Markdown ------------------
`r t_list[[1]]`
See this image as a quick example:
PDF output
Hope this helps, but please do let me know if it doesn't.
I have a data frame df with column names containing underscores. I would like to format it using knitr::kable(df, ...). When this is used with rmarkdown you can pass it a list of nice column names, and it will also automatically spread the names over multiple lines to make the table fit on page.
However, I am trying to use kable with Shiny. That is, I would like a renderXXX() type output to write to the output object so that I can call my nice table from ui.R. I've tried in server.R:
renderText(
knitr::kable(df, format = "html")
)
and in ui.R:
htmlOutput("results_table")
but this just leads to a very messy table.
I think you'll find the full answer here:
https://cran.r-project.org/web/packages/kableExtra/vignettes/use_kable_in_shiny.html
UPDATE
Using the mtcars dataset as an example, first you add the below to your ui section:
mainPanel(
tableOutput("mtcars_kable")
)
and follow that through in the server section with:
library(kableExtra)
output$mtcars_kable <- function() {
req(input$mpg)
mtcars %>%
mutate(car = rownames(.)) %>%
select(car, everything()) %>%
filter(mpg <= input$mpg) %>%
knitr::kable("html") %>%
kable_styling("striped", full_width = F) %>%
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6))
}
If you want a more powerful data table in Shiny, with the ability to extensively customize the appearance, try:
Using DT in Shiny
I am trying to create a pretty LaTeX table where the names of the row and column variables of a table are included when using the xtable library.
MWE:
```{r results="asis"}
test <- data.frame(Apples=c(1,2,3), Oranges=c(4,5,6), Watermelons=c(7,8,9))
testxtab <- xtable(with(test,table(Apples,Oranges)))
print(testxtab, comment=FALSE)
```
The result is a LaTeX table that is missing the "Apples" and "Oranges" labels. How do I include them?
I'm sure you could adapt this to xtable, but here's one approach you can take using pixiedust. (It isn't a very elegant solution, but given the structure of the table object, I'm not sure elegant is an option).
library(pixiedust)
obj <- with(test, table(Apples, Oranges))
dimnames <- names(attr(obj, "dimnames"))
head <-
rbind(c("", dimnames[2], rep("", ncol(obj) - 1)),
c(dimnames[1], colnames(obj))) %>%
as.data.frame(stringsAsFactors = FALSE)
body <- cbind(rownames(obj), obj) %>%
as.data.frame(stringsAsFactor = FALSE)
dust(body) %>%
redust(head, part = "head") %>%
sprinkle(rows = 1,
cols = 2:4,
merge = TRUE,
part = "head") %>%
medley_bw() %>%
sprinkle_print_method("latex")