【问题标题】:Export pivot table into R markdown (for final output in word)将数据透视表导出到 R markdown(用于 word 中的最终输出)
【发布时间】:2021-07-11 12:41:18
【问题描述】:

我想要关于数据透视表(来自 pivottabler)和导出到 word 文档(通过 Rmarkdown 步骤)的帮助。

在我的日常工作中,我将表格导出为 Excel 文件,但对于年度报告,我希望将汇总表自动导出为 word 文档并保持格式。 我尝试导出 html 和 Latex 或将可透视的值转换为数据框,但没有成功。

有人可以帮帮我吗? 要么向我展示这个包的技术,要么告诉我在 R markdown 中可以轻松导出哪个其他包,我可以轻松地重现(并且完全相同)我的表?

library(pivottabler)
pt <- PivotTable$new()
pt$addData(bhmtrains)
pt$addColumnDataGroups("TrainCategory")
pt$addRowDataGroups("TOC", 
                    outlineBefore=list(isEmpty=FALSE, 
                                       groupStyleDeclarations=list(color="blue")), 
                    outlineTotal=list(groupStyleDeclarations=list(color="blue")))
pt$addRowDataGroups("PowerType", addTotal=FALSE)
pt$addRowDataGroups("SchedSpeedMPH", addTotal=FALSE)
pt$defineCalculation(calculationName="TotalTrains", summariseExpression="n()")
pt$renderPivot()

【问题讨论】:

    标签: r r-markdown markdown pivot-table


    【解决方案1】:

    这里有一个使用 R Markdown 到 Word 的教程: https://rmarkdown.rstudio.com/articles_docx.html

    产生 Word 输出的示例 Markdown 文件:

    ---
    title: "Example Output to Word"
    output: word_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## Example Pivot Table
    
    ```{r}
    library(pivottabler)
    pt <- PivotTable$new()
    pt$addData(bhmtrains)
    pt$addColumnDataGroups("TrainCategory")
    pt$addRowDataGroups("TOC", 
                        outlineBefore=list(isEmpty=FALSE, 
                                           groupStyleDeclarations=list(color="blue")), 
                        outlineTotal=list(groupStyleDeclarations=list(color="blue")))
    pt$addRowDataGroups("PowerType", addTotal=FALSE)
    pt$addRowDataGroups("SchedSpeedMPH", addTotal=FALSE)
    pt$defineCalculation(calculationName="TotalTrains", summariseExpression="n()")
    pt$renderPivot()
    ```
    

    【讨论】:

    • 是的,我读过它,但它不适用于我的复杂表(超过 20 个)......我不知道为什么......自从我发布后,我使用了 GT() 的其他解决方案和.png一种快速有效的替代方法。
    猜你喜欢
    • 2021-07-15
    • 2011-10-03
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2017-08-15
    相关资源
    最近更新 更多