【问题标题】:R ReporteRs: Adding a regulartable object to a pptx presentationR ReporteRs:将规则表对象添加到 pptx 演示文稿
【发布时间】:2018-11-29 22:00:18
【问题描述】:

我在使用 ReporteRs 包向 PowerPoint 演示文稿添加弹性表格时遇到问题,因为我要显示的列名在语法上无效。

因此,显然 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 采用空的或包含空格的列名,我会很高兴。

提前感谢您的帮助!

【问题讨论】:

    标签: r flextable reporters officer


    【解决方案1】:

    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")
    

    【讨论】:

    • 非常感谢您的帮助!
    • 同时拥有flextable()regulartable() 的理由是什么?除了支持非语法名称之外,还有什么可以做而另一个不能做的? flextable(head(iris)) 的输出似乎与 regulartable(head(iris)) 相同,除了第一个继承 complextable 类和第二个 regulartable...
    • 我建议您阅读试图回答您问题的文档的第一段:davidgohel.github.io/flextable/articles/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多