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

Modify selected cell.Properties without replacing existing format #151

Closed Seyphaton closed 7 years ago

Seyphaton commented 7 years ago

Hey,

let me first describe the problem before I provide a minimal example below. I have to create many FlexTables with the same (really complex) basic design. Thats why I wrote a function that creates this basic layout and returns a FlexTable object.

The problem: The problem occurs when I try to modify the created tables afterwards to adjust some special layout requirements. As an example: A header's text direction is set to "btlr" but I want to change this for one cell back to "lrtb". The problem is that using: ft[3,2, to = 'header'] <- cellProperties( text.direction = 'lrtb') also resets all predefined properties such as borders, ... to the default values. Changing one single property doesnt work without setting various other different properties back to the required parameters. (there are more examples like this where I have to change one property only without the reset to the default values for all other properties)

What I think would solve my problem:

Minimal example:

dt <- data.table(Number = c(1,2,3,4),
                 Sub1Group1 = c(2,2,2,2),
                 Sub2Group1 = c(3,3,3,3),
                 Sub1Group2 = c(2,2,2,2),
                 Sub2Group2 = c(3,3,3,3)
                 )

# Basic formatting example (assumed to be inside a function)
ft <- FlexTable(dt,header.columns = F)

ft <- addHeaderRow(ft,
                   value =  c( "Numbers", "Sub 1", "Sub 2", "Sub 1", "Sub 2"),
                   colspan = c(1,1,1,1,1),
                   cell.properties = cellProperties(text.direction = "btlr", background.color = "gray"),
                   text.properties = textProperties(color = 'white'))

ft[1, , to='header', side = 'bottom'] <- borderProperties(width = 2, color = 'red')

ft <- addHeaderRow(ft,
                   value =  c( "", "Group 1", "Group 2"),
                   colspan = c(1,2,2),
                   first = T)

ft[2, , to='header', side = 'top'] <- borderProperties( style = 'none')

LATER: i want to change ONE property like:

ft[2,1, to = 'header'] <- cellProperties(text.direction = "lrtb")
# -> all formats gone (border, background) :(

example Somehow the export from word to png forgot some horizontal borders in the picture above (in the body).

Thanks a lot for your help!

Regards

PS: Please let me stress that there are more example like this e.g. for bold cells when there are special text.properties,...

davidgohel commented 7 years ago

Hi,

I think chprop is the function you are looking for.

http://davidgohel.github.io/ReporteRs/articles/formatting_properties.html#change-properties

David

Seyphaton commented 7 years ago

I don't think that solves I problem - at least I haven't found out how to use it in that way: What I want to change in the above example is the text direction only!

I need something like: chprop(oldProperty, text.direction = 'lrtb')

But my problem is how to get the oldProperty? Like i described above I apply many (!) custom changes within a function and it's simple not possible to somehow remember all of them for later use.

davidgohel commented 7 years ago

But my problem is how to get the oldProperty?

I think the link I sent shows that.

Also, you won't be able to change a posteriori the header orientation. http://www.ardata.fr/2015/04/flextable-rotate-text/

Seyphaton commented 7 years ago

I think I have not explained my problem clearly enough. I know how to use the syntax you gave me, but I don't have a variable containing the old cell property since everything is formatted within a big function which returns my FlexTable object. Another example below:

in one file:

createABasicFlexTable <- function(data)
{
  # Basic formatting example (assumed to be inside a function)
  ft <- FlexTable(data,header.columns = F)

  ft <- addHeaderRow(ft,
                     value =  c( "Numbers", "Sub 1", "Sub 2", "Sub 1", "Sub 2"),
                     colspan = c(1,1,1,1,1),
                     cell.properties = cellProperties(text.direction = "btlr", background.color = "gray"),
                     text.properties = textProperties(color = 'white'))

  ft[1, , to='header', side = 'bottom'] <- borderProperties(width = 2, color = 'red')

  ft <- addHeaderRow(ft,
                     value =  c( "", "Group 1", "Group 2"),
                     colspan = c(1,2,2),
                     first = T)

  ft[2, , to='header', side = 'top'] <- borderProperties( style = 'none')

  #***
  #*
  # a lot more formatting for much bigger tables which is the reason for this function
  #*
  #**
  return(ft)
}

in another file without access to the properties setin createABasicFlexTable:

dt <- data.table(Number = c(1,2,3,4),
                 Sub1Group1 = c(2,2,2,2),
                 Sub2Group1 = c(3,3,3,3),
                 Sub1Group2 = c(2,2,2,2),
                 Sub2Group2 = c(3,3,3,3)
                 )

myBasicFlexTable <- createABasicFlexTable(dt)

# now change the first column, second row in the heaer
ft[2,1, to = 'header'] <- chprop(?????????, text.direction = "lrtb")

Could you please tell me what I have to replace '?????????' with? (assume i want to change something else if header orientation is a problem - the question will be the same)

davidgohel commented 7 years ago

I know how to use the syntax you gave me, but I don't have a variable containing the old cell property since everything is formatted within a big function which returns my FlexTable object.

Then you coded something that does not fit your need and that is not using ReporteRs as it should be.

Could you please tell me what I have to replace '?????????' with?

Have a look at the documentation and use formatting properties objects, that's how ReporteRs is made. It's not related to ReporteRs to me. This is more about how you should code your R functions and work with R objects.