How to deploy kable table latex format in markdown - r

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.

Related

Quarto file not producing data table

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')

html kable_styling not showing output in a for loop

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

wrap text in knitr::kable table cell using "\n"

How can I wrap a cell in a knitr::kable table cell using \n?
I want to generate a .rmd file containing some tables where one column needs text wrapping. The places where the wrapping should occur are marked with \n. I tried (this is a standalone .rmd document):
---
output: pdf_document
---
## A Table with text wrap
```{r cars}
knitr::kable(data.frame(col1 = c("a", "b"), col2 = c("one\ntwo", "three\nfour")))
```
..but this doesn't work. Instead of staying in col2, the wrapped part lives on the next line of col1.
Expected output is:
col1 | col2
-------------
a | one
| two
b | three
| four
Solutions using other packages than knitr are welcome as long as they allow to print (nearly) as nice.
A fire-and-forget solution for flexible dual HTML/PDF of standard tables. It incorporates kableExtra's linebreak function outlined by #snoram.
Assumption: You use <br> as your line break indicator.
```{r}
knit_table(df1)
```
Code
library(dplyr)
library(knitr)
library(kableExtra)
knit_table <- function(df){
if (is_html_output()) {
df %>%
kable("html", escape = F) %>%
kable_styling()
} else {
df <- data.frame(lapply(df, function(x) {gsub("<br>", "\n", x)}), stringsAsFactors = F)
df %>%
mutate_all(linebreak) %>%
kable("latex", booktabs = T, escape = F)
}
}
Data
df1 <- data.frame(col1 = c("a", "b"),
col2 = c("one<br>two", "three<br>four"))
If you don't mind using kableExtra you can definitely achieve this, here is one example:
library(kableExtra)
knitr::kable(data.frame(col1 = c("a", "b"), col2 = linebreak(c("one\ntwo", "three\nfour"))),
escape = FALSE)
More details here.

How to show and run code but don't print results in Rmarkdown knitr

When I knit the following code chunk in Rmarkdown it will print out the results as well. I just want to run and show the code. In other code chunks in the same .Rmd file this knitr syntax works...
```{r import, results = "hide"}
gs_ls()
df <- gs_title("worlds-view-of-America")
confInPres <- df %>% gs_read(ws = "Sheet1", range = cell_rows(1:38))
colnames(confInPres) <- paste("year", colnames(confInPres), sep = "_")
colnames(confInPres)[1] <- "Country"
confInTrump <- select(confInPres, Country, year_2017)
favUS <- df %>% gs_read(ws = "Sheet2", range = cell_rows(1:38))
```
Take a look here.
If you want to show the code, use echo=TRUE.

R, Latex and RMarkdown: table() input for xtable() missing column and row labels?

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")

Resources