0

I have a simple dataframe I am trying to export to a word document. I am working with ReporteRs. I want all number to be at the center of the cell (both in the body and the header).

a <- 1:5
b <- 1:5
df <- data.frame(a,b)

According to the documentation "Use ft_object[] <- value to perform the operation on the whole part (body, header or footer) of the FlexTable."

So I try

df_Flex <- FlexTable(df)
setFlexTableWidths(df_Flex, rep(0.5,2))
df_Flex[] <- parProperties(text.align = 'center')

Yet, the results is a table with only the number in the body being at the center. The header is not. If I want both the header and the body to be at the center I have to write two lines of code

df_Flex[] <- parProperties(text.align = 'center')
df_Flex[,,to='header'] <- parProperties(text.align = 'center')

which is annoying because if I want to perform other formatting I will need to write the code twice each time.

Does anyone know why this happens and how to solve the problem?

1 Answer 1

3

Use the functions provided in the flextable package itself, rather than in reporteRs, as these provide a much simpler interface for modifying the body and header parameters. An example using the pipe operator from magrittr or dplyr:

df_Flex2 = 
  regulartable(df) %>%
  width(width = c(0.5, 2)) %>%
  align(align = "center", part = "all")

Edit: Formatting decimal places

To control the number of decimal places shown for non-integer values in a regulartable use the set_formatter function with custom formatting for each column you want to display with different decimal places:

df_Flex2 = 
  regulartable(df) %>%
  set_formatter(a = function(x) sprintf("%.1f", x),
                b = function(x) sprintf("%.1f", x)) %>%
  width(width = c(0.5, 2)) %>%
  align(align = "center", part = "all")
df_Flex2

Output:

enter image description here

If you don't want to name individual columns you can set a revised default for numeric double types by using set_formatter_type in place of set_formatter. This example sets the number of decimal places for doubles in the regulartable to 1 decimal place in a piped expression:

set_formatter_type(fmt_double = "%.1f")

If not used with the pipe (%>%) you'd need to supply the name of the regulartable variable as the first argument:

set_formatter_type(df_Flex2, fmt_double = "%.1f")
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it works. Yet, if I work with decimal numbers, the default is to add "0" until three decimal number (try it with df = data.frame(a=1.1, b=1.1), it will displays 1.100.) I have looked in the documentation but did not find a way to deal with. ReporteRs does not have this behavior.
Now added details of how to format numbers. To set 0 decimal places (i.e. whole number only) use sprintf("%.0f", x) in the anonymous functions above.
@StewartRoss thanks for helping (I didn't see that post). I really appreciate this kind of accurate and qualitative help :)
You're welcome @David Gohel. You have designed these very, very useful packages, and putting a little help back in for others is the least I can do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.