我正在使用官员制作ppt,并且我正在使用以下代码在一张幻灯片中插入表格。一切正常。尽管在表中,我需要进行条件文本着色,如果我多次进行,则可以正常工作。但是,当我在函数中使用for循环时,代码工作正常,但是我的ppt损坏了,我看不到ppt的输出。任何想法
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)
}
}
}
}
)
数据就像
dput(head(tm.rank.out))
structure(list(rank(rank = 1:6,human = c(“ AoEC(2.1)”,“ LSEC(2.1)”, “ LMVEC(2)”,“前列腺(2)”,“反式淋巴细胞(2)”,“甲状腺(2)” ),cyno = c(“ AoEC(2.2)”,“ DR Ganglion(2.2)”,“ Kidney(2.2)”, “ LMVEC(2.2)”,“视网膜(2.2)”,“胆囊(2.1)”),dog = c(“ DR Ganglion(2.6)”, “纹状体(2.6)”,“骨髓(2.5)”,“ NAc(2.5)”,“肾上腺(2.5)”, “ Cecum(2.5)”)),已排序=“ rank”,类别= 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”),人= c(“ AoEC”, “ LSEC”,“ LMVEC”,“前列腺”,“反式淋巴细胞”,“甲状腺”), cyno = c(“ AoEC”,“ DR Ganglion”,“ Kidney”,“ LMVEC”,“ Retina”, “胆囊”),狗= c(“ DR神经节”,“纹状体”,“骨髓”, “ NAc”,“肾上腺”,“盲肠”)),类别= c(“ data.table”,“ data.frame” ),row.names = c(NA,-6L),.internal.selfref =)
答案 0 :(得分:0)
我想通了,我必须在函数的末尾添加“ x”。就像
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
}
)