1,Table的细节处理
(1)列名的处理
doc <- addTable( doc , iris
, header.labels = list("Sepal.Length" = " Sepal Length"
, "Sepal.Width" = " Sepal Width"
, "Petal.Length" = " Petal Length", "Petal.Width" = " Petal Width"
, "Species" = "Species"
)
)
(2)列名的分组
doc <- addTable( doc
, iris
, grouped.cols=list( "Sepal" = c( "Sepal.Length", "Sepal.Width" )
, "Petal" = c("Petal.Length", "Petal.Width")
, "Species" = c("Species")
)
)
(3)更细维度的处理
doc <- new ( "Docx" , title = "My example" )header.cellProperties = cellProperties ( border.left.width = 0 , border.right.width = 0, border.bottom.width = 2 , border.top.width = 0, padding = 5 )data.cellProperties = cellProperties ( border.left.width = 0 , border.right.width = 0, border.bottom.width = 1 , border.top.width = 0, padding = 3 )header.textProperties = textProperties ( font.size = 12 , color = "#1B1BAA" , font.weight = "bold" )data.textProperties = textProperties ( font.size = 10 , color = "#303030" )leftPar = parProperties ( text.align = "left" )rightPar = parProperties ( text.align = "right" )centerPar = parProperties ( text.align = "center" )my.formats = tableProperties (character.par = leftPar , percent.par = rightPar, double.par = rightPar , integer.par = rightPar, groupedheader.par = centerPar , groupedheader.text = header.textProperties, groupedheader.cell = header.cellProperties, header.cell = header.cellProperties , header.par = centerPar, header.text = header.textProperties, data.cell = data.cellProperties , data.text = data.textProperties, percent.addsymbol = " %", integer.digit = 1L, fraction.double.digit = 3L, fraction.percent.digit = 2L)doc <- new ( "Docx" , title = "My example" )doc <- addTable ( doc , weights.summary, formats = my.formats, col.types = list ( "id" = "character" , "avg.weight" = "double", "regularity" = "percent" , "visit.n" = "integer", "last.day" = "date" ))writeDoc ( doc , docx.file )
2,图表的处理
(1) 多个画图语句处理,可以用function{}
doc <- addPlot( doc , function( ) {
print( qplot(Sepal.Length, Petal.Length
, data = iris, color = Species, size = Petal.Width ) )
plot( x = rnorm( 10 ), y = rnorm( 10 ), main = "main title" )
}
, legend = "graph example", stylename="PlotReference"
)
3,文字处理
(1)文字替换&字体颜色设置
x = "[animal] eats [food]."
# define formatting properties for replacement text – see chapter "styles"
textProp = textProperties( color = "blue" )
replacement.styles = list( animal= textProp, food= textProp )
# define replacement text
replacements = list( animal = "donkey", food = "grass" )
doc <- addParagraph( doc, value = x, stylename = "Normal"
, replacements = replacements
, replacement.styles = replacement.styles
)
4,个别文字替换
Funtion 'replaceText' can be executed to replace texts into a docx document. E.g. if we want to replace a keyword ‘AUTHOR’ by "John Doe", simply run:
doc = replaceText( doc, "AUTHOR", "John Doe")