I managed to create a flextable based on the below code.
The data that is being used in opleiding1 through 4 looks a bit like this: 0, 0, 1, 7, 3, 7, 0
t7 <- data.frame(df$opleiding.1.[df$startvraag=="Ja"],
df$opleiding.2.[df$startvraag=="Ja"],
df$opleiding.3.[df$startvraag=="Ja"],
df$opleiding.4.[df$startvraag=="Ja"]
)
t7 <- data.frame(aantal=(apply(t7, 2, sum))) %>% round(digits = 0)
rownames(t7) <- c("hbo, universitair", "mbo, vwo, havo", "vmbo/mavo", "onbekend")
t7
Creating a flextable with the following code:
t7 <- xtable(t7)
ft7 <- t7 %>% xtable_to_flextable() %>%
fontsize(part = "header", size = 20) %>%
fontsize(part = "body", size = 18) %>%
align_text_col(align = "left") %>%
align_nottext_col(align = "center") %>%
width(width = 2.3) %>%
width(j = 1, width = 2.0) %>%
height_all(height = .5) %>%
border_inner(border = std_border) %>%
font(fontname = "Open Sans") %>%
color(part = "header", color = d) %>%
ft7
This gives me a flextable which is just as I want it except for the decimal which are shown here. I've looked for possible solutions but nothing seems to work. It looks like the decimal is added when converting the table to an flextable. When I run the previous code (mentions on top) it does not return with any decimals as shown here. Hope someone can help me sort this.
I think that is because your aantal column's type is double, meaning it is holding real values. You can change it's type to integer as you create the data.frame.
Try this
t7 <- data.frame(aantal=(apply(t7, 2, sum))) %>%
round(digits = 0) %>%
mutate(aantal = as.integer(aantal))
Related
I am trying to properly set line breaks in flextable formatted tibble.
library(dplyr)
library(flextable)
tibble(Ind1 = rep("Indicator name 1", 3),
ranges1 = c("[0; 1)", "[1; 2)", "[2; 3)"),
`[0; 1.5)` = c("Text11\na\nb\nc\nd\ne", "Text21\na\nb\nc\nd\ne", "Text31\na\nb\nc\nd\ne"),
`[1,5; 3)` = c("Text12\na\nb\nc\nd\ne", "Text22\na\nb\nc\nd\ne", "Text32\na\nb\nc\nd\ne"),
`[3; 4.5)` = c("Text13\na\nb\nc\nd\ne", "Text23\na\nb\nc\nd\ne", "Text33\na\nb\nc\nd\ne")) %>%
flextable() %>%
flextable::theme_box() %>%
flextable::bold(j = c(1, 2)) %>%
flextable::rotate(j = c(1, 2), rotation = "btlr") %>%
merge_v(j = 1) %>%
flextable::add_header_row(colwidths = c(1, 1, 3),
values = c("", "", "Indicator name\n2"))
I got stuck as I am getting this wrong output:
The line breaks are automatically set breaking the line at unnecessary positions (red arrows).
What is the proper way to stretch the rotated text to the extent of merged cells and how to remove text from header with merging four upper left corner cells (yellow box) if I want to get the following output?
Appended
Emptying the yellow square at header and merging its cells
can be solved by:
%>% mk_par(i = 2, j = c(1, 2), value = as_paragraph(""), part = "header") %>%
merge_at(i = c(1, 2), j = c(1, 2), part = "header")
I have several tables that all need to be formatted similarly. I figured out how I want the tables formatted on one table, and have been trying to write a function that I can apply to all tables easily without having to copy and paste the code over and over
Here is my sample data frame
example <- data.frame(
stringsAsFactors = FALSE,
age = c("age 12 to 17", "age 18 to 25", "age 26 and up"),
alc_est = c(14579.28558, 131872.35964, 660957.6512),
bing_alc = c(0.0477, 0.3143, 0.224),
marj_est = c(35913.3345, 137033.12968, 534667.52856),
mj_use = c(0.1175, 0.3266, 0.1812),
heroin_est = c(NA, 419.5748, 18294.36356),
heroin_use = c(NA, 0.001, 0.0062),
meth_est = c(550.16172, 2139.83148, 68161.25778),
meth_use = c(0.0018, 0.0051, 0.0231)
)
Here is some of the formatting that will be applied to multiple tables
table_format <- example %>%
gt(rowname_col = "age") %>%
tab_stubhead(label = md("**Statewide**")) %>%
tab_header(
title = md("**table title here**"),
subtitle = "more info here"
) %>%
fmt_number(
columns = c(2, 4, 6, 8),
decimals = 0
) %>%
tab_spanner_delim(
delim = "_",
columns = c(2:9)
)
Here is how I tried to write a function to apply to multiple tables. The function runs but doesn't actually format the table
format_use_est <- function(df_here) {
result <- df_here %>%
gt(rowname_col = "age") %>%
tab_stubhead(label = md("**Statewide**")) %>%
tab_header(
title = md("**table title here**"),
subtitle = "more info here"
) %>%
fmt_percent(
columns = c(3, 5, 7, 9),
decimals = 1
) %>%
fmt_number(
columns = c(2, 4, 6, 8),
decimals = 0
) %>%
tab_spanner_delim(
delim = "_",
columns = c(2:9)
)
}
How can I write a function for formatting these tables and apply to multiple tables more effectively? Thank you!
I need to create table with same footnote being placed in both header and body of the table, I cannot figure out how to make it happen using flextable, what I can create is something as below:
library(flextable)
library(dplyr)
library(tidyr)
data(iris)
iris %>%
as_tibble %>%
gather(.,key = variable,value = value,-Species) %>%
group_by(Species,variable) %>%
summarise(value=formatC(mean(value),digits = 2,format = 'f')) %>%
ungroup %>%
spread(.,key = variable,value = value) %>%
flextable %>%
footnote(.,part = 'header',i = 1,j = c(2:5),
value = as_paragraph(c('Rounded to two decimal places')),
ref_symbols = c('*'),
inline=FALSE) %>%
footnote(.,part = 'body',i = c(1:3),j = 1,
value = as_paragraph(c('Rounded to two decimal places')),
ref_symbols = c('*'),
inline=FALSE)
Currently I created two footnotes with the same statement for header and body, I wonder if I can merge the two statements into one.
Thanks!
(I did not imagine footnotes would be repeated when this function has been implemented but) by using merge_v, you can merge them if identical:
library(flextable)
library(dplyr)
library(tidyr)
data(iris)
iris %>%
as_tibble %>%
gather(.,key = variable,value = value,-Species) %>%
group_by(Species,variable) %>%
summarise(value=formatC(mean(value),digits = 2,format = 'f')) %>%
ungroup %>%
spread(.,key = variable,value = value) %>%
flextable %>%
footnote(.,part = 'header',i = 1,j = c(2:5),
value = as_paragraph(c('Rounded to two decimal places')),
ref_symbols = c('*'),
inline=FALSE) %>%
footnote(.,part = 'body',i = c(1:3),j = 1,
value = as_paragraph(c('Rounded to two decimal places')),
ref_symbols = c('*'),
inline=FALSE) %>%
merge_v(part = "footer")
I'm using R version 3.6.1 in RStudio. I have flextable version 0.5.5 and officer version 0.3.5.
I'm having difficulty with formatting my numbers in flextables within RMarkdown. By default, all numbers show up with 3 decimal places. For some of my numbers, this is fine (and actually preferred), but for others, I want to remove the decimals.
Using the advice found here I was able to adjust my table so that all numbers are rounded to the nearest whole number. My code is below (example table used for reproduciblility; otherwise formatting is the same as my current code).
ft_test <- head(iris) %>% flextable() %>%
hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
align(align ='center', part = 'all') %>%
align(j = 1, align ='left', part = 'all') %>%
set_formatter_type(fmt_double = "%.0f")
ft_test
However, I only want certain columns to be whole numbers, and other columns to still have decimals. I've tried using the j argument to call certain columns:
ft_test <- head(iris) %>% flextable() %>%
hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
align(align ='center', part = 'all') %>%
align(j = 1, align ='left', part = 'all') %>%
set_formatter_type(fmt_double = "%.0f", j = 2)
ft_test
... but then I get an error telling me j = 2 is an unused argument.
Any suggestions for how to adjust the numbers of only some columns? Thanks in advance for your help!
You can not use argument j as it is not an argument of set_formatter_type. The function is setting formatters for one or several data type. In your case, it's better to use colformat_num.
library(flextable)
library(officer)
library(magrittr)
ft_test <- head(iris) %>% flextable() %>%
hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
align(align ='center', part = 'all') %>%
align(j = 1, align ='left', part = 'all') %>%
colformat_num(j = c("Sepal.Length", "Sepal.Width",
"Petal.Length", "Petal.Width"), digits = 1)
ft_test
You can learn more about formatting content here: https://davidgohel.github.io/flextable/articles/display.html
I need to create a latex table in RStudio for pdf output with the following structure:
This table was created for html output with the following code:
mat <- data.frame(a = c("column header","column header"),
rowx=c("row1","row2"),b = c("a","b"),
c = c("x","y"))
kable(mat, align = "c",col.names = c("","","v1","v2")) %>%
kable_styling(bootstrap_options = "striped", full_width = F,
position = "left",font_size = 12) %>%
column_spec(1, bold = T,width="2em",extra_css="transform: rotate(-90deg);") %>%
collapse_rows(columns = 1, valign = "middle") %>%
add_header_above(c(" " = 2, "row header" = 2))
I need to create a similar structure with LaTeX tables.
His is how far I got:
mat <- data.frame(a = c("column header","column header"),
rowx=c("row1","row2"),b = c("a","b"),c = c("x","y"))
kable(mat, align = "c",col.names = c("","","v1","v2")) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left",font_size = 12) %>%
collapse_rows(columns = 1, latex_hline = "none") %>%
add_header_above(c(" " = 2, "rows" = 2))
So I still need at least 2 more things:
rotate the label in the very first column
remove the spurious leftmost column separator in the second row.
Can this be achieved with kableExtra commands and parameters?
Here's a shot with huxtable (my package):
as_hux(mat, add_colnames = TRUE) %>%
insert_row(c("", "", "rows", "")) %>%
merge_cells(3:4, 1) %>%
merge_cells(1, 3:4) %>%
merge_cells(1:2, 1:2) %>%
set_rotation(3, 1, 90) %>%
set_bottom_border(0.4) %>%
set_bold(1:2, everywhere, TRUE) %>%
set_wrap(3, 1, TRUE) %>%
set_bottom_padding(4, -1, 48) %>%
set_bottom_padding(3, -1, 30) %>%
set_row_height(c("1em", "1em", "1.5em", "1.5em")) %>%
quick_pdf()
I have to admit, this took a lot of tweaking. TeX tables are hard to understand....