How to Upload R Script in Word
Create and format Word documents using R software and Reporters bundle
- Install and load the ReporteRs R package
- Create a simple Word document
- Add texts : title and paragraphs of texts
- Format the text of a Discussion document using R software
- Text properties : font, color, size
- Alter the global font size and font family
- Alter the advent of a "Piece Of Text" (pot)
- Add together plots and images
- Add a tabular array
- Add together a unproblematic table
- Add a zebra striped table
- Add lists : ordered and unordered lists
- Add simple lists
- Add multi-level lists
- Add together a footnote to a Word document
- Add R scripts
- Add a table of contents into a Word document
- Infos
Word document is one of the file formats widely used by many people, including researchers, for editing their text and tracking changes between different authors.
There are different solutions to generate a Word document from R software :
- R2wd bundle : unfortunately it depends on statconnDCOM or RDCOMClient servers, and works on Windows just.
- knitr + rmarkdown + pandoc : this is an easy manner to quickly create a Word report only the output is very basic.
- ReporteRs package, by David Gohel, provides easy to employ functions to write and format Word documents. It can be also used to generate Discussion document from a template file with logos, fonts, etc. ReporteRs is Java-based solution, then it works on Windows, Linux and Mac OS systems.
The aim of this R tutorial is to show you how to hands and quickly consign outputs (including data tabular array, plots, paragraphs of text and R scripts) from R statistical software to a Microsoft Discussion certificate (.docx file format) using ReporteRs package.
Install and load the ReporteRs R package
ReporteRs tin can be installed and loaded as follow :
install.packages('ReporteRs') # Install library('ReporteRs') # Load
Note that ReporteRs requires Java (>= 1.six) ; make sure yous have an installed JRE
The version of Java installed on your calculator, can be checked with this R lawmaking :
system("java -version")
Create a uncomplicated Word document
Word document can be created using the docx() part. And then, contents tin can be added to the certificate using the functions below:
- addTitle: Add a championship
- addParagraph: Add together paragraphs of text
- addFlexTable: Add a table
- addPlot: Add a plot generated in R
- addImage: Add external images
- addTOC: Add a table of contents
- addRScript: highlight and add R code
- addMarkdown: Add markdown
- addPageBreak: Add a page pause
Afterward adding contents, you accept to use the writeDoc() function to write the Word document into a .docx file.
Add texts : championship and paragraphs of texts
Texts can be added as a title or a paragraph using the R functions addTitle() or addParagraph(). A simplified format of these functions are :
addTitle(medico, value, level=ane) # Add titles addParagraph(doc, value) # Add a paragraph of texts
- doc : docx object.
- value : the text to add together as a title or a paragraph.
- level : an integer specifying the heading level. east.1000 : 1 for title1, two for title2, 3 for title3, etc.
addTitle() and addParagraph() functions tin be used as follow :
library(ReporteRs) # Create a discussion document to contain R outputs medico <- docx() # Add a championship to the certificate dr. <- addTitle(medico, "Simple Word certificate", level=1) # Add a paragraph of text into the Give-and-take document doc <- addParagraph(doc, "This simple Word document is created using R software and ReporteRs bundle. Information technology contains only a unproblematic text.") # Add a sub title md <- addTitle(doc, "What is R language?", level = 2) dr. <- addParagraph(doc, "R is a linguistic communication and environs for statistical computing and graphics. R provides a broad diverseness of statistical and graphical techniques, and is highly extensible.") medico <- addTitle(doc, "What is ReporteRs", level = 2) doc <- addParagraph(physician, "ReporteRs is a bundle to write and format easily a Word document from R software.") # Write the Word document to a file writeDoc(medico, file = "r-reporters-simple-word-document.docx")
Format the text of a Word document using R software
Text properties : font, colour, size
Amidst the text properties bachelor in a Give-and-take document, there are :
font family (e.grand : "Arial"), font size (east.g : 11), font weight (due east.thousand: bold), font manner (eastward.g : italic), underlined text, vertical align (superscript, subscript) and color (e.g: "blue").
These properties are shown in the figure below :
.
This section describes how to format and write a Word document using R software
Change the global font size and font family
The default font size and font family can exist changed as follow :
options( "ReporteRs-fontsize" = 12, "ReporteRs-default-font" = "Arial")
Note that, this irresolute will be practical to the whole content of the Discussion certificate. An case is shown in the next section.
Change the appearance of a "Piece Of Text" (pot)
.
The function pot() [Pieces Of Text] is used to format text chunks earlier adding into the document. pot() function can be besides used to create a hyperlink and a footnote. A simplified format is :
pot(value="", format = textProperties())
- value : the text to be formatted
- format : the backdrop to employ for formatting the text
The possible values for the parameter format
are the following functions :
- textProperties() : the text formatting backdrop
- textBold() : shortcut for bold text
- textItalic() : shortcut for italic text
- textBoldItalic() : shortcut for assuming-italic text
- textNormal() : shortcut for normal text
The arguments for these functions are :
- color : font color; eastward.g : colour="#000000" or color = "black".
- font.size : a integer indicating the font size.
- font.weight : the font weight. Possible values are "normal" or "bold".
- font.style : the font style. Possible values are "normal" or "italic".
- underlined : a logical value specifying if the text should be underlined.
- font.family unit : the font family; e.g : "Arial".
- vertical.align : a character indicating font vertical alignments. Expected values are "baseline"" or "subscript" or "superscript". Default value is baseline.
- shading.color : background color of the text (e.chiliad "#000000" or "black")
pot() function tin be used every bit follow :
# Formatted text my_text <- 'This is a ' + pot('formatted text', textProperties(color="bluish")) + ' created using' + pot('ReporteRs', textBold()) + 'package' # Create a hyperlynk my_link <- pot( 'Click here to visit STHDA website!', hyperlink = 'http://www.sthda.com/english', format=textBoldItalic(color = '#428BCA', underline = True ))
The R code below creates a Discussion certificate containing a formatted text and a hyperlink :
physician <- docx() # Change the default font size and font family options('ReporteRs-fontsize'=12, 'ReporteRs-default-font'='Arial') # Add a formatted paragraph of texts #++++++++++++++++++++++++++++++ medico <- addTitle(medico, "Formatted text", level=1) # Define a style to highlight a text highlight_style <- textProperties(color='#1163A5',font.size = 20, font.weight = 'bold', font.family = 'Courier New' ) my_text = 'This ' + pot('Word document', highlight_style) + ' is created using' + pot(' R software', textProperties(color="red", font.size=xviii)) + ' and'+ pot(' ReporteRs', textBoldItalic(color="#F0A91B", underlined=Truthful)) + ' package.' md <- addParagraph(md, my_text) # Add a hyperlink doc <- addTitle(medico, "STHDA Web site", level=1) my_link <- pot('Click here to visit STHDA web site!', hyperlink = 'http://www.sthda.com/english', format=textBoldItalic(color = 'blueish', underline = TRUE )) doc <- addParagraph(doc, my_link) # Write the Give-and-take document to a file writeDoc(doc, file = "r-reporters-formatted-give-and-take-document.docx")
Add plots and images
The functions addPlot() and addImage() tin can exist used for adding a plot or an external image to the document. addPlot() works with all R plots (base graphics, lattice, ggplot2 and filigree).
The format of these 2 functions are :
# Add plots # fun : R plotting office # ... : other arguments to laissez passer to the plotting function addPlot(doc, fun, ...) # Add images # filename : path to the external prototype addImage(doc, filename)
The R code below creates a Word document containing an introduction, a box plot, a histogram and an prototype (downloaded from STHDA web site).
medico <- docx() # Create a Word document # Add a title doc <- addTitle(doc, "Word document with plots and Images", level = 1) # Add an introduction dr. <- addTitle(physician, "Introduction", level = 2) doc = addParagraph(doc, value ="This Word document is created using R software and ReporteRs parcel. The goal of this section is to show you lot how to add plots and images into a Discussion document. This can be done easily using the functions addPlot() and addImages().") # Add a box plot dr. <- addTitle(dr., "Box plot using R software", level = 2) boxplotFunc<-function(){ boxplot(len ~ dose, data = ToothGrowth, col = rainbow(iii), main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length") } doctor <- addPlot(doc, boxplotFunc) dr. <- addPageBreak(md) # Get to the side by side page # Add a histogram doc <- addTitle(doc, "Histogram plot", level = ii) doc <- addPlot(md, role() hist(iris$Sepal.Width, col="lightblue")) # Change point size of plotted text (in pixels, default = 12) md <- addTitle(doc, "Histogram with pointsize = 18", level = 2) doc <- addPlot(physician, function() hist(iris$Sepal.Width,col="lightblue"), pointsize=eighteen) # Add an image # +++++++++++++++++++++++++++ # download an image from STHDA web site download.file(url="http://www.sthda.com/sthda/RDoc/effigy/like shooting fish in a barrel-ggplot2/ggplot2-histogram-demo.png", destfile="ggplot2-histogram-demo.png", quiet=True) doc <- addTitle(doc, "Image from STHDA web site", level = 2) doc <- addImage(doctor, "ggplot2-histogram-demo.png") # Write the Word document to a file writeDoc(doc, file = "r-reporters-word-document-with-plot.docx")
The Discussion document created by the R code above is available here : R software and ReporteRs parcel - Give-and-take document containing plots
-
Note that, for addPlot() function, you can employ the arguments width and tiptop to specify the plot width and top in inches (default values are six X 6)
-
Some other optional argument of addPlot() function is par.properties which value tin can be parRight(), parLeft(), parJustify() for the plot alignments:
doc <- addPlot( physician = md, fun = part(){ barplot( 1:5, par.properties = parCenter() )} )
- For addImage() function, the allowed file formats are PNG, WMF, JPEG and GIF images..
Add a table
The function addFlexTable() is used to add a uncomplicated or customized table.
- The commencement step is to create a table using 1 of the functions below :
- FlexTable() to create a 'flexible' tabular array which tin can be easily formatted
- vanilla.table() which is shortcut to quickly produce a nice FlexTable
- The second step is to add together the created table into the Give-and-take document using addFlexTable() function as follow :
# doc : docx object # flextable : FlexTable object addFlexTable(doc, flextable)
Add together a uncomplicated table
doc <- docx() information<-iris[i:five, ] # Add a first tabular array : Default table doc <- addTitle(doc, "Default table") doc <- addFlexTable( md, FlexTable(data)) doc <- addParagraph(doc, c("", "")) # 2 line breaks # Add a 2d table, theme : vanilla table doc <- addTitle(doc, "Vanilla table") dr. <- addFlexTable( dr., vanilla.table(information)) writeDoc(doc, file = "r-reporters-word-document-add-table.docx")
Add a zebra striped table
setZebraStyle() function can be used to color odd and even rows differently; for instance, odd rows in gray color and even rows in white colour.
doc <- docx() information<-iris[1:5, ] # Zebra striped tables doc <- addTitle(doc, "Zebra striped tables") MyFTable <- vanilla.table(data) MyFTable <- setZebraStyle(MyFTable, odd = '#eeeeee', fifty-fifty = 'white') doc <- addFlexTable( physician, MyFTable) writeDoc(physician, file = "r-reporters-word-certificate-zebra.docx")
Add lists : ordered and unordered lists
Ordered and unordered lists can be added using addParagraph() part as follow :
md = addParagraph(dr., value = c('Item 1', "Particular 2", "Item three") par.properties = parProperties(list.style = 'ordered', level = i )
- value : a fix of texts to be added as a listing
- par.properties : the paragraph formatting backdrop
- listing.manner : possible values are 'unordered' and 'ordered'
- level : a numeric value indicating the level of the item to exist added in the list
Add elementary lists
doc <- docx() # Ordered list medico <- addTitle(doc, "Ordered List") doc <- addParagraph(physician, value= c("Item 1", "Item 2", "Detail three"), par.properties = parProperties(list.style = 'ordered')) # Unordered list doc <- addTitle(doc, "Unordered List") physician <- addParagraph(doc, value= c("Detail 1", "Item 2", "Item iii"), par.properties = parProperties(list.mode = 'unordered')) writeDoc(dr., file = "r-reporters-discussion-document-lists.docx")
Add multi-level lists
To simplify the code, we'll offset define some levels to exist used for creating multi-level lists.
medico <- docx() # Define some levels for ordered lists (ol) ol1 = parProperties(list.fashion = "ordered", level = ane) ol2 = parProperties(listing.mode = "ordered", level = two) # Define some levels for unordered lists (ul) ul1 = parProperties(list.mode = "unordered", level = i) ul2 = parProperties(list.manner = "unordered", level = 2) # Multi-lvel ordered list doc <- addTitle(doc, "Ordered List") physician <- addParagraph(doc, value= "Item ane", par.properties = ol1) doc <- addParagraph(physician, value= "Item 1.1", par.properties = ol2) doc <- addParagraph(doc, value= "Item 1.2", par.properties = ol2) doc <- addParagraph(dr., value= "Item ii", par.backdrop = ol1) # Multi-lvel unordered listing doc <- addTitle(doc, "Unordered Listing") dr. <- addParagraph(doc, value= "Detail 1", par.backdrop = ul1) dr. <- addParagraph(doc, value= "Item 1.1", par.properties = ul2) physician <- addParagraph(doc, value= "Detail 1.2", par.properties = ul2) md <- addParagraph(md, value= "Particular 2", par.backdrop = ul1) writeDoc(medico, file = "r-reporters-word-document-multilevel-lists.docx")
Add R scripts
The part addRScript() can be used as follow :
doc <- docx() r_code <- 'summary(cars$dist) x <- rnorm(100) hist(ten) ' # Change the backgroud-color doc <- addRScript(doc, text = r_code, par.properties= parProperties(shading.color = 'gray90')) writeDoc(doc, file = "r-reporters-word-add-r-code.docx")
Add a table of contents into a Word document
To add together a table of contents, the role addTOC() can be used.
Notation that, when a tabular array of content (TOC) is added into a Give-and-take document, a message box is displayed when opening the file for the first time. This message asks if yous want to update TOC entries. This is not an mistake and you should click 'Yeah' to update TOC entries. Y'all should salve the certificate to avoid this to happen the side by side fourth dimension.
dr. <- docx() # Create a Word certificate # Add a championship doc <- addTitle(doctor, "Create a Word document with TOC", level = 1) # Add a table of contents doctor <- addTOC(dr.) dr. <- addPageBreak(doc) # go to the next page # Add an introduction md <- addTitle(medico, "Introduction", level = 2) md = addParagraph(doc, value ="This Word document is created using R software and ReporteRs package. The goal of this section is to show you how to add a table of contents into a Discussion certificate. This can be done easily using the function addTOC.") # Add a box plot doc <- addTitle(doc, "Box plot using R software", level = 2) boxplotFunc<-function(){ boxplot(len ~ dose, data = ToothGrowth, col = rainbow(3), main = "Republic of guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length") } doc <- addPlot(medico, boxplotFunc) doc <- addPageBreak(dr.) # go to the side by side page # Add plots #+++++++++++++++++++++ doc <- addTitle(medico, "Bones plots using R software", level = two) dr. <- addParagraph(doc, "R is a free software for plotting and information analysis. This chapter contains examples of graphs generated using R.") # Add a histogram doc <- addTitle(doc, "Histogram", level = iii) medico <- addPlot(physician, role() hist(iris$Sepal.Width, col="lightblue")) doc <- addPageBreak(doc) # go to the side by side page # Add together a bar plot md <- addTitle(doc, "Bar plot", level = 3) physician <- addPlot(medico, function() barplot(VADeaths)) # Write the Word document to a file writeDoc(doc, file = "r-reporters-give-and-take-document-toc.docx")
The Discussion document created past the R lawmaking higher up is available hither : R software and ReporteRs package - Discussion certificate with a tabular array of contents
Infos
This analysis has been performed using R (ver. 3.1.0).
You can read more about ReporteRs and download the source lawmaking at the following link :
GitHub (David Gohel): ReporteRs
Enjoyed this article? I'd be very grateful if you lot'd help it spread past emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.
Show me some dear with the like buttons beneath... Cheers and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en fifty'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for y'all
This section contains best data scientific discipline and self-evolution resources to help you on your path.
Want to Learn More than on R Programming and Information Science?
Follow u.s. by Email
On Social Networks:
Get involved :
Click to follow usa on Facebook and Google+ :
Annotate this article by clicking on "Discussion" push button (top-correct position of this page)
Source: http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package
Belum ada Komentar untuk "How to Upload R Script in Word"
Posting Komentar