davidgohel / ReporteRs

Note that ReporteRs has been removed from CRAN the 16th of July 2018 and is not maintained anymore. please migrate to officer.
244 stars 44 forks source link

cellProperties / text.direction argument #130

Closed gilleschapron closed 8 years ago

gilleschapron commented 8 years ago

David,

I was wondering if it was possible to define rotation of text in Flextable's cells. In cellProperties help, there is a text.direction argument with 3 possibilies: "lrtb", "tbrl", "btlr". But when I try to use it, I see no difference. Is it normal ? Thanks for any help.

Hereafter an exemple:

library(boot)
library(dplyr)
library(magrittr)
library(ReporteRs)

data(melanoma)

melanoma$Status = c( 'Melanoma', 'Alive' , 'Non-melanoma' )[melanoma$status]
melanoma$Gender = ifelse( melanoma$sex > 0, 'Male', 'Female' )
melanoma$Ulceration = ifelse( melanoma$ulcer > 0, 'Present', 'Absent' )

data = melanoma %>% 
  group_by( Status, Ulceration ) %>% 
  summarize(n = n(), Mean = round( mean( thickness, na.rm = T ), 3 ), 
            SD = round( sd( thickness, na.rm = T ), 3 ), 
            Median = median( thickness, na.rm = T ), 
            Min = min( thickness, na.rm = T ), 
            Max = max( thickness, na.rm = T ), 
            Missing = sum( is.na( thickness ) )
)

data = as.data.frame( data )
data
mytable1 <- FlexTable(data)
mytable2 <- FlexTable(data)
mytable3 <- FlexTable(data)
mytable1[3:4, 'Status'] <- cellProperties(text.direction =  "lrtb")
mytable2[3:4, 'Status'] <- cellProperties(text.direction =  "tbrl")
mytable3[3:4, 'Status'] <- cellProperties(text.direction =  "btlr")
mytable1
mytable2
mytable3
davidgohel commented 8 years ago

Gilles,

FlexTable lets you manage only rotated headers text.

library(ReporteRs)
library(magrittr)

header_cells_props = cellProperties( text.direction = "btlr", background.color = "#00557F", padding = 3 )
header_text_props = textProperties( color = "white", font.size = 11, font.weight = "bold" )

MyFTable = FlexTable( data = iris[46:55,], 
                      header.cell.props = header_cells_props, 
                      header.text.props = header_text_props, 
                      body.text.props = textProperties( font.size = 10 ) 
                      ) %>%
  setFlexTableWidths( widths = c(.5, .5, .5, .5, .8 ))
MyFTable[] = parCenter()

doc = docx( title = "title" ) %>% 
  addFlexTable( MyFTable )
writeDoc( doc, file = "rotated_table.docx" )

This is far from being perfect. But I am working on a new API (easier and free of java) and I will try to integrate that feature with no restrictions - issue was about being able to compute size of a string (with formats) but with gdtools it is now possible.

David

gilleschapron commented 8 years ago

Thanks a lot for you answer and the example. My intention was indeed to use it on text, but on rows (first column). Never mind, I'll be waiting for your work. A new API free of java, what an exiting news! Good luck with that.

Gilles

davidgohel commented 8 years ago

Not yet ready, but here is the first version. Will be integrated into ReporteRs later.

https://davidgohel.github.io/flextable/articles/introduction.html

David