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
Related
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 have drafted some dataframes with text inside that linebreaks are present in each cell, the dataframe is converted to flextable and then inserted into a powerpoint slide using officer. I found the line height a bit too much, I tried using the height_all function in flextable to make reduce the line height, but it is not working. Please find the sample code as below:
library(officer)
library(dplyr)
pptx.output.st00 <- read_pptx()
data(iris)
data.df <- head(iris) %>%
as_tibble %>%
mutate_all(.,as.character) %>%
mutate_all(.,~paste0(.,'\ntesting'))
pptx.tbl <- data.df %>%
flextable %>%
height_all(height = 0.01) # this line is not working
pptx.output.st01 <- pptx.output.st00 %>%
add_slide(.,layout = 'Title and Content',master = 'Office Theme') %>%
ph_with(.,value=pptx.tbl,location=ph_location(type='body'))
print(pptx.output.st01,'presentation.output.pptx')
Currently I need to manually change the paragraph settings for table as the screen capture below:
Is there a way in officer of flextable to set up line height for table? Thanks!
I didn't satisfied by this ad hoc way, but padding(padding.top = 0, padding.bottom = 0.5) and height_all(0.45) gives a bit better output.
pptx.output.st01 <- pptx.output.st00 %>%
add_slide(.,layout = 'Title and Content',master = 'Office Theme') %>%
ph_with(.,
value=pptx.tbl %>%
padding(padding.top = 0, padding.bottom = 0.5) %>%
height_all(0.45),
location=ph_location(type='body'))
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
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.
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")