#R_example#R2DOCX的细节设计

 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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值