R ReporteRs:将常规对象添加到 pptx 演示文稿
R ReporteRs: Adding a regulartable object to a pptx presentation
我在使用 ReporteRs 包向 PowerPoint 演示文稿添加 flextable 时遇到问题,因为我想显示的列名称在语法上无效。
因此,显然 flextable 只接受具有语法上有效的列名称的数据帧,这特别意味着名称不能为空字符串,也不能包含空格。
我的第一个解决方案是简单地使用 regulartable 函数,效果很好。但是当我想将我的常规对象添加到我的 PowerPoint 演示文稿时,它告诉我我只能添加可伸缩对象(......并且循环开始......)。
最小示例:
x <- c('a', 'b')
y <- c(1, 2)
d <- data.frame(x, y)
names(d) <- c(" ", "this column") #not syntactically valid column names!
#trying to call flextable(d) leads to an error
#so, try to use regulartable:
table <- regulartable(x)
#now try to create a pptx with this:
mySlides <- pptx()
mySlides <- addSlide(mySlides, 'Blank')
mySlides <- addFlexTable(mySlides, flextable = test) #Error: argument flextable must be a FlexTable object.
有人知道如何解决这个问题吗?
如果我知道如何在我的幻灯片中添加一个 regurtable,或者如何使 flextable 接受空的或包含空格的列名,我会很高兴。
在此先感谢您的帮助!
flextable
旨在与 officer
一起使用,而不是与 ReporteRs
一起使用。请注意,包 ReporteRs
将于 2018 年 7 月 16 日从 CRAN 中删除(由于与 java >=9 不兼容),包 officer
正在替换 ReporteRs
.
你的代码应该是这样的:
library(flextable)
library(officer)
x <- c('a', 'b')
y <- c(1, 2)
d <- data.frame(x, y)
names(d) <- c(" ", "this column") #not syntactically valid column names!
table <- regulartable(d)
ppt <- read_pptx()
ppt <- add_slide(ppt, layout = "Title and Content", master = "Office Theme")
ppt <- ph_with_flextable(ppt, value = table, type = "body")
print(ppt, target = "example.pptx")
关于列名。您不必为了显示它们而更改 data.frame 的名称。您可以使用不同的功能来做到这一点:
library(flextable)
library(officer)
x <- c('a', 'b')
y <- c(1, 2)
d <- data.frame(x, y)
meta <- data.frame(
col_keys = c("x", "y"),
upper_labels = c("hi", "world"),
labels = c(" ", "this column"),
stringsAsFactors = FALSE)
table <- regulartable(d)
table <- set_header_df( table, mapping = meta )
d <- data.frame(a = x, b = y)# must not contain x as set_header_labels first arg is also x...
ft <- regulartable(d)
ft <- set_header_labels( ft, a = "", b = "this column" )
ppt <- read_pptx()
ppt <- add_slide(ppt, layout = "Two Content", master = "Office Theme")
ppt <- ph_with_flextable(ppt, value = table, type = "body", index = 1)
ppt <- ph_with_flextable(ppt, value = ft, type = "body", index = 2)
print(ppt, target = "example.pptx")
我在使用 ReporteRs 包向 PowerPoint 演示文稿添加 flextable 时遇到问题,因为我想显示的列名称在语法上无效。
因此,显然 flextable 只接受具有语法上有效的列名称的数据帧,这特别意味着名称不能为空字符串,也不能包含空格。 我的第一个解决方案是简单地使用 regulartable 函数,效果很好。但是当我想将我的常规对象添加到我的 PowerPoint 演示文稿时,它告诉我我只能添加可伸缩对象(......并且循环开始......)。
最小示例:
x <- c('a', 'b')
y <- c(1, 2)
d <- data.frame(x, y)
names(d) <- c(" ", "this column") #not syntactically valid column names!
#trying to call flextable(d) leads to an error
#so, try to use regulartable:
table <- regulartable(x)
#now try to create a pptx with this:
mySlides <- pptx()
mySlides <- addSlide(mySlides, 'Blank')
mySlides <- addFlexTable(mySlides, flextable = test) #Error: argument flextable must be a FlexTable object.
有人知道如何解决这个问题吗?
如果我知道如何在我的幻灯片中添加一个 regurtable,或者如何使 flextable 接受空的或包含空格的列名,我会很高兴。
在此先感谢您的帮助!
flextable
旨在与 officer
一起使用,而不是与 ReporteRs
一起使用。请注意,包 ReporteRs
将于 2018 年 7 月 16 日从 CRAN 中删除(由于与 java >=9 不兼容),包 officer
正在替换 ReporteRs
.
你的代码应该是这样的:
library(flextable)
library(officer)
x <- c('a', 'b')
y <- c(1, 2)
d <- data.frame(x, y)
names(d) <- c(" ", "this column") #not syntactically valid column names!
table <- regulartable(d)
ppt <- read_pptx()
ppt <- add_slide(ppt, layout = "Title and Content", master = "Office Theme")
ppt <- ph_with_flextable(ppt, value = table, type = "body")
print(ppt, target = "example.pptx")
关于列名。您不必为了显示它们而更改 data.frame 的名称。您可以使用不同的功能来做到这一点:
library(flextable)
library(officer)
x <- c('a', 'b')
y <- c(1, 2)
d <- data.frame(x, y)
meta <- data.frame(
col_keys = c("x", "y"),
upper_labels = c("hi", "world"),
labels = c(" ", "this column"),
stringsAsFactors = FALSE)
table <- regulartable(d)
table <- set_header_df( table, mapping = meta )
d <- data.frame(a = x, b = y)# must not contain x as set_header_labels first arg is also x...
ft <- regulartable(d)
ft <- set_header_labels( ft, a = "", b = "this column" )
ppt <- read_pptx()
ppt <- add_slide(ppt, layout = "Two Content", master = "Office Theme")
ppt <- ph_with_flextable(ppt, value = table, type = "body", index = 1)
ppt <- ph_with_flextable(ppt, value = ft, type = "body", index = 2)
print(ppt, target = "example.pptx")