How can I add bracklets () in column names using the gt package in R?
column name in r = grp35_rs12468485. What I want: grp35(rs12468485)
I already created a table with the gt package:
table1_pgx <-
pgx_rs %>%
gt() %>%
tab_header(
title = md("pgx")
Thanks!
The solution can be find on: https://gt.rstudio.com/articles/intro-creating-gt-tables.html
For me this worked:
pgx_table <- pgx_rs %>%
gt() %>%
tab_header(
title = md("pgx")) %>%
cols_label(
grp35_rs12468485 = html("gpr35(rs12468485)")
)
Are you looking for such a solution: Here in the iris example dataset the string after . is enclosed in parenthesis in all column names. In your case you could replace . by _
library(gt)
library(dplyr)
iris %>%
as_tibble() %>%
rename_with(~str_c(str_replace(., "\\.", "("), ")")) %>%
gt()
Related
I am using the gt() table package in R and so far I love it. However for some reason when I publish the below in quarto I get an awkward table header that says "?caption" in bold. However when I run the table separately, I don't get anything.
Any thoughts?
Ignore the titles and columns names, I know it doesn't make sense with the diamonds package
library(tidyverse)
library(gt)
business_segment_summary <- diamonds %>%
group_by(cut) %>%
summarise(n=n(),
sum=sum(price),
sum_od=sum(price,na.rm=TRUE),
prop_25=quantile(price,.25,na.rm=TRUE),
prop_50=quantile(price,.5,na.rm=TRUE),
prop_75=quantile(price,.75,na.rm=TRUE),
mean=mean(price,na.rm=TRUE),
mean_aging=mean(table),
mean_rank=mean(depth),
prop_od=mean(carat),
sd=sd(price,na.rm=TRUE),
mad=mad(price,na.rm=TRUE),
.groups="drop"
) %>%
mutate(tar_prop=sum/sum(sum),
n_prop=n/sum(n))
business_segment_summary %>%
select(1,n,tar_prop,n_prop,prop_od,prop_25,prop_50,prop_75) %>%
gt::gt()
gt::cols_label(cut="Business Segment",
n="Customer #",
tar_prop="% of TAR",
n_prop="% of Customers",
prop_od=gt::html("% of Customers<br>with overdue"),
prop_25="25%",
prop_50="50%",
prop_75="75%") %>%
gt::tab_spanner(label="Customer Account Percentile ($k)",columns = c(prop_25,prop_50,prop_75)) %>%
gt::fmt_number(c(prop_25,prop_50,prop_75),decimals = 0,scale_by = 1/1e3) %>%
gt::fmt_number(n,decimals = 0) %>%
gt::fmt_percent(c(3:5),decimals = 0) %>%
gt::opt_stylize(style=1,color="red") %>%
gt::tab_header(title="Summary of TAR by business segments") %>%
gt::cols_align(align="left",columns = 1)
Could you please help with getting the row gap or break line between the sections displayed in the report? I am using the r2rtf package along with tidyverse.
For example using mtcars I have a column rowname I want to display the data with gap between these rownames
mtcars$rowname <- rowname(mtcars)
mtcars %>%
rtf_body() %>%
rtf_encode() %>%
write_rtf('cars.rtf')
It can be handled in data manipulation step using dplyr before r2rtf.
You just need to add \n at the end of each value.
mtcars %>%
mutate(across(everything(), function(x) paste(as.character(x), "\n"))) %>%
rtf_body() %>%
rtf_encode() %>%
write_rtf('cars.rtf')
I'm not really familiar with the r2rtf package, but you could try with the text_space_after argument of rtf_body
library(r2rtf)
mtcars$rowname <- row.names(mtcars)
mtcars[1:5, c("rowname", "mpg", "cyl")] |>
rtf_body(text_space_after = 200) |>
rtf_encode() |>
write_rtf('cars.rtf')
Created on 2022-10-03 with reprex v2.0.2
I'm trying to bold the maximum value in every row with this code
mtcars %>%
flextable() %>%
bold(max(.[1:32,]))
and got this error: Error in .[1:32, ] : incorrect number of dimensions
I also tried, with inspiration from conditionally bold values in flextable
mtcars %>%
flextable() %>%
bold(~ max(.), 1)
Error in eval(as.call(f[[2]]), envir = data) : object '.' not found
Removing the . in max(.) doesn't make any difference.
To get the max from every row you can try to overwrite the attributes of your flextable object:
Edit:
As mentioned in the comments it is not recommended to change the object strucutre by hand:
library(magrittr)
mtcars %>%
flextable::flextable() -> bold_flex2
for(i in seq_len(nrow(mtcars)))
{
bold_flex2 %<>% flextable::bold(i, which.max(mtcars[i,]))
}
bold_flex2
old answer:
library(magrittr)
mtcars %>%
flextable::flextable() -> bold_flex
for(i in seq_len(nrow(mtcars)))
{
bold_flex$body$styles$text$bold$data[i, which.max(mtcars[i,])] <- TRUE
}
To bold the maximum value, I believe you have to do each column separately.
I used two of the columns here and only made the column where the variable is bold.
library(tidyverse)
library(flextable)
data(mtcars)
mtcars %>%
flextable() %>%
bold(~mpg == max(mpg), 1) %>%
bold(~drat == max(drat), 5)
I have this dataframe:
df = data.frame(a = c("$B_{a}$", "$m^{a}$"))
When I make a table using kable in Rmarkdown like so:
df %>% knitr::kable()
and knit it to a pdf_document, I get this:
which is what I expected.
Now, I want to reproduce the same table, but using the package gt. When I do:
library(gt)
df %>% gt()
I get this:
What else do I have to do that so that gt table "understands" these are mathematical notations?
The <sub></sub> and <sup></sup> works with gt. One option is to replace the characters in the original dataset column with html syntax using str_remove/str_replace from stringr
library(gt)
library(stringr)
library(dplyr)
df %>%
mutate(a = str_remove_all(a, "[{}$]") %>%
str_replace_all( c('(.)_(.)', "(.)\\^(.)"),
c("\\1<sub>\\2</sub>", "\\1<sup>\\2</sup>"))) %>%
gt() %>%
fmt_markdown(columns = everything())
-output
I'm trying to extract a table from a PDF with the R tabulizer package. The functions work fine, but it can't get all the data from the entire table.
Below are my codes
library(tabulizer)
library(tidyverse)
library(abjutils)
D_path = "https://github.com/financebr/files/raw/master/Compacto09-08-2019.pdf"
out <- extract_tables(D_path,encoding = 'UTF-8')
arrumar_nomes <- function(x) {
x %>%
tolower() %>%
str_trim() %>%
str_replace_all('[[:space:]]+', '_') %>%
str_replace_all('%', 'p') %>%
str_replace_all('r\\$', '') %>%
abjutils::rm_accent()
}
tab_tidy <- out %>%
map(as_tibble) %>%
bind_rows() %>%
set_names(arrumar_nomes(.[1,])) %>%
slice(-1) %>%
mutate_all(funs(str_replace_all(., '[[:space:]]+', ' '))) %>%
mutate_all(str_trim)
Comparing the PDF table (D_path) with the tab_tidy database you can see that some information was missing. All first columns, which are merged, are not found during extract_tables(). Also, all lines that contain “Boi Gordo” and “Boi Magro” information are not found by the function either.
The rest is in perfect condition. Would you know why and how to solve it? The questions here in the forum dealing with this do not have much answer.