R - ReporteRs 包。是否可以将条件格式应用于文本?
R - ReporteRs Package. Is it possible to apply conditional formatting to text?
我关注dataframe
:
> View(AuthorsMM)
Autor # Sentiment
1 Autor 1 33 J
2 Autor 2 33 J
3 Autor 3 22 K
4 Autor 4 18 L
5 Autor 5 16 L
6 Autor 6 15 K
7 Autor 7 15 L
8 Autor 8 15 K
9 Autor 9 15 K
10 Autor 10 14 K
我正在使用包 ReporteRs
将此 data.frame 作为 flextable
发送到 Powerpoint:
AuthorsMM_ft <- FlexTable( data = head(AuthorsMM,10), header.columns = TRUE )
我使用以下文本属性定义情绪列:
AuthorsMM_ft[, 3] = textProperties( color = 'white', font.weight = 'bold', font.family = 'Wingdings', font.size = 12 )
这使得Powerpoint根据栏目内容显示不同的表情符号(因为Wingdings trueType)。
但是,我想根据内容另外对文本应用不同的颜色(绿色、黄色、红色)。因此:
if (Sentiment == "J") then textProperties( color = 'green',
font.weight = 'bold', font.family = 'Wingdings', font.size = 12)
if (Sentiment == "K") then textProperties( color = 'yellow',
font.weight = 'bold', font.family = 'Wingdings', font.size = 12)
if (Sentiment == "L") then textProperties( color = 'red',
font.weight = 'bold', font.family = 'Wingdings', font.size = 12)
可以使用这个包吗?
是的,有可能。
这段代码展示了如何做到这一点:
library(ReporteRs)
# define the data frame - extracted with dput()
AuthorsMM <- structure(list(Autor = c("Autor 1", "Autor 2", "Autor 3", "Autor 4",
"Autor 5", "Autor 6", "Autor 7", "Autor 8", "Autor 9", "Autor 10"
), `#` = c(33L, 33L, 22L, 18L, 16L, 15L, 15L, 15L, 15L, 14L),
Sentiment = c("J", "J", "K", "L", "L", "K", "L", "K", "K",
"K")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-10L), .Names = c("Autor", "#", "Sentiment"), spec = structure(list(
cols = structure(list(Autor = structure(list(), class = c("collector_character",
"collector")), `#` = structure(list(), class = c("collector_integer",
"collector")), Sentiment = structure(list(), class = c("collector_character",
"collector"))), .Names = c("Autor", "#", "Sentiment")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
# conditional formatting:
base_text_prop = textProperties(font.size = 12,
color = "black",
font.weight = 'bold',
font.family = 'Wingdings')
myCellProps = cellProperties(padding = 5)
AuthorsMM_ft <- FlexTable(data = head(AuthorsMM,10),
header.columns = TRUE,
body.text.props = base_text_prop)
AuthorsMM_ft[AuthorsMM$Sentiment == 'J',3] = chprop(base_text_prop, color = 'green')
AuthorsMM_ft[AuthorsMM$Sentiment == 'K',3] = chprop(base_text_prop, color = 'yellow')
AuthorsMM_ft[AuthorsMM$Sentiment == 'L',3] = chprop(base_text_prop, color = 'red')
AuthorsMM_ft
出于某种原因,在 FlexTable
中定义 textProperties
无效。当我使用 background.color
时,它似乎改变了格式。
FlexTables 中 changing properties and conditional formatting 的更多示例。
我关注dataframe
:
> View(AuthorsMM)
Autor # Sentiment
1 Autor 1 33 J
2 Autor 2 33 J
3 Autor 3 22 K
4 Autor 4 18 L
5 Autor 5 16 L
6 Autor 6 15 K
7 Autor 7 15 L
8 Autor 8 15 K
9 Autor 9 15 K
10 Autor 10 14 K
我正在使用包 ReporteRs
将此 data.frame 作为 flextable
发送到 Powerpoint:
AuthorsMM_ft <- FlexTable( data = head(AuthorsMM,10), header.columns = TRUE )
我使用以下文本属性定义情绪列:
AuthorsMM_ft[, 3] = textProperties( color = 'white', font.weight = 'bold', font.family = 'Wingdings', font.size = 12 )
这使得Powerpoint根据栏目内容显示不同的表情符号(因为Wingdings trueType)。
但是,我想根据内容另外对文本应用不同的颜色(绿色、黄色、红色)。因此:
if (Sentiment == "J") then textProperties( color = 'green', font.weight = 'bold', font.family = 'Wingdings', font.size = 12)
if (Sentiment == "K") then textProperties( color = 'yellow', font.weight = 'bold', font.family = 'Wingdings', font.size = 12)
if (Sentiment == "L") then textProperties( color = 'red', font.weight = 'bold', font.family = 'Wingdings', font.size = 12)
可以使用这个包吗?
是的,有可能。
这段代码展示了如何做到这一点:
library(ReporteRs)
# define the data frame - extracted with dput()
AuthorsMM <- structure(list(Autor = c("Autor 1", "Autor 2", "Autor 3", "Autor 4",
"Autor 5", "Autor 6", "Autor 7", "Autor 8", "Autor 9", "Autor 10"
), `#` = c(33L, 33L, 22L, 18L, 16L, 15L, 15L, 15L, 15L, 14L),
Sentiment = c("J", "J", "K", "L", "L", "K", "L", "K", "K",
"K")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-10L), .Names = c("Autor", "#", "Sentiment"), spec = structure(list(
cols = structure(list(Autor = structure(list(), class = c("collector_character",
"collector")), `#` = structure(list(), class = c("collector_integer",
"collector")), Sentiment = structure(list(), class = c("collector_character",
"collector"))), .Names = c("Autor", "#", "Sentiment")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
# conditional formatting:
base_text_prop = textProperties(font.size = 12,
color = "black",
font.weight = 'bold',
font.family = 'Wingdings')
myCellProps = cellProperties(padding = 5)
AuthorsMM_ft <- FlexTable(data = head(AuthorsMM,10),
header.columns = TRUE,
body.text.props = base_text_prop)
AuthorsMM_ft[AuthorsMM$Sentiment == 'J',3] = chprop(base_text_prop, color = 'green')
AuthorsMM_ft[AuthorsMM$Sentiment == 'K',3] = chprop(base_text_prop, color = 'yellow')
AuthorsMM_ft[AuthorsMM$Sentiment == 'L',3] = chprop(base_text_prop, color = 'red')
AuthorsMM_ft
出于某种原因,在 FlexTable
中定义 textProperties
无效。当我使用 background.color
时,它似乎改变了格式。
FlexTables 中 changing properties and conditional formatting 的更多示例。