2014-06-19 35 views

回答

1

這裏是The R Statistics Website

採取的示例代碼基本上你必須

  • 創建一個新的Word文件
  • 創建標題和子標題
  • 移動到文檔中的新頁
  • 輸入文字
  • 插入表(即「data.frame」和「黑客帝國」的對象)
  • 插入地塊
  • 保存並關閉Word文檔

代碼:

# install.packages("R2wd") 
# library(help=R2wd) 
require(R2wd) 


wdGet(T) # If no word file is open, it will start a new one - can set if to have the file visiable or not 
wdNewDoc("c:\\This.doc") # this creates a new file with "this.doc" name 

wdApplyTemplate("c:\\This.dot") # this applies a template 


wdTitle("Examples of R2wd (a package to write Word documents from R)") # adds a title to the file 

wdSection("Example 1 - adding text", newpage = T) # This can also create a header 

wdHeading(level = 2, "Header 2") 
wdBody("This is the first example we will show") 
wdBody("(Notice how, by using two different lines in wdBody, we got two different paragraphs)") 
wdBody("(Notice how I can use this: '\ n' (without the space), to \n go to the next 
     line)") 
wdBody("האם זה עובד בעברית ?") 
wdBody("It doesn't work with Hebrew...") 
wdBody("O.k, let's move to the next page (and the next example)") 

wdSection("Example 2 - adding tables", newpage = T) 
wdBody("Table using 'format'") 
wdTable(format(head(mtcars))) 
wdBody("Table without using 'format'") 
wdTable(head(mtcars)) 


wdSection("Example 3 - adding lm summary", newpage = T) 

## Example from ?lm 
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) 
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) 
group <- gl(2,10,20, labels=c("Ctl","Trt")) 
weight <- c(ctl, trt) 

# This wouldn't work! 
# temp <- summary(lm(weight ~ group)) 
# wdBody(temp) 

# Here is a solution for how to implent the summary.lm output to word 
wdBody.anything <- function(output) 
{ 
    # This function takes the output of an object and prints it line by line into the word document 
    # Notice that in many cases you will need to change the text font into courier new roman... 
    a <- capture.output(output) 
    for(i in seq_along(a)) 
    { 
     wdBody(format(a[i])) 
    } 
} 

temp <- summary(lm(weight ~ group)) 
wdBody.anything(temp) 



wdSection("Example 4 - Inserting some plots", newpage = T) 

wdPlot(rnorm(100), plotfun = plot, height = 10, width =20, pointsize = 20) 
wdPlot(rnorm(100), plotfun = plot, height = 10, width =20, pointsize = 20) 
wdPlot(rnorm(100), plotfun = plot, height = 10, width =20, pointsize = 50) 

# wdPageBreak() 


wdSave("c:\\This.doc") # save current file (can say what file name to use) 
wdQuit() # close the word file 
+0

我想我錯過了這是如何回答OP的問題? –

+0

這是一個r命令,寫出來的單詞,閱讀答案 –

+0

似乎OP是問如何保存一個文件,然後將其複製到單詞中,而不是直接寫入單詞。另外,它看起來好像只有幾行內容是相關的(例如,在這種情況下,希伯來文和lm摘要幾乎不適用)。一點點修改可能有助於讓你的例子更有用? –

相關問題