crsh / papaja

papaja (Preparing APA Journal Articles) is an R package that provides document formats to produce complete APA manuscripts from RMarkdown-files (PDF and Word documents) and helper functions that facilitate reporting statistics, tables, and plots.
https://frederikaust.com/papaja_man/
Other
652 stars 132 forks source link

print idented rows bold for pdf #223

Closed HenrikEckermann closed 6 years ago

HenrikEckermann commented 6 years ago

Hi Frederik,

To print idented rows bold, I used markdown code and this worked for the docx version:

colnames(df) <- sapply(colnames(df), function(x) glue("**{x}**"))
colnames(d)[1] <- ""
apa_table(
  list(
    `**Childsex**` = d[1:2,],
    `**Infant age at CC entrance (weeks)**` = d[3:5,],
    `**Maternal age**` = d[6:8,],
    `**Birthweight (gram)**` = d[9:11,]

  ),
  caption = table_caption_descr, 
  note = table_note_descr, 
  escape = F, 
  small = TRUE)

However, the same table is not scaled in the pdf (despite the small = TRUE arg) and thus is printed outside of page + it prints the ** literally. I use escape = F because within the df I use latex code. Is there some workaround implemented to make column names and the idented rownames bold?

Thanks

P.S.: I made a good experience using papaja lately, especially to collaborate with docx users I could get away without having to do to much extra work. I created 2 Rmd files that use your template. Bothe are almost empty despite the YAML header + the tables that are added and I needed to separate those due to docx and pdf differences. Inside both of these files, I use an R chunk with r child = 'body.Rmd' where body contains all the content that can be kept similar. If you know a better approach please give me a hint but this worked really well already...

crsh commented 6 years ago

Hi Henrik,

the deeper issue here is that the pandoc table representation is currently rather limited. Hence, apa_table() produces on LaTeX code for PDF documents by default. This means that you need to use LaTeX syntax to do typesetting. This of course makes it hard to get a proper Word version of your manuscript.

However, from what I can see, the extra flexibility granted LaTeX may not be necessary in this case. You could try to use format = "word" in your apa_print()-call to overwrite the default behavior. The generated table will then be in Markdown format and I would expect additional Markdown-typesetting to work.

The problem with small = TRUE may be a bug. I need to look into this (I don't think I'll get around to it today).

The approach you describe to go to both formats sounds useful. I'd be interested in what it is about figures that keeps you from having them in the shared document part. For tables there are, unfortunately, some known limitations in Word documents...

HenrikEckermann commented 6 years ago

In fact, I kept the figures in there as well. That was a mistake in my above description. I did not use apa_print. In this case the df is a demographics table as tibble. I prepared similarly model parameter tables of a PERMANOVA and LME in a tibble, which I passed to apa_table. By adding caption and tablenote as arguments and as in the above example indented rows, I ended up with apa conform tables in word. For the pdf version I used thus far kableExtra, which I also find very useful but with that I cannot produce apa tables for docx as easy as with papaja when I use your template. The I found that I could use apa_table for 2 of my tables for pdf and get the same output. Only the demographics table that I show above did not work...

crsh commented 6 years ago

Sorry, I meant to say include it in your call to apa_table():

colnames(df) <- sapply(colnames(df), function(x) glue("**{x}**"))
colnames(d)[1] <- ""
apa_table(
  list(
    `**Childsex**` = d[1:2,],
    `**Infant age at CC entrance (weeks)**` = d[3:5,],
    `**Maternal age**` = d[6:8,],
    `**Birthweight (gram)**` = d[9:11,]

  ),
  caption = table_caption_descr, 
  note = table_note_descr, 
  format = "word")

This should work for both Word and PDF documents. It would probably be best to rename this option to markdown...

HenrikEckermann commented 6 years ago

Thanks for the suggestion and I tried it out:

This throws an error due to the latex code I use in my df such as "$\\pm$". I now tried using latex for columns and row "headers" as well. In the following example only the first rowheader and all colnames are changed for testing and this works:

colnames(d) <- sapply(colnames(d), function(x) glue("$\\textbf{[x]}$", .open = "[", .close = "]"))
colnames(d)[1] <- ""
apa_table(
  list(
    `$\\textbf{Childsex}$` = d[1:2,],
    `**Infant age at CC entrance (weeks)**` = d[3:5,],
    `**Maternal age**` = d[6:8,],
    `**Birthweight (gram)**` = d[9:11,]

  ),
  caption = table_caption_descr, 
  note = "a short table note", 
  escape = F,
  small = TRUE)

Also, I tested whether the scaling issue is due to the long table note but that was not the problem. So, in case you find a solution to the scaling problem please let me know.

Thanks!

crsh commented 6 years ago

Hmm, I don't see the code for your first attempt. Generally, math should not be a problem for either format. Could you add the code so I can take alook?

HenrikEckermann commented 6 years ago

I tried exactly as you suggested in your answer. I attach a pic of the printed tible, once using the print function and once without so you can see how the tibble looks as passed into apa_table. The error is also on the pic:

screenshot

In case it is inconvenient to work with this information, maybe I can reproduce the same problem using a dataset available in R or generate a df.

mischavk commented 6 years ago

Hi Frederik, sorry for jumping in, but I'm currently trying to solve similar issues...

If I add the format command, I get the following error message. error

Also, as you mentioned it should be possible to format the table using latex, which approach would suffice here?

For example an equivalent to

note = "This is _special_" ?

All the commands I tried gave me errors.

crsh commented 6 years ago

Thanks again for reporting this! There was indeed a nasty bug due to R's default partial argument matching. 😒

This should now work:

apa_table(
  data.frame(c(1, 2), c(1, 2))
  , format = "markdown"
)
d <- cars[1:11, ]
d[1, 1] <- "1 $\\pm$ 2"
colnames(d) <- sapply(colnames(d), function(x) knitr::knit_expand(text = "$\\textbf{[x]}$", delim = c("[", "]")))
apa_table(
  list(
    `**Childsex**` = d[1:2,],
    `**Infant age at CC entrance (weeks)**` = d[3:5,],
    `**Maternal age**` = d[6:8,],
    `**Birthweight (gram)**` = d[9:11,]
  ),
  , format = "markdown"
  , escape = FALSE
)

Could you give it a try (devtools::install_github("crsh/papaja@devel")) to see if it works for you?

mischavk commented 6 years ago

Thank you for fixing this! I don't get any error messages any more. However, if I use format = "markdown", it seems some aspects of the table (eg. heading, note) are omitted. So how could I eg. use italics in the table note? Sorry if this question is trivial.

crsh commented 6 years ago

You are right, that's something I haven't addressed, yet.

  1. If you use apa_table() they are not omitted but printed in the body of the text. This is because the markdown format is intended for use with Word documents. I'll have to try and see whether this can be generalized. If you prefer to use knitr::kable() you can do the following (but I don't think there is a way to add a table note):
knitr::kable(
  data.frame(c(1, 2), c(1, 2))
  , caption = "This is a caption"
  , format = "pandoc"
)
  1. When you use apa_table() without specifying the markdown format, I recommend you use bookdown's text references to specify and format captions and notes. Put the following somewhere in the markdown parts of the document:
(ref:caption)
*This* is a caption.

Then you can use this text-reference in you call to, e.g., apa_table():

apa_table(
  data.frame(c(1, 2), c(1, 2))
  , caption = "(ref:caption)"
)
crsh commented 6 years ago

A solution for your initial question, @HenrikEckermann, could be to use LaTeX markup syntax:

textbf <- function(x) paste0("\\textbf{", x, "}")

df <- cars[1:11, ]
colnames(df) <- sapply(colnames(df), textbf)

df_list <- list(
  `Childsex` = df[1:2,],
  `Infant age at CC entrance (weeks)` = df[3:5,],
  `Maternal age` = df[6:8,],
  `Birthweight (gram)` = df[9:11,]
)
names(df_list) <- textbf(names(df_list))

apa_table(
  df_list
  , caption = "This is a caption."
  , escape = FALSE
)
mischavk commented 6 years ago

Thanks again!

HenrikEckermann commented 6 years ago

Thanks Frederik, this worked!

edit: I left it open since this was a workaround but feel free to close it!