随着R语言的发展,它的社区也逐步扩展,现在R语言也越来越丰富了,不仅仅限于处理科学计算和数据可视化等它最擅长的领域,在一些你想不到的领域起始也可以小试牛刀,比如,操控office。
用解释性语言来操控office可以灵活地批量化处理一些文档事务,R语言自然也不能落后。R语言最常见的就是操作excel了,因为这个比较常用,这里就不再赘述。这里重点介绍说明一下R语言对word的操控。
1 存在的问题
一个比较不错的方案就是使用officer包。经过本人亲测,这个包在使用时存在一个致命问题,那就是非常重要的 body_replace_all_text 函数,通常达不到预期效果。当然,作者自己也在说明中提示了该风险:
Note that the behind-the-scenes representation of text in a Word document is frequently not what
you might expect! Sometimes a paragraph of text is broken up (or "chunked") into several "runs,"
as a result of style changes, pauses in text entry, later revisions and edits, etc. If you have not
styled the text, and have entered it in an "all-at-once" fashion, e.g. by pasting it or by outputing
it programmatically into your Word document, then this will likely not be a problem. If you are wo