楼主: oliyiyi
985 5

Using R in consulting: playing nice with Microsoft Word [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

As I use R more in consulting, I’m finding the need to make the quick transition from R to Microsoft products (usually Word) more serious. (I’m using a Windows platform, but I’m sure the challenges on the Mac would be similar.) I simply don’t have time to do the text manipulations necessary to convert text to Word tables, for example. There are a few solutions I’ve tried:

  • RWeave and LaTeX2RTF – a bit clunky and producing disastrous results. I really like RWeave, but it’s the LaTeX to RTF conversion that’s the weak link in the chain here. It simply doesn’t work well enough for consulting use. Also, it takes too long to set up when you just want to copy a couple of tables over to Word.
  • odfWeave – I’m interested in this solution, but there are a couple of issues. Microsoft support of open document format (ODF) isn’t quite there yet, I have to install OpenOffice writer as an intermediary. And I haven’t quite gotten odfWeave to work yet, as it requires a command line zip utility and wzcline (the command line interface to WinZip required as a separate download) doesn’t seem to work very well yet. Maybe some of the other command lines would work better, but there was a warning as of this writing that the zip utility recommended in the odfWeave help files has a security flaw.
  • HTML – I haven’t tried the full R2HTML package, but it still seems that this is too much for what I need. It’s probably possible to use this to output a basic html file which can then be read into Word, but as of right now that requires too much setup for what I’m doing.

So, basically, I’ve come up with my own lightweight solution, the my.html.table function which takes a matrix, data.frame, or array and outputs the html code for a table in the console or a file. Here’s the function:


# function: my.html.table
# purpose: convert a matrix, array, or table to an html table
# inputs:
# file - NULL (default) to output to screen, or file name to output to file
# header - F (default) to use for table header, T to use (ignored if
# colnames is F)
# rownames - F (default) to leave out the rownames attribute of the table, T to
# include it in the first column
# colnames - F (default) to leave out the colnames attribute of the table, T to
# include it in the top row (affects header)
# ... - additional parameters are passed to format, affecting the body of the
# table only (not the headers or row names)
# interactions: header is used only if colnames is T
# output: html text (output to a file if file is not NULL)
# tip: use this function, and copy/paste to Excel, and then copy/paste the Excel
# table to easily create a table in MS Word
# note: for more functionality, use the R2HTML package, as this function is
# intended for lightweight use only

  1. my.html.table <- function(tab,file=NULL,header=F,rownames=F,colnames=F,...) {
  2. if (!is.null(file)) sink(file)
  3. nr <- nrow(tab)
  4. cat("<table>\n")
  5. if (header) {
  6. th.tag <- "<th>"
  7. th.tag.end <- "</th>"
  8. } else {
  9. th.tag <- "<td>"
  10. th.tag.end <- "</td>"
  11. }
  12. if (colnames) {
  13. cat("<tr>",th.tag,sep='')
  14. if (rownames) {
  15. cat(th.tag.end,th.tag,sep='')
  16. }
  17. cat(colnames(tab),sep=paste(th.tag.end,th.tag,sep=''))
  18. cat(th.tag.end,"</tr>\n",sep='')
  19. }
  20. for (i in 1:nr) {
  21. cat("<tr><td>")
  22. if (rownames) {
  23. cat(rownames(tab)[i],'</td><td>')
  24. }
  25. cat(format(tab[i,],...),sep='</td><td>')
  26. cat("</td></tr>\n")
  27. }
  28. cat("</table>\n")
  29. if (!is.null(file)) sink()
  30. }
  31. And here’s an example:
  32. a<-c(0.5290,0.5680,0.6030,0.6380,0.7050,0.7000,0.7090) b<-c(0.0158,0.0157,0.0155,0.0152,0.0144,0.0145,0.0144) c<-c(87.1350,108.5070,128.6900,149.6190,170.7140,190.6750,211.9240)
  33. foo <- rbind(a,b,c,d,e,f,g)
  34. colnames(foo) <- paste("Col",1:7)
  35. rownames(foo) <- c("Lbl 1","Lbl 2","Lbl 3","Lbl 4","Lbl 5","Lbl 6","Lbl 7") my.html.table(foo)
  36. my.html.table(foo,rownames=T,colnames=T,header=T,digits=2)
  37. my.html.table(foo,width=5)
复制代码


To easily create a Word table from a matrix, data.frame, or array, just use my.html.table with suitable parameters (including anything that can be passed to format), copy the html from the console, paste into Excel, select and copy from Excel, and paste into Word. It’s a little clunky, and maybe through the use of R’s clipboard connection I can cut out a step or two, but it’s a vast improvement over manual formatting and the bloated solutions listed above.

If you have a better way, feel free to comment.

By the way, using the right-click menu on graphs to copy as a metafile works wonderfully.

If reports are to be updated regularly using changing/updated data, this of course is the wrong solution, and one of the literate programming/weave solutions or appropriately programmed R2HTML is much better.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Consulting Microsoft CONSULT Playing Using Microsoft playing Word

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
沙发
newfei188 发表于 2016-10-19 10:29:54 |只看作者 |坛友微信交流群
where is the attachment?

使用道具

藤椅
oliyiyi 发表于 2016-10-19 11:01:13 |只看作者 |坛友微信交流群
newfei188 发表于 2016-10-19 10:29
where is the attachment?
this's not a book, neither a lecture.  what do u mean about the attachment?

使用道具

板凳
Kamize 学生认证  发表于 2016-10-19 14:17:22 来自手机 |只看作者 |坛友微信交流群
oliyiyi 发表于 2016-10-19 10:25
As I use R more in consulting, I’m finding the need to make the quick transition from R to Microsof ...
谢谢楼主分享的资料不错啊!

使用道具

报纸
newfei188 发表于 2016-10-19 20:31:15 |只看作者 |坛友微信交流群
oliyiyi 发表于 2016-10-19 11:01
this's not a book, neither a lecture.  what do u mean about the attachment?
hehe, we are cheated..

使用道具

地板
水调歌头 在职认证  发表于 2016-10-20 10:32:46 |只看作者 |坛友微信交流群
谢谢楼主分享!
已有 1 人评分论坛币 收起 理由
oliyiyi + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 08:50