R-Blogger블로그·해설한국어2008-10-17
R→Word 테이블 문제에 대한 또 다른 해결책
RTF 파일 생성 예시 이전에는 HTML 솔루션을 사용했으나, 이번에는 RTF 파일을 생성하는 예시를 보여 드립니다. 함수 정의: my.rtf.table # function: my.rtf.table # purpose: convert a matrix, data.frame, or array into a rtf table # output: text for RTF, possibly written to a file # inputs: # tab - a table, dataframe, or array (needs rownames and colnames) # outfile - name of file (or console if NULL, which is default) # rtffile - if T (default) then add {\rtf1 to beginning and } to end, making # a full RTF file, if F then leave these off # header - if T (default) then bold the table header # ... - passed to format for the table body only # tips: output to tempfile and use WordInsertFile(...) from the svViews # package to easily convert a table to Microsoft Word my.rtf.table 함수 내부 구현은 아래와 같습니다. my.rtf.table if (!is.null(outfile)) sink(outfile) tab.nrow tab.ncol if (rtffile) { #begin RTF document cat("{\rtf1\n") } #populate header row cat("\trowd\trautofit1\intbl\n") j for (i in 1:(tab.ncol+1)) { cat("\cellx",j,'\n',sep='') j } cat("{\n") # loop through and write column headers cat(" \cell\n") for (i in 1:tab.ncol) { if (header) { cat('\b ',colnames(tab)[i],"\b0\cell \n",sep='') } else { cat(colnames(tab)[i],"\cell \n",sep='') } } cat("}\n") cat("{\n") cat("\trowd\trautofit1\intbl\n") j for (i in 1:(tab.ncol+1)) { cat("\cellx",j,'\n',sep='') j } cat("\row }\n") #write table contents for (k in 1:tab.nrow) { cat("\trowd\trautofit1\intbl\n") j for (i in 1:(tab.ncol+1)) { cat("\cellx",j,'\n',sep='') j } cat("{\n") cat(rownames(tab)[k],'\cell\n',sep='') for (i in 1:tab.ncol) { cat(format(tab[k,i],...),"\cell \n",sep='') } cat("}\n") cat("{\n") cat("\trowd\trautofit1\intbl\n") j for (i in 1:(tab.ncol+1)) { cat("\cellx",j,'\n',sep='') j } cat("\row }\n") } if (rtffile) { # end the rtffile cat("}\n") } if (!is.null(outfile)) sink() 위 코드를 사용하려면 svViews 패키지가 필요합니다. library(svViews) myfile my.rtf.table(table, outfile=myfile) # use svViews commands
원문 URL
전체 글은 원문 페이지에서 이어서 읽을 수 있습니다.
- 작성자
- R-Blogger
- 출처
- R-Blogger
- 플랫폼
- R-Blogger
- 분류
- 블로그·해설
- 언어
- 한국어
- 발행일
- 2008-10-17