Quarto file not producing data table - r

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

Related

Writing gt table to pdf using pdf()

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

How to deploy kable table latex format in markdown

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.

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

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