5

I am having some troubles in formatting some tables using expss in an R Markdown. The output is a pdf file. knitr options are:

knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)

Following the vignette on expss (available here https://cran.r-project.org/web/packages/expss/vignettes/tables-with-labels.html), I have written the following code:

sl_expss_long %>%  # the tibble
  calc_cro_cpct(
    cell_vars = list(br, cl, cm, fgm, vd), # rows
    col_vars = list(total(), area) # columns
  ) %>% 
  set_caption("Table 1")

This code works just fine in R Studio, and produces this table:

 Table 1                                                                                    
 |                                      |              |  #Total |   Area |        |
 |                                      |              |         |  Rural |  Urban |
 | ------------------------------------ | ------------ | ------- | ------ | ------ |
 |                   Birth registration |        FALSE |    64.4 |   61.6 |   70.9 |
 |                                      |         TRUE |    35.6 |   38.4 |   29.1 |
 |                                      | #Total cases |  8207.0 | 5732.0 | 2475.0 |
 |                         Child labour |        FALSE |    50.5 |   47.0 |   64.2 |
 |                                      |         TRUE |    49.5 |   53.0 |   35.8 |
 |                                      | #Total cases |  5136.0 | 4085.0 | 1051.0 |
 |                       Child marriage |        FALSE |    98.8 |   98.6 |   99.2 |
 |                                      |         TRUE |     1.2 |    1.4 |    0.8 |
 |                                      | #Total cases | 12158.0 | 7827.0 | 4331.0 |
 | Female genitale mutilation / cutting |        FALSE |     8.2 |    7.6 |    9.3 |
 |                                      |         TRUE |    91.8 |   92.4 |   90.7 |
 |                                      | #Total cases |  9203.0 | 6144.0 | 3059.0 |
 |             Violent child discipline |        FALSE |     9.9 |   10.4 |    8.9 |
 |                                      |         TRUE |    90.1 |   89.6 |   91.1 |
 |                                      | #Total cases | 11547.0 | 7818.0 | 3729.0 |

Similarly, it works in R Studio with the following code:

sl_expss_long %>% 
  tab_cells(br, cl, cm, fgm, vd) %>%
  tab_cols(total(), area) %>% 
  tab_stat_cpct() %>%
    tab_pivot() %>% 
  set_caption("Table with summary statistics and significance marks.")

However, when I put the code into my R Markdown, I get the following result:

Table with summary statistics
National
Area
Rural
Urban
Birth registration
FALSE
64.4
61.6
70.9
TRUE
35.6
38.4
29.1
#Total cases
8207

etc.

My table is just one column wide and three pages long.

I temporarily fixed it using pander, kable and kableExtra>

sl_expss_long %>% 
  tab_cells(br, cl, cm, fgm, vd) %>%
  tab_cols(total(label = " National| |"), area) %>% 
  tab_stat_cpct() %>%
  tab_pivot() %>% 
  set_caption("Table with summary statistics") %>% 
  split_table_to_df() %>% 
  kable(caption = "Table with summary statistics") %>% 
  kable_styling(bootstrap_options = c("striped"),
               latex_options = "hold_position") %>% 
  row_spec(1:2, bold = TRUE)

and get this result:

output table from the previous code

As you can see, the table from R Markdown has added a row at the beginning of the table, and the word "area" is supposed to be on top of both "Rural" and Urban". This is based on my understanding from the vignette is that using expss in a R Markdown would produce the table that we can see in the vignette.

expss table example from vignette

Any help on what I might be missing?

Many thanks in advance

Manolo

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Manolo
  • 73
  • 8
  • Update, if I run the current chunk alone, tables are displayed correctly. Therefore, I would assume there is something wrong with the knitting options? – Manolo Sep 30 '19 at 11:50

1 Answers1

0

Can you say, what kind of document you are knittering? In my case, it works with HTML documents, as it is said in the vignette. But for Word or PDF /Latex documents, it does not work and does the same as shown in the question.

However, I am also looking forwards to a possibility to have the same nice tables in PDF or Word.

R learner
  • 101
  • 7