r - R ReporteRs:将规则表对象添加到 pptx 演示文稿
问题描述
我在使用 ReporteRs 包向 PowerPoint 演示文稿添加 flextable 时遇到问题,因为我要显示的列名在语法上无效。
因此,显然 flextable 只接受具有语法有效列名的数据帧,这尤其意味着名称不能是空字符串,也不能包含空格。我的第一个解决方案是简单地使用 regulartable 函数,它工作得很好。但是当我想将我的规则对象添加到我的 PowerPoint 演示文稿中时,它告诉我我只能添加 flextable 对象(......并且循环开始......)。
最小的例子:
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
not with 一起使用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")
推荐阅读
- php - PHP-GD 已安装,但在 wordpress 安装期间需要
- c# - 无法向空 TcpClient 发送数据(检查是否调用了 Connect)”
- javascript - 如何从网站获取html数据
- python - 如何在 PyCharm 中通过 Tensorflow 访问 GPU?
- vba - Networkdays 循环 - 我想获得 Networkdays 循环。但给出一个错误。我正在使用office 2016。提前感谢您的努力
- xcode - websocket 适用于所有浏览器,但不适用于cordova + 模拟器?
- typescript - React Native 上的 GraphQL Apollo 类型生成
- asp.net - 尝试发布 .net 5 Web 应用程序时出错
- ruby-on-rails - ArgumentError:使用 `bundle exec rake db:migrate` 的参数数量错误
- c - 生成所有 10 位电话号码 - 回溯