I am using officer to make a ppt and i am using following code to insert a table in one of the slide. All is working fine. Though In the table, I need to do conditional text coloring, which works fine, if I do it multiple times. But when I use a for loop with in a function, codes works fine, but my ppt gets corrupted and I dont see the output in ppt. Any idea
library(data.table)
library(RMySQL)
library(officer) #from source
library(magrittr)
library(flextable)
library(RColorBrewer)
library(ggplot2)
tmrank.ft <- regulartable(data = tm.rank.out) %>%
theme_booktabs() %>%
autofit() %>%
merge_v(j=missing_species) %>%
align(align = 'center') %>%
align(align = 'center', part = 'header') %>%
bold(part = 'header') %>%
height(height = 1, part = 'header') %>%
fontsize(size = 14, part = 'all') %>%
width(j=1, width = 1.0 ) %>%
width(j=2:ncol(tm.rank.out), width = 2.5) %>%
(
function(x) {
## These for loop causes the issue. If I dont use for loop, it works great.
for (ii in 2:length(species)) {
group_result <- group_vector( tm.rank.out.temp, grp.type, 'human')
if(length(group_result) >0) {
for (jj in 1:length(group_result)) {
text_color <- grp.clr[names(group_result)[jj]]
color(x, i = group_result[[jj]], j = ~ human, color = text_color)
}
}
}
}
)
Data is like
dput(head(tm.rank.out))
structure(list(rank = 1:6, human = c("AoEC (2.1)", "LSEC (2.1)", "LMVEC (2)", "Prostate (2)", "Trans Lymphocytes (2)", "Thyroid (2)" ), cyno = c("AoEC (2.2)", "DR Ganglion (2.2)", "Kidney (2.2)", "LMVEC (2.2)", "Retina (2.2)", "Gallbladder (2.1)"), dog = c("DR Ganglion (2.6)", "Striatum (2.6)", "Bone Marrow (2.5)", "NAc (2.5)", "Adrenal (2.5)", "Cecum (2.5)")), sorted = "rank", class = c("data.table", "data.frame" ), row.names = c(NA, -6L), .internal.selfref = )
dput(head(tm.rank.out.temp))
structure(list(rank = c("1", "2", "3", "4", "5", "6"), human = c("AoEC", "LSEC", "LMVEC", "Prostate", "Trans Lymphocytes", "Thyroid"), cyno = c("AoEC", "DR Ganglion", "Kidney", "LMVEC", "Retina", "Gallbladder"), dog = c("DR Ganglion", "Striatum", "Bone Marrow", "NAc", "Adrenal", "Cecum")), class = c("data.table", "data.frame" ), row.names = c(NA, -6L), .internal.selfref = )
i figured out, I had to put "x" in the end loop of the function. so it would be like
tmrank.ft <- regulartable(data = tm.rank.out) %>%
theme_booktabs() %>%
autofit() %>%
merge_v(j=missing_species) %>%
align(align = 'center') %>%
align(align = 'center', part = 'header') %>%
bold(part = 'header') %>%
height(height = 1, part = 'header') %>%
fontsize(size = 14, part = 'all') %>%
width(j=1, width = 1.0 ) %>%
width(j=2:ncol(tm.rank.out), width = 2.5) %>%
(
function(x) {
## These for loop causes the issue. If I dont use for loop, it works great.
for (ii in 2:length(species)) {
group_result <- group_vector( tm.rank.out.temp, grp.type, 'human')
if(length(group_result) >0) {
for (jj in 1:length(group_result)) {
text_color <- grp.clr[names(group_result)[jj]]
color(x, i = group_result[[jj]], j = ~ human, color = text_color)
}
}
}
x
}
)