-1

What I have tried so far:

par( mfrow = c( 1, 2 ) )

matplot(rOU,type="l", ylim=range(rOU))
matplot(rEM,type="l", ylim=range(rEM))
Higgs
  • 550
  • 5
  • 18
user3756940
  • 7
  • 1
  • 2

1 Answers1

2

here is sample code taken from The R Statistics Website

Basically you have to

  • Create a new Word file
  • Create headers and sub-headers
  • Move to a new pages in the document
  • Write text
  • Insert tables (that is “data.frame” and “matrix”objects)
  • Insert plots
  • Save and close the Word document

Code:

# 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
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • I think I'm missing how this answers the OP's question? – Andy Clifton Jun 19 '14 at 15:03
  • this is an r command that writes out to word, read the answer – Rachel Gallen Jun 19 '14 at 15:08
  • Seems the OP is asking how to save a file then copy it in to word, not write directly to word. Also, it looks like there are only a few lines in there that are relevant (for example, the hebrew stuff and lm summary are hardly applicable in this case). A bit of an edit might help make your example more useful? – Andy Clifton Jun 19 '14 at 15:23