2

I am using ReporteRs to generate a flexTable using the following piece of code:

library( ReporteRs )
baseCellProp <- cellProperties( padding = 2 )
baseParProp <- parProperties(text.align = "center")
ft.list <- list(mua= 1, mub = 2, mug = 1e-7)
ft.data <- t(data.frame(ft.list))

ft.FlexTable <-  FlexTable( data = ft.data,
                            add.rownames = TRUE,
                            header.columns = FALSE,
                            body.cell.props = baseCellProp,
                            body.par.props = baseParProp)

ft.FlexTable <- setFlexTableWidths(ft.FlexTable, widths =c(1,2))
ft.FlexTable

The problem I am having is all the numbers are coming out in scientific notation like this:

mua 1e+00
mub 2e+00
mug 1e-07

Is there a way to tell flexTable how to format numbers? Specifically do not apply the same format to all the rows, but apply a format that makes sense on a row-by-row basis?

Ron Jensen
  • 641
  • 7
  • 20

2 Answers2

1

I solved this problem using flextable in R Markdown document by adding set_formatter_type and controlling how all my double data type are presented. My output was padding everything with lots of zeros that I did not want.

```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(flextable)
library(tidyverse)
results <- as.tibble(read.csv("D:/ALLRESULTS.csv",header=TRUE))
results %>% na.omit() %>% filter(grepl("KEY-K",Alignment)) %>% 
      arrange(Year,Source) %>% 
      select(Source,Question,Year,Target,Actual,Highest,n) %>% 
      regulartable() %>% 
      set_formatter_type(fmt_double="%.01f") %>% 
      align(part="header",align="center") %>% 
      align(align="left") %>% autofit()
```
mysteRious
  • 4,102
  • 2
  • 16
  • 36
  • [examples can be found here](https://cran.r-project.org/web/packages/flextable/vignettes/examples.html) .Not particularly clear from this answer here (would suggest improving it): Using `set_formatter_type` requires a `regulartable` object and you have to specify the format for each column separately. – tjebo Dec 03 '18 at 13:59
0

I solved my problem using a hint from r transposing a data frame. The problem was transposing a data frame frame coerces all the elements to characters. Using matrices solves that problem:

ft.list <- list(mua= 1, mub = 2, mug = 1e-7)
ft.data <- as.matrix(ft.list, rownames.force=TRUE )
Community
  • 1
  • 1
Ron Jensen
  • 641
  • 7
  • 20